14 private $streams = [];
16 private $seekable =
true;
26 foreach ($streams as $stream) {
36 }
catch (\Exception $e) {
48 public function addStream(StreamInterface $stream)
50 if (!$stream->isReadable()) {
51 throw new \InvalidArgumentException(
'Each stream must be readable');
55 if (!$stream->isSeekable()) {
56 $this->seekable =
false;
59 $this->streams[] = $stream;
72 public function close()
74 $this->pos = $this->current = 0;
75 $this->seekable =
true;
77 foreach ($this->streams as $stream) {
93 $this->pos = $this->current = 0;
94 $this->seekable =
true;
96 foreach ($this->streams as $stream) {
103 public function tell()
120 foreach ($this->streams as $stream) {
121 $s = $stream->getSize();
131 public function eof()
133 return !$this->streams ||
134 ($this->current >= count($this->streams) - 1 &&
135 $this->streams[$this->current]->eof());
148 public function seek($offset, $whence = SEEK_SET)
150 if (!$this->seekable) {
151 throw new \RuntimeException(
'This AppendStream is not seekable');
152 } elseif ($whence !== SEEK_SET) {
153 throw new \RuntimeException(
'The AppendStream can only seek with SEEK_SET');
156 $this->pos = $this->current = 0;
159 foreach ($this->streams as $i => $stream) {
162 }
catch (\Exception $e) {
163 throw new \RuntimeException(
'Unable to seek stream '
164 . $i .
' of the AppendStream', 0, $e);
169 while ($this->pos < $offset && !$this->
eof()) {
170 $result = $this->
read(min(8096, $offset - $this->pos));
171 if ($result ===
'') {
182 public function read($length)
185 $total = count($this->streams) - 1;
186 $remaining = $length;
187 $progressToNext =
false;
189 while ($remaining > 0) {
192 if ($progressToNext || $this->streams[$this->current]->
eof()) {
193 $progressToNext =
false;
194 if ($this->current === $total) {
200 $result = $this->streams[$this->current]->read($remaining);
203 if ($result ==
null) {
204 $progressToNext =
true;
209 $remaining = $length - strlen($buffer);
212 $this->pos += strlen($buffer);
229 return $this->seekable;
232 public function write($string)
234 throw new \RuntimeException(
'Cannot write to an AppendStream');
239 return $key ? null : [];