ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
BufferStream.php
Go to the documentation of this file.
1 <?php
2 namespace GuzzleHttp\Psr7;
3 
5 
14 class BufferStream implements StreamInterface
15 {
16  private $hwm;
17  private $buffer = '';
18 
26  public function __construct($hwm = 16384)
27  {
28  $this->hwm = $hwm;
29  }
30 
31  public function __toString()
32  {
33  return $this->getContents();
34  }
35 
36  public function getContents()
37  {
39  $this->buffer = '';
40 
41  return $buffer;
42  }
43 
44  public function close()
45  {
46  $this->buffer = '';
47  }
48 
49  public function detach()
50  {
51  $this->close();
52  }
53 
54  public function getSize()
55  {
56  return strlen($this->buffer);
57  }
58 
59  public function isReadable()
60  {
61  return true;
62  }
63 
64  public function isWritable()
65  {
66  return true;
67  }
68 
69  public function isSeekable()
70  {
71  return false;
72  }
73 
74  public function rewind()
75  {
76  $this->seek(0);
77  }
78 
79  public function seek($offset, $whence = SEEK_SET)
80  {
81  throw new \RuntimeException('Cannot seek a BufferStream');
82  }
83 
84  public function eof()
85  {
86  return strlen($this->buffer) === 0;
87  }
88 
89  public function tell()
90  {
91  throw new \RuntimeException('Cannot determine the position of a BufferStream');
92  }
93 
97  public function read($length)
98  {
99  $currentLength = strlen($this->buffer);
100 
101  if ($length >= $currentLength) {
102  // No need to slice the buffer because we don't have enough data.
104  $this->buffer = '';
105  } else {
106  // Slice up the result to provide a subset of the buffer.
107  $result = substr($this->buffer, 0, $length);
108  $this->buffer = substr($this->buffer, $length);
109  }
110 
111  return $result;
112  }
113 
117  public function write($string)
118  {
119  $this->buffer .= $string;
120 
121  // TODO: What should happen here?
122  if (strlen($this->buffer) >= $this->hwm) {
123  return false;
124  }
125 
126  return strlen($string);
127  }
128 
129  public function getMetadata($key = null)
130  {
131  if ($key == 'hwm') {
132  return $this->hwm;
133  }
134 
135  return $key ? null : [];
136  }
137 }
eof()
Returns true if the stream is at the end of the stream.
seek($offset, $whence=SEEK_SET)
Seek to a position in the stream.
$result
close()
Closes the stream and any underlying resources.
Provides a buffer stream that can be written to to fill a buffer, and read from to remove bytes from ...
getSize()
Get the size of the stream if known.
rewind()
Seek to the beginning of the stream.
tell()
Returns the current position of the file read/write pointer.
write($string)
Writes data to the buffer.
read($length)
Reads data from the buffer.
isWritable()
Returns whether or not the stream is writable.
isReadable()
Returns whether or not the stream is readable.
detach()
Separates any underlying resources from the stream.
isSeekable()
Returns whether or not the stream is seekable.
getContents()
Returns the remaining contents in a string.
__toString()
Reads all data from the stream into a string, from the beginning to end.
$key
Definition: croninfo.php:18
getMetadata($key=null)
Get stream metadata as an associative array or retrieve a specific key.
Describes a data stream.