ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
GuzzleHttp\Psr7\LimitStream Class Reference

Decorator used to return only a subset of a stream. More...

+ Inheritance diagram for GuzzleHttp\Psr7\LimitStream:
+ Collaboration diagram for GuzzleHttp\Psr7\LimitStream:

Public Member Functions

 __construct (StreamInterface $stream, $limit=-1, $offset=0)
 
 eof ()
 Returns true if the stream is at the end of the stream. More...
 
 getSize ()
 Returns the size of the limited subset of data {Get the size of the stream if known.
Returns
int|null Returns the size in bytes if known, or null if unknown.
}. More...
 
 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.}. More...
 
 tell ()
 Give a relative tell() {Returns the current position of the file read/write pointer.
Returns
int Position of the file pointer
Exceptions
}. More...
 
 setOffset ($offset)
 Set the offset to start limiting from. More...
 
 setLimit ($limit)
 Set the limit of bytes that the decorator allows to be read from the stream. More...
 
 read ($length)
 Read data from the stream. More...
 
- Public Member Functions inherited from Psr\Http\Message\StreamInterface
 __toString ()
 Reads all data from the stream into a string, from the beginning to end. More...
 
 close ()
 Closes the stream and any underlying resources. More...
 
 detach ()
 Separates any underlying resources from the stream. More...
 
 isSeekable ()
 Returns whether or not the stream is seekable. More...
 
 rewind ()
 Seek to the beginning of the stream. More...
 
 isWritable ()
 Returns whether or not the stream is writable. More...
 
 write ($string)
 Write data to the stream. More...
 
 isReadable ()
 Returns whether or not the stream is readable. More...
 
 getContents ()
 Returns the remaining contents in a string. More...
 
 getMetadata ($key=null)
 Get stream metadata as an associative array or retrieve a specific key. More...
 

Private Attributes

 $offset
 
 $limit
 

Detailed Description

Decorator used to return only a subset of a stream.

Definition at line 10 of file LimitStream.php.

Constructor & Destructor Documentation

◆ __construct()

GuzzleHttp\Psr7\LimitStream::__construct ( StreamInterface  $stream,
  $limit = -1,
  $offset = 0 
)
Parameters
StreamInterface$streamStream to wrap
int$limitTotal number of bytes to allow to be read from the stream. Pass -1 for no limit.
int$offsetPosition to seek to before reading (only works on seekable streams).

Definition at line 27 of file LimitStream.php.

References GuzzleHttp\Psr7\LimitStream\$limit, GuzzleHttp\Psr7\LimitStream\$offset, GuzzleHttp\Psr7\$stream, GuzzleHttp\Psr7\LimitStream\setLimit(), and GuzzleHttp\Psr7\LimitStream\setOffset().

31  {
32  $this->stream = $stream;
33  $this->setLimit($limit);
34  $this->setOffset($offset);
35  }
$stream
PHP stream implementation.
setLimit($limit)
Set the limit of bytes that the decorator allows to be read from the stream.
setOffset($offset)
Set the offset to start limiting from.
+ Here is the call graph for this function:

Member Function Documentation

◆ eof()

GuzzleHttp\Psr7\LimitStream::eof ( )

Returns true if the stream is at the end of the stream.

Returns
bool

Implements Psr\Http\Message\StreamInterface.

Definition at line 37 of file LimitStream.php.

References GuzzleHttp\Psr7\LimitStream\$limit.

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  }

◆ getSize()

GuzzleHttp\Psr7\LimitStream::getSize ( )

Returns the size of the limited subset of data {Get the size of the stream if known.

Returns
int|null Returns the size in bytes if known, or null if unknown.
}.

Implements Psr\Http\Message\StreamInterface.

Definition at line 56 of file LimitStream.php.

References GuzzleHttp\Psr7\LimitStream\$offset.

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  }

◆ read()

GuzzleHttp\Psr7\LimitStream::read (   $length)

Read data from the stream.

Parameters
int$lengthRead up to $length bytes from the object and return them. Fewer than $length bytes may be returned if underlying stream call returns fewer bytes.
Returns
string Returns the data read from the stream, or an empty string if no bytes are available.
Exceptions

Implements Psr\Http\Message\StreamInterface.

Definition at line 138 of file LimitStream.php.

References GuzzleHttp\Psr7\LimitStream\$limit, and $remaining.

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  }
if($state['core:TerminatedAssocId'] !==null) $remaining

◆ seek()

GuzzleHttp\Psr7\LimitStream::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.}.

Implements Psr\Http\Message\StreamInterface.

Definition at line 71 of file LimitStream.php.

References GuzzleHttp\Psr7\LimitStream\$limit, and GuzzleHttp\Psr7\LimitStream\$offset.

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  }

◆ setLimit()

GuzzleHttp\Psr7\LimitStream::setLimit (   $limit)

Set the limit of bytes that the decorator allows to be read from the stream.

Parameters
int$limitNumber of bytes to allow to be read from the stream. Use -1 for no limit.

Definition at line 133 of file LimitStream.php.

References GuzzleHttp\Psr7\LimitStream\$limit.

Referenced by GuzzleHttp\Psr7\LimitStream\__construct().

134  {
135  $this->limit = $limit;
136  }
+ Here is the caller graph for this function:

◆ setOffset()

GuzzleHttp\Psr7\LimitStream::setOffset (   $offset)

Set the offset to start limiting from.

Parameters
int$offsetOffset to seek to and begin byte limiting from
Exceptions

Definition at line 108 of file LimitStream.php.

References $current, and GuzzleHttp\Psr7\LimitStream\$offset.

Referenced by GuzzleHttp\Psr7\LimitStream\__construct().

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  }
+ Here is the caller graph for this function:

◆ tell()

GuzzleHttp\Psr7\LimitStream::tell ( )

Give a relative tell() {Returns the current position of the file read/write pointer.

Returns
int Position of the file pointer
Exceptions
}.

Implements Psr\Http\Message\StreamInterface.

Definition at line 96 of file LimitStream.php.

References GuzzleHttp\Psr7\LimitStream\$offset.

97  {
98  return $this->stream->tell() - $this->offset;
99  }

Field Documentation

◆ $limit

◆ $offset


The documentation for this class was generated from the following file: