BufferStream.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. }
  45. public function getSize()
  46. {
  47. return strlen($this->buffer);
  48. }
  49. public function isReadable()
  50. {
  51. return true;
  52. }
  53. public function isWritable()
  54. {
  55. return true;
  56. }
  57. public function isSeekable()
  58. {
  59. return false;
  60. }
  61. public function rewind()
  62. {
  63. $this->seek(0);
  64. }
  65. public function seek($offset, $whence = SEEK_SET)
  66. {
  67. throw new \RuntimeException('Cannot seek a BufferStream');
  68. }
  69. public function eof()
  70. {
  71. return strlen($this->buffer) === 0;
  72. }
  73. public function tell()
  74. {
  75. throw new \RuntimeException('Cannot determine the position of a BufferStream');
  76. }
  77. /**
  78. * Reads data from the buffer.
  79. */
  80. public function read($length)
  81. {
  82. $currentLength = strlen($this->buffer);
  83. if ($length >= $currentLength) {
  84. // No need to slice the buffer because we don't have enough data.
  85. $result = $this->buffer;
  86. $this->buffer = '';
  87. } else {
  88. // Slice up the result to provide a subset of the buffer.
  89. $result = substr($this->buffer, 0, $length);
  90. $this->buffer = substr($this->buffer, $length);
  91. }
  92. return $result;
  93. }
  94. /**
  95. * Writes data to the buffer.
  96. */
  97. public function write($string)
  98. {
  99. $this->buffer .= $string;
  100. // TODO: What should happen here?
  101. if (strlen($this->buffer) >= $this->hwm) {
  102. return false;
  103. }
  104. return strlen($string);
  105. }
  106. public function getMetadata($key = null)
  107. {
  108. if ($key == 'hwm') {
  109. return $this->hwm;
  110. }
  111. return $key ? null : [];
  112. }
  113. }