PluginProbe
Media Cloud Sync / 1.4.2
Media Cloud Sync v1.4.2
1.4.2 1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 All 36 releases
media-cloud-sync / includes / sdk / google / guzzlehttp / psr7 / src / LimitStream.php

LimitStream.php in Media Cloud Sync 1.4.2, at includes/sdk/google/guzzlehttp/psr7/src/LimitStream.php

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