ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
BufferStream.php
Go to the documentation of this file.
1<?php
2namespace GuzzleHttp\Psr7;
3
5
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}
$result
An exception for terminatinating execution or to throw for unit testing.
Provides a buffer stream that can be written to to fill a buffer, and read from to remove bytes from ...
seek($offset, $whence=SEEK_SET)
Seek to a position in the stream.
isSeekable()
Returns whether or not the stream is seekable.
close()
Closes the stream and any underlying resources.
__toString()
Reads all data from the stream into a string, from the beginning to end.
write($string)
Writes data to the buffer.
eof()
Returns true if the stream is at the end of the stream.
isReadable()
Returns whether or not the stream is readable.
isWritable()
Returns whether or not the stream is writable.
read($length)
Reads data from the buffer.
rewind()
Seek to the beginning of the stream.
getMetadata($key=null)
Get stream metadata as an associative array or retrieve a specific key.
getContents()
Returns the remaining contents in a string.
tell()
Returns the current position of the file read/write pointer.
getSize()
Get the size of the stream if known.
detach()
Separates any underlying resources from the stream.
$key
Definition: croninfo.php:18
Describes a data stream.