ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
GuzzleHttp\Psr7\CachingStream Class Reference

Stream decorator that can cache previously read bytes from a sequentially read stream. More...

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

Public Member Functions

 __construct (StreamInterface $stream, StreamInterface $target=null)
 We will treat the buffer object as the body of the stream. More...
 
 getSize ()
 Get the size of the stream if known. More...
 
 rewind ()
 Seek to the beginning of the stream. More...
 
 seek ($offset, $whence=SEEK_SET)
 Seek to a position in the stream. More...
 
 read ($length)
 Read data from the stream. More...
 
 write ($string)
 Write data to the stream. More...
 
 eof ()
 Returns true if the stream is at the end of the stream. More...
 
 close ()
 Close both the remote stream and buffer 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...
 
 getSize ()
 Get the size of the stream if known. More...
 
 tell ()
 Returns the current position of the file read/write pointer. More...
 
 eof ()
 Returns true if the stream is at the end of the stream. More...
 
 isSeekable ()
 Returns whether or not the stream is seekable. More...
 
 seek ($offset, $whence=SEEK_SET)
 Seek to a position in the stream. 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...
 
 read ($length)
 Read data from the stream. 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 Member Functions

 cacheEntireStream ()
 

Private Attributes

 $remoteStream
 
 $skipReadBytes = 0
 

Detailed Description

Stream decorator that can cache previously read bytes from a sequentially read stream.

Definition at line 10 of file CachingStream.php.

Constructor & Destructor Documentation

◆ __construct()

GuzzleHttp\Psr7\CachingStream::__construct ( StreamInterface  $stream,
StreamInterface  $target = null 
)

We will treat the buffer object as the body of the stream.

Parameters
StreamInterface$streamStream to cache
StreamInterface$targetOptionally specify where data is cached

Definition at line 26 of file CachingStream.php.

29 {
30 $this->remoteStream = $stream;
31 $this->stream = $target ?: new Stream(fopen('php://temp', 'r+'));
32 }
$target
Definition: test.php:19
$stream
PHP stream implementation.

References GuzzleHttp\Psr7\$stream, and $target.

Member Function Documentation

◆ cacheEntireStream()

GuzzleHttp\Psr7\CachingStream::cacheEntireStream ( )
private

Definition at line 131 of file CachingStream.php.

132 {
133 $target = new FnStream(['write' => 'strlen']);
134 copy_to_stream($this, $target);
135
136 return $this->tell();
137 }
tell()
Returns the current position of the file read/write pointer.
copy_to_stream(StreamInterface $source, StreamInterface $dest, $maxLen=-1)
Copy the contents of a stream into another stream until the given number of bytes have been read.
Definition: functions.php:369

References $target, GuzzleHttp\Psr7\copy_to_stream(), and Psr\Http\Message\StreamInterface\tell().

Referenced by GuzzleHttp\Psr7\CachingStream\seek().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ close()

GuzzleHttp\Psr7\CachingStream::close ( )

Close both the remote stream and buffer stream.

Implements Psr\Http\Message\StreamInterface.

Definition at line 126 of file CachingStream.php.

127 {
128 $this->remoteStream->close() && $this->stream->close();
129 }

◆ eof()

GuzzleHttp\Psr7\CachingStream::eof ( )

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

Returns
bool

Implements Psr\Http\Message\StreamInterface.

Definition at line 118 of file CachingStream.php.

119 {
120 return $this->stream->eof() && $this->remoteStream->eof();
121 }

◆ getSize()

GuzzleHttp\Psr7\CachingStream::getSize ( )

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 34 of file CachingStream.php.

35 {
36 return max($this->stream->getSize(), $this->remoteStream->getSize());
37 }

◆ read()

GuzzleHttp\Psr7\CachingStream::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

RuntimeException if an error occurs.

Implements Psr\Http\Message\StreamInterface.

Definition at line 75 of file CachingStream.php.

76 {
77 // Perform a regular read on any previously read data from the buffer
78 $data = $this->stream->read($length);
79 $remaining = $length - strlen($data);
80
81 // More data was requested so read from the remote stream
82 if ($remaining) {
83 // If data was written to the buffer in a position that would have
84 // been filled from the remote stream, then we must skip bytes on
85 // the remote stream to emulate overwriting bytes from that
86 // position. This mimics the behavior of other PHP stream wrappers.
87 $remoteData = $this->remoteStream->read(
88 $remaining + $this->skipReadBytes
89 );
90
91 if ($this->skipReadBytes) {
92 $len = strlen($remoteData);
93 $remoteData = substr($remoteData, $this->skipReadBytes);
94 $this->skipReadBytes = max(0, $this->skipReadBytes - $len);
95 }
96
97 $data .= $remoteData;
98 $this->stream->write($remoteData);
99 }
100
101 return $data;
102 }
$data
Definition: bench.php:6
if($state['core:TerminatedAssocId'] !==null) $remaining

References $data, and $remaining.

Referenced by GuzzleHttp\Psr7\CachingStream\seek().

+ Here is the caller graph for this function:

◆ rewind()

GuzzleHttp\Psr7\CachingStream::rewind ( )

Seek to the beginning of the stream.

If the stream is not seekable, this method will raise an exception; otherwise, it will perform a seek(0).

See also
seek() on failure.

Implements Psr\Http\Message\StreamInterface.

Definition at line 39 of file CachingStream.php.

40 {
41 $this->seek(0);
42 }
seek($offset, $whence=SEEK_SET)
Seek to a position in the stream.

References GuzzleHttp\Psr7\CachingStream\seek().

+ Here is the call graph for this function:

◆ seek()

GuzzleHttp\Psr7\CachingStream::seek (   $offset,
  $whence = SEEK_SET 
)

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 44 of file CachingStream.php.

45 {
46 if ($whence == SEEK_SET) {
47 $byte = $offset;
48 } elseif ($whence == SEEK_CUR) {
49 $byte = $offset + $this->tell();
50 } elseif ($whence == SEEK_END) {
51 $size = $this->remoteStream->getSize();
52 if ($size === null) {
53 $size = $this->cacheEntireStream();
54 }
55 $byte = $size + $offset;
56 } else {
57 throw new \InvalidArgumentException('Invalid whence');
58 }
59
60 $diff = $byte - $this->stream->getSize();
61
62 if ($diff > 0) {
63 // Read the remoteStream until we have read in at least the amount
64 // of bytes requested, or we reach the end of the file.
65 while ($diff > 0 && !$this->remoteStream->eof()) {
66 $this->read($diff);
67 $diff = $byte - $this->stream->getSize();
68 }
69 } else {
70 // We can just do a normal seek since we've already seen this byte.
71 $this->stream->seek($byte);
72 }
73 }
$size
Definition: RandomTest.php:84
read($length)
Read data from the stream.

References $size, GuzzleHttp\Psr7\CachingStream\cacheEntireStream(), GuzzleHttp\Psr7\CachingStream\read(), and Psr\Http\Message\StreamInterface\tell().

Referenced by GuzzleHttp\Psr7\CachingStream\rewind().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ write()

GuzzleHttp\Psr7\CachingStream::write (   $string)

Write data to the stream.

Parameters
string$stringThe string that is to be written.
Returns
int Returns the number of bytes written to the stream.
Exceptions

RuntimeException on failure.

Implements Psr\Http\Message\StreamInterface.

Definition at line 104 of file CachingStream.php.

105 {
106 // When appending to the end of the currently read stream, you'll want
107 // to skip bytes from being read from the remote stream to emulate
108 // other stream wrappers. Basically replacing bytes of data of a fixed
109 // length.
110 $overflow = (strlen($string) + $this->tell()) - $this->remoteStream->tell();
111 if ($overflow > 0) {
112 $this->skipReadBytes += $overflow;
113 }
114
115 return $this->stream->write($string);
116 }

References Psr\Http\Message\StreamInterface\tell().

+ Here is the call graph for this function:

Field Documentation

◆ $remoteStream

GuzzleHttp\Psr7\CachingStream::$remoteStream
private

Definition at line 15 of file CachingStream.php.

◆ $skipReadBytes

GuzzleHttp\Psr7\CachingStream::$skipReadBytes = 0
private

Definition at line 18 of file CachingStream.php.


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