BufferStream.php 3.4 KB

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