BufferStream.php 3.0 KB

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