File indexing completed on 2024-12-29 05:27:23

0001 <?php
0002 namespace GuzzleHttp\Psr7;
0003 
0004 use Psr\Http\Message\StreamInterface;
0005 
0006 /**
0007  * Provides a buffer stream that can be written to to fill a buffer, and read
0008  * from to remove bytes from the buffer.
0009  *
0010  * This stream returns a "hwm" metadata value that tells upstream consumers
0011  * what the configured high water mark of the stream is, or the maximum
0012  * preferred size of the buffer.
0013  */
0014 class BufferStream implements StreamInterface
0015 {
0016     private $hwm;
0017     private $buffer = '';
0018 
0019     /**
0020      * @param int $hwm High water mark, representing the preferred maximum
0021      *                 buffer size. If the size of the buffer exceeds the high
0022      *                 water mark, then calls to write will continue to succeed
0023      *                 but will return false to inform writers to slow down
0024      *                 until the buffer has been drained by reading from it.
0025      */
0026     public function __construct($hwm = 16384)
0027     {
0028         $this->hwm = $hwm;
0029     }
0030 
0031     public function __toString()
0032     {
0033         return $this->getContents();
0034     }
0035 
0036     public function getContents()
0037     {
0038         $buffer = $this->buffer;
0039         $this->buffer = '';
0040 
0041         return $buffer;
0042     }
0043 
0044     public function close()
0045     {
0046         $this->buffer = '';
0047     }
0048 
0049     public function detach()
0050     {
0051         $this->close();
0052     }
0053 
0054     public function getSize()
0055     {
0056         return strlen($this->buffer);
0057     }
0058 
0059     public function isReadable()
0060     {
0061         return true;
0062     }
0063 
0064     public function isWritable()
0065     {
0066         return true;
0067     }
0068 
0069     public function isSeekable()
0070     {
0071         return false;
0072     }
0073 
0074     public function rewind()
0075     {
0076         $this->seek(0);
0077     }
0078 
0079     public function seek($offset, $whence = SEEK_SET)
0080     {
0081         throw new \RuntimeException('Cannot seek a BufferStream');
0082     }
0083 
0084     public function eof()
0085     {
0086         return strlen($this->buffer) === 0;
0087     }
0088 
0089     public function tell()
0090     {
0091         throw new \RuntimeException('Cannot determine the position of a BufferStream');
0092     }
0093 
0094     /**
0095      * Reads data from the buffer.
0096      */
0097     public function read($length)
0098     {
0099         $currentLength = strlen($this->buffer);
0100 
0101         if ($length >= $currentLength) {
0102             // No need to slice the buffer because we don't have enough data.
0103             $result = $this->buffer;
0104             $this->buffer = '';
0105         } else {
0106             // Slice up the result to provide a subset of the buffer.
0107             $result = substr($this->buffer, 0, $length);
0108             $this->buffer = substr($this->buffer, $length);
0109         }
0110 
0111         return $result;
0112     }
0113 
0114     /**
0115      * Writes data to the buffer.
0116      */
0117     public function write($string)
0118     {
0119         $this->buffer .= $string;
0120 
0121         // TODO: What should happen here?
0122         if (strlen($this->buffer) >= $this->hwm) {
0123             return false;
0124         }
0125 
0126         return strlen($string);
0127     }
0128 
0129     public function getMetadata($key = null)
0130     {
0131         if ($key == 'hwm') {
0132             return $this->hwm;
0133         }
0134 
0135         return $key ? null : [];
0136     }
0137 }