PluginProbe
Media Cloud Sync / 1.2.13
Media Cloud Sync v1.2.13
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 1.2.9 All 35 releases
media-cloud-sync / includes / sdk / s3 / GuzzleHttp / Psr7 / LimitStream.php

LimitStream.php in Media Cloud Sync 1.2.13, at includes/sdk/s3/GuzzleHttp/Psr7/LimitStream.php

129 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\s3\GuzzleHttp\Psr7;
5
6 use Dudlewebs\WPMCS\s3\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 } else {
54 return \min($this->limit, $length - $this->offset);
55 }
56 }
57 /**
58 * Allow for a bounded seek on the read limited stream
59 */
60 public function seek($offset, $whence = \SEEK_SET) : void
61 {
62 if ($whence !== \SEEK_SET || $offset < 0) {
63 throw new \RuntimeException(\sprintf('Cannot seek to offset %s with whence %s', $offset, $whence));
64 }
65 $offset += $this->offset;
66 if ($this->limit !== -1) {
67 if ($offset > $this->offset + $this->limit) {
68 $offset = $this->offset + $this->limit;
69 }
70 }
71 $this->stream->seek($offset);
72 }
73 /**
74 * Give a relative tell()
75 */
76 public function tell() : int
77 {
78 return $this->stream->tell() - $this->offset;
79 }
80 /**
81 * Set the offset to start limiting from
82 *
83 * @param int $offset Offset to seek to and begin byte limiting from
84 *
85 * @throws \RuntimeException if the stream cannot be seeked.
86 */
87 public function setOffset(int $offset) : void
88 {
89 $current = $this->stream->tell();
90 if ($current !== $offset) {
91 // If the stream cannot seek to the offset position, then read to it
92 if ($this->stream->isSeekable()) {
93 $this->stream->seek($offset);
94 } elseif ($current > $offset) {
95 throw new \RuntimeException("Could not seek to stream offset {$offset}");
96 } else {
97 $this->stream->read($offset - $current);
98 }
99 }
100 $this->offset = $offset;
101 }
102 /**
103 * Set the limit of bytes that the decorator allows to be read from the
104 * stream.
105 *
106 * @param int $limit Number of bytes to allow to be read from the stream.
107 * Use -1 for no limit.
108 */
109 public function setLimit(int $limit) : void
110 {
111 $this->limit = $limit;
112 }
113 public function read($length) : string
114 {
115 if ($this->limit === -1) {
116 return $this->stream->read($length);
117 }
118 // Check if the current position is less than the total allowed
119 // bytes + original offset
120 $remaining = $this->offset + $this->limit - $this->stream->tell();
121 if ($remaining > 0) {
122 // Only return the amount of requested data, ensuring that the byte
123 // limit is not exceeded
124 return $this->stream->read(\min($remaining, $length));
125 }
126 return '';
127 }
128 }
129