12 use StreamDecoratorTrait;
32 $this->stream = $stream;
40 if ($this->stream->eof()) {
45 if ($this->limit == -1) {
49 return $this->stream->tell() >= $this->offset + $this->limit;
58 if (
null === ($length = $this->stream->getSize())) {
60 } elseif ($this->limit == -1) {
61 return $length - $this->offset;
63 return min($this->limit, $length - $this->offset);
71 public function seek($offset, $whence = SEEK_SET)
73 if ($whence !== SEEK_SET || $offset < 0) {
74 throw new \RuntimeException(sprintf(
75 'Cannot seek to offset %s with whence %s',
81 $offset += $this->offset;
83 if ($this->limit !== -1) {
84 if ($offset > $this->offset + $this->limit) {
85 $offset = $this->offset + $this->limit;
89 $this->stream->seek($offset);
96 public function tell()
98 return $this->stream->tell() - $this->offset;
110 $current = $this->stream->tell();
112 if ($current !== $offset) {
114 if ($this->stream->isSeekable()) {
115 $this->stream->seek($offset);
116 } elseif ($current > $offset) {
117 throw new \RuntimeException(
"Could not seek to stream offset $offset");
119 $this->stream->read($offset - $current);
123 $this->offset = $offset;
135 $this->limit = $limit;
138 public function read($length)
140 if ($this->limit == -1) {
141 return $this->stream->read($length);
146 $remaining = ($this->offset + $this->limit) - $this->stream->tell();
147 if ($remaining > 0) {
150 return $this->stream->read(min($remaining, $length));