-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathMemoryHandle.php
175 lines (148 loc) · 3.79 KB
/
MemoryHandle.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
declare(strict_types=1);
namespace Psl\IO;
use Psl\DateTime\Duration;
use Psl\Math;
use function str_repeat;
use function strlen;
use function substr;
final class MemoryHandle implements CloseSeekReadWriteHandleInterface
{
use WriteHandleConvenienceMethodsTrait;
use ReadHandleConvenienceMethodsTrait;
/**
* @var int<0, max>
*/
private int $offset = 0;
private string $buffer;
private bool $closed = false;
private bool $reachedEof = false;
/**
* @psalm-external-mutation-free
*/
public function __construct(string $buffer = '')
{
$this->buffer = $buffer;
}
/**
* {@inheritDoc}
*
* @psalm-mutation-free
*/
public function reachedEndOfDataSource(): bool
{
$this->assertHandleIsOpen();
return $this->reachedEof;
}
/**
* {@inheritDoc}
*
* @psalm-external-mutation-free
*/
public function tryRead(null|int $max_bytes = null): string
{
$this->assertHandleIsOpen();
if (null === $max_bytes) {
$max_bytes = Math\INT64_MAX;
}
$length = strlen($this->buffer);
if ($this->offset >= $length) {
$this->reachedEof = true;
return '';
}
$length -= $this->offset;
$length = $length > $max_bytes ? $max_bytes : $length;
$result = substr($this->buffer, $this->offset, $length);
$this->offset = ($offset = $this->offset + $length) >= 0 ? $offset : 0;
return $result;
}
/**
* {@inheritDoc}
*
* @psalm-external-mutation-free
*/
public function read(null|int $max_bytes = null, null|Duration $timeout = null): string
{
return $this->tryRead($max_bytes);
}
/**
* {@inheritDoc}
*
* @psalm-external-mutation-free
*/
public function seek(int $offset): void
{
$this->assertHandleIsOpen();
$this->offset = $offset;
}
/**
* {@inheritDoc}
*
* @psalm-mutation-free
*/
public function tell(): int
{
$this->assertHandleIsOpen();
return $this->offset;
}
/**
* {@inheritDoc}
*
* @psalm-external-mutation-free
*/
public function tryWrite(string $bytes, null|Duration $timeout = null): int
{
$this->assertHandleIsOpen();
$length = strlen($this->buffer);
if ($length < $this->offset) {
$this->buffer .= str_repeat("\0", $this->offset - $length);
$length = $this->offset;
}
$bytes_length = strlen($bytes);
$new = substr($this->buffer, 0, $this->offset) . $bytes;
if ($this->offset < $length) {
$offset = $this->offset + $bytes_length;
$offset = $offset > $length ? $length : $offset;
$new .= substr($this->buffer, $offset);
}
$this->buffer = $new;
$this->offset = ($offset = $this->offset + $bytes_length) >= 0 ? $offset : 0;
return $bytes_length;
}
/**
* {@inheritDoc}
*
* @psalm-external-mutation-free
*/
public function write(string $bytes, null|Duration $timeout = null): int
{
return $this->tryWrite($bytes);
}
/**
* {@inheritDoc}
*
* @psalm-external-mutation-free
*/
public function close(): void
{
$this->closed = true;
}
/**
* @psalm-mutation-free
*/
public function getBuffer(): string
{
return $this->buffer;
}
/**
* @throws Exception\AlreadyClosedException If the handle has been already closed.
*
* @psalm-mutation-free
*/
private function assertHandleIsOpen(): void
{
if ($this->closed) {
throw new Exception\AlreadyClosedException('Handle has already been closed.');
}
}
}