酒店预订平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

158 lines
4.1 KiB

  1. <?php
  2. namespace GuzzleHttp\Psr7;
  3. use Psr\Http\Message\StreamInterface;
  4. /**
  5. * Decorator used to return only a subset of a stream.
  6. *
  7. * @final
  8. */
  9. class LimitStream implements StreamInterface
  10. {
  11. use StreamDecoratorTrait;
  12. /** @var int Offset to start reading from */
  13. private $offset;
  14. /** @var int Limit the number of bytes that can be read */
  15. private $limit;
  16. /**
  17. * @param StreamInterface $stream Stream to wrap
  18. * @param int $limit Total number of bytes to allow to be read
  19. * from the stream. Pass -1 for no limit.
  20. * @param int $offset Position to seek to before reading (only
  21. * works on seekable streams).
  22. */
  23. public function __construct(
  24. StreamInterface $stream,
  25. $limit = -1,
  26. $offset = 0
  27. ) {
  28. $this->stream = $stream;
  29. $this->setLimit($limit);
  30. $this->setOffset($offset);
  31. }
  32. public function eof()
  33. {
  34. // Always return true if the underlying stream is EOF
  35. if ($this->stream->eof()) {
  36. return true;
  37. }
  38. // No limit and the underlying stream is not at EOF
  39. if ($this->limit == -1) {
  40. return false;
  41. }
  42. return $this->stream->tell() >= $this->offset + $this->limit;
  43. }
  44. /**
  45. * Returns the size of the limited subset of data
  46. * {@inheritdoc}
  47. */
  48. public function getSize()
  49. {
  50. if (null === ($length = $this->stream->getSize())) {
  51. return null;
  52. } elseif ($this->limit == -1) {
  53. return $length - $this->offset;
  54. } else {
  55. return min($this->limit, $length - $this->offset);
  56. }
  57. }
  58. /**
  59. * Allow for a bounded seek on the read limited stream
  60. * {@inheritdoc}
  61. */
  62. public function seek($offset, $whence = SEEK_SET)
  63. {
  64. if ($whence !== SEEK_SET || $offset < 0) {
  65. throw new \RuntimeException(sprintf(
  66. 'Cannot seek to offset %s with whence %s',
  67. $offset,
  68. $whence
  69. ));
  70. }
  71. $offset += $this->offset;
  72. if ($this->limit !== -1) {
  73. if ($offset > $this->offset + $this->limit) {
  74. $offset = $this->offset + $this->limit;
  75. }
  76. }
  77. $this->stream->seek($offset);
  78. }
  79. /**
  80. * Give a relative tell()
  81. * {@inheritdoc}
  82. */
  83. public function tell()
  84. {
  85. return $this->stream->tell() - $this->offset;
  86. }
  87. /**
  88. * Set the offset to start limiting from
  89. *
  90. * @param int $offset Offset to seek to and begin byte limiting from
  91. *
  92. * @throws \RuntimeException if the stream cannot be seeked.
  93. */
  94. public function setOffset($offset)
  95. {
  96. $current = $this->stream->tell();
  97. if ($current !== $offset) {
  98. // If the stream cannot seek to the offset position, then read to it
  99. if ($this->stream->isSeekable()) {
  100. $this->stream->seek($offset);
  101. } elseif ($current > $offset) {
  102. throw new \RuntimeException("Could not seek to stream offset $offset");
  103. } else {
  104. $this->stream->read($offset - $current);
  105. }
  106. }
  107. $this->offset = $offset;
  108. }
  109. /**
  110. * Set the limit of bytes that the decorator allows to be read from the
  111. * stream.
  112. *
  113. * @param int $limit Number of bytes to allow to be read from the stream.
  114. * Use -1 for no limit.
  115. */
  116. public function setLimit($limit)
  117. {
  118. $this->limit = $limit;
  119. }
  120. public function read($length)
  121. {
  122. if ($this->limit == -1) {
  123. return $this->stream->read($length);
  124. }
  125. // Check if the current position is less than the total allowed
  126. // bytes + original offset
  127. $remaining = ($this->offset + $this->limit) - $this->stream->tell();
  128. if ($remaining > 0) {
  129. // Only return the amount of requested data, ensuring that the byte
  130. // limit is not exceeded
  131. return $this->stream->read(min($remaining, $length));
  132. }
  133. return '';
  134. }
  135. }