ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
LimitStream.php
Go to the documentation of this file.
1 <?php
2 namespace GuzzleHttp\Psr7;
3 
5 
6 
10 class LimitStream implements StreamInterface
11 {
13 
15  private $offset;
16 
18  private $limit;
19 
27  public function __construct(
29  $limit = -1,
30  $offset = 0
31  ) {
32  $this->stream = $stream;
33  $this->setLimit($limit);
34  $this->setOffset($offset);
35  }
36 
37  public function eof()
38  {
39  // Always return true if the underlying stream is EOF
40  if ($this->stream->eof()) {
41  return true;
42  }
43 
44  // No limit and the underlying stream is not at EOF
45  if ($this->limit == -1) {
46  return false;
47  }
48 
49  return $this->stream->tell() >= $this->offset + $this->limit;
50  }
51 
56  public function getSize()
57  {
58  if (null === ($length = $this->stream->getSize())) {
59  return null;
60  } elseif ($this->limit == -1) {
61  return $length - $this->offset;
62  } else {
63  return min($this->limit, $length - $this->offset);
64  }
65  }
66 
71  public function seek($offset, $whence = SEEK_SET)
72  {
73  if ($whence !== SEEK_SET || $offset < 0) {
74  throw new \RuntimeException(sprintf(
75  'Cannot seek to offset % with whence %s',
76  $offset,
77  $whence
78  ));
79  }
80 
82 
83  if ($this->limit !== -1) {
84  if ($offset > $this->offset + $this->limit) {
85  $offset = $this->offset + $this->limit;
86  }
87  }
88 
89  $this->stream->seek($offset);
90  }
91 
96  public function tell()
97  {
98  return $this->stream->tell() - $this->offset;
99  }
100 
108  public function setOffset($offset)
109  {
110  $current = $this->stream->tell();
111 
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");
118  } else {
119  $this->stream->read($offset - $current);
120  }
121  }
122 
123  $this->offset = $offset;
124  }
125 
133  public function setLimit($limit)
134  {
135  $this->limit = $limit;
136  }
137 
138  public function read($length)
139  {
140  if ($this->limit == -1) {
141  return $this->stream->read($length);
142  }
143 
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));
151  }
152 
153  return '';
154  }
155 }
eof()
Returns true if the stream is at the end of the stream.
Definition: LimitStream.php:37
Decorator used to return only a subset of a stream.
Definition: LimitStream.php:10
__construct(StreamInterface $stream, $limit=-1, $offset=0)
Definition: LimitStream.php:27
if($state['core:TerminatedAssocId'] !==null) $remaining
$stream
PHP stream implementation.
read($length)
Read data from the stream.
getSize()
Returns the size of the limited subset of data {Get the size of the stream if known.int|null Returns the size in bytes if known, or null if unknown.}.
Definition: LimitStream.php:56
tell()
Give a relative tell() {Returns the current position of the file read/write pointer.int Position of the file pointer }.
Definition: LimitStream.php:96
seek($offset, $whence=SEEK_SET)
Allow for a bounded seek on the read limited stream {Seek to a position in the stream.int $offset Stream offset int $whence Specifies how the cursor position will be calculated based on the seek offset. Valid values are identical to the built-in PHP $whence values for fseek(). SEEK_SET: Set position equal to offset bytes SEEK_CUR: Set position to current location plus offset SEEK_END: Set position to end-of-stream plus offset. on failure.}.
Definition: LimitStream.php:71
setLimit($limit)
Set the limit of bytes that the decorator allows to be read from the stream.
Describes a data stream.
setOffset($offset)
Set the offset to start limiting from.