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