2 namespace GuzzleHttp\Psr7;
4 use Psr\Http\Message\StreamInterface;
8 * Decorator used to return only a subset of a stream
10 class LimitStream implements StreamInterface
12 use StreamDecoratorTrait;
14 /** @var int Offset to start reading from */
17 /** @var int Limit the number of bytes that can be read */
21 * @param StreamInterface $stream Stream to wrap
22 * @param int $limit Total number of bytes to allow to be read
23 * from the stream. Pass -1 for no limit.
24 * @param int $offset Position to seek to before reading (only
25 * works on seekable streams).
27 public function __construct(
28 StreamInterface $stream,
32 $this->stream = $stream;
33 $this->setLimit($limit);
34 $this->setOffset($offset);
39 // Always return true if the underlying stream is EOF
40 if ($this->stream->eof()) {
44 // No limit and the underlying stream is not at EOF
45 if ($this->limit == -1) {
49 return $this->stream->tell() >= $this->offset + $this->limit;
53 * Returns the size of the limited subset of data
56 public function getSize()
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);
68 * Allow for a bounded seek on the read limited stream
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 % 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);
93 * Give a relative tell()
96 public function tell()
98 return $this->stream->tell() - $this->offset;
102 * Set the offset to start limiting from
104 * @param int $offset Offset to seek to and begin byte limiting from
106 * @throws \RuntimeException if the stream cannot be seeked.
108 public function setOffset($offset)
110 $current = $this->stream->tell();
112 if ($current !== $offset) {
113 // If the stream cannot seek to the offset position, then read to it
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;
127 * Set the limit of bytes that the decorator allows to be read from the
130 * @param int $limit Number of bytes to allow to be read from the stream.
131 * Use -1 for no limit.
133 public function setLimit($limit)
135 $this->limit = $limit;
138 public function read($length)
140 if ($this->limit == -1) {
141 return $this->stream->read($length);
144 // Check if the current position is less than the total allowed
145 // bytes + original offset
146 $remaining = ($this->offset + $this->limit) - $this->stream->tell();
147 if ($remaining > 0) {
148 // Only return the amount of requested data, ensuring that the byte
149 // limit is not exceeded
150 return $this->stream->read(min($remaining, $length));