BufferStream.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace GuzzleHttp\Stream;
  3. use GuzzleHttp\Stream\Exception\CannotAttachException;
  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. * @package GuzzleHttp\Stream
  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. }
  47. public function attach($stream)
  48. {
  49. throw new CannotAttachException();
  50. }
  51. public function getSize()
  52. {
  53. return strlen($this->buffer);
  54. }
  55. public function isReadable()
  56. {
  57. return true;
  58. }
  59. public function isWritable()
  60. {
  61. return true;
  62. }
  63. public function isSeekable()
  64. {
  65. return false;
  66. }
  67. public function seek($offset, $whence = SEEK_SET)
  68. {
  69. return false;
  70. }
  71. public function eof()
  72. {
  73. return strlen($this->buffer) === 0;
  74. }
  75. public function tell()
  76. {
  77. return false;
  78. }
  79. /**
  80. * Reads data from the buffer.
  81. */
  82. public function read($length)
  83. {
  84. $currentLength = strlen($this->buffer);
  85. if ($length >= $currentLength) {
  86. // No need to slice the buffer because we don't have enough data.
  87. $result = $this->buffer;
  88. $this->buffer = '';
  89. } else {
  90. // Slice up the result to provide a subset of the buffer.
  91. $result = substr($this->buffer, 0, $length);
  92. $this->buffer = substr($this->buffer, $length);
  93. }
  94. return $result;
  95. }
  96. /**
  97. * Writes data to the buffer.
  98. */
  99. public function write($string)
  100. {
  101. $this->buffer .= $string;
  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. }