BufferStream.php 3.0 KB

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