annotate vendor/guzzlehttp/psr7/src/BufferStream.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2 namespace GuzzleHttp\Psr7;
Chris@0 3
Chris@0 4 use Psr\Http\Message\StreamInterface;
Chris@0 5
Chris@0 6 /**
Chris@0 7 * Provides a buffer stream that can be written to to fill a buffer, and read
Chris@0 8 * from to remove bytes from the buffer.
Chris@0 9 *
Chris@0 10 * This stream returns a "hwm" metadata value that tells upstream consumers
Chris@0 11 * what the configured high water mark of the stream is, or the maximum
Chris@0 12 * preferred size of the buffer.
Chris@0 13 */
Chris@0 14 class BufferStream implements StreamInterface
Chris@0 15 {
Chris@0 16 private $hwm;
Chris@0 17 private $buffer = '';
Chris@0 18
Chris@0 19 /**
Chris@0 20 * @param int $hwm High water mark, representing the preferred maximum
Chris@0 21 * buffer size. If the size of the buffer exceeds the high
Chris@0 22 * water mark, then calls to write will continue to succeed
Chris@0 23 * but will return false to inform writers to slow down
Chris@0 24 * until the buffer has been drained by reading from it.
Chris@0 25 */
Chris@0 26 public function __construct($hwm = 16384)
Chris@0 27 {
Chris@0 28 $this->hwm = $hwm;
Chris@0 29 }
Chris@0 30
Chris@0 31 public function __toString()
Chris@0 32 {
Chris@0 33 return $this->getContents();
Chris@0 34 }
Chris@0 35
Chris@0 36 public function getContents()
Chris@0 37 {
Chris@0 38 $buffer = $this->buffer;
Chris@0 39 $this->buffer = '';
Chris@0 40
Chris@0 41 return $buffer;
Chris@0 42 }
Chris@0 43
Chris@0 44 public function close()
Chris@0 45 {
Chris@0 46 $this->buffer = '';
Chris@0 47 }
Chris@0 48
Chris@0 49 public function detach()
Chris@0 50 {
Chris@0 51 $this->close();
Chris@0 52 }
Chris@0 53
Chris@0 54 public function getSize()
Chris@0 55 {
Chris@0 56 return strlen($this->buffer);
Chris@0 57 }
Chris@0 58
Chris@0 59 public function isReadable()
Chris@0 60 {
Chris@0 61 return true;
Chris@0 62 }
Chris@0 63
Chris@0 64 public function isWritable()
Chris@0 65 {
Chris@0 66 return true;
Chris@0 67 }
Chris@0 68
Chris@0 69 public function isSeekable()
Chris@0 70 {
Chris@0 71 return false;
Chris@0 72 }
Chris@0 73
Chris@0 74 public function rewind()
Chris@0 75 {
Chris@0 76 $this->seek(0);
Chris@0 77 }
Chris@0 78
Chris@0 79 public function seek($offset, $whence = SEEK_SET)
Chris@0 80 {
Chris@0 81 throw new \RuntimeException('Cannot seek a BufferStream');
Chris@0 82 }
Chris@0 83
Chris@0 84 public function eof()
Chris@0 85 {
Chris@0 86 return strlen($this->buffer) === 0;
Chris@0 87 }
Chris@0 88
Chris@0 89 public function tell()
Chris@0 90 {
Chris@0 91 throw new \RuntimeException('Cannot determine the position of a BufferStream');
Chris@0 92 }
Chris@0 93
Chris@0 94 /**
Chris@0 95 * Reads data from the buffer.
Chris@0 96 */
Chris@0 97 public function read($length)
Chris@0 98 {
Chris@0 99 $currentLength = strlen($this->buffer);
Chris@0 100
Chris@0 101 if ($length >= $currentLength) {
Chris@0 102 // No need to slice the buffer because we don't have enough data.
Chris@0 103 $result = $this->buffer;
Chris@0 104 $this->buffer = '';
Chris@0 105 } else {
Chris@0 106 // Slice up the result to provide a subset of the buffer.
Chris@0 107 $result = substr($this->buffer, 0, $length);
Chris@0 108 $this->buffer = substr($this->buffer, $length);
Chris@0 109 }
Chris@0 110
Chris@0 111 return $result;
Chris@0 112 }
Chris@0 113
Chris@0 114 /**
Chris@0 115 * Writes data to the buffer.
Chris@0 116 */
Chris@0 117 public function write($string)
Chris@0 118 {
Chris@0 119 $this->buffer .= $string;
Chris@0 120
Chris@0 121 // TODO: What should happen here?
Chris@0 122 if (strlen($this->buffer) >= $this->hwm) {
Chris@0 123 return false;
Chris@0 124 }
Chris@0 125
Chris@0 126 return strlen($string);
Chris@0 127 }
Chris@0 128
Chris@0 129 public function getMetadata($key = null)
Chris@0 130 {
Chris@0 131 if ($key == 'hwm') {
Chris@0 132 return $this->hwm;
Chris@0 133 }
Chris@0 134
Chris@0 135 return $key ? null : [];
Chris@0 136 }
Chris@0 137 }