2
namespace GuzzleHttp\Stream;
4
use GuzzleHttp\Stream\Exception\CannotAttachException;
7
* Provides a buffer stream that can be written to to fill a buffer, and read
8
* from to remove bytes from the buffer.
10
* This stream returns a "hwm" metadata value that tells upstream consumers
11
* what the configured high water mark of the stream is, or the maximum
12
* preferred size of the buffer.
14
* @package GuzzleHttp\Stream
16
class BufferStream implements StreamInterface
22
* @param int $hwm High water mark, representing the preferred maximum
23
* buffer size. If the size of the buffer exceeds the high
24
* water mark, then calls to write will continue to succeed
25
* but will return false to inform writers to slow down
26
* until the buffer has been drained by reading from it.
28
public function __construct($hwm = 16384)
33
public function __toString()
35
return $this->getContents();
38
public function getContents()
40
$buffer = $this->buffer;
46
public function close()
51
public function detach()
56
public function attach($stream)
58
throw new CannotAttachException();
61
public function getSize()
63
return strlen($this->buffer);
66
public function isReadable()
71
public function isWritable()
76
public function isSeekable()
81
public function seek($offset, $whence = SEEK_SET)
88
return strlen($this->buffer) === 0;
91
public function tell()
97
* Reads data from the buffer.
99
public function read($length)
101
$currentLength = strlen($this->buffer);
103
if ($length >= $currentLength) {
104
// No need to slice the buffer because we don't have enough data.
105
$result = $this->buffer;
108
// Slice up the result to provide a subset of the buffer.
109
$result = substr($this->buffer, 0, $length);
110
$this->buffer = substr($this->buffer, $length);
117
* Writes data to the buffer.
119
public function write($string)
121
$this->buffer .= $string;
123
if (strlen($this->buffer) >= $this->hwm) {
127
return strlen($string);
130
public function getMetadata($key = null)
136
return $key ? null : [];