ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
LimitStream.php
Go to the documentation of this file.
1<?php
2namespace GuzzleHttp\Psr7;
3
5
6
11{
12 use StreamDecoratorTrait;
13
15 private $offset;
16
18 private $limit;
19
27 public function __construct(
29 $limit = -1,
30 $offset = 0
31 ) {
32 $this->stream = $stream;
33 $this->setLimit($limit);
34 $this->setOffset($offset);
35 }
36
37 public function eof()
38 {
39 // Always return true if the underlying stream is EOF
40 if ($this->stream->eof()) {
41 return true;
42 }
43
44 // No limit and the underlying stream is not at EOF
45 if ($this->limit == -1) {
46 return false;
47 }
48
49 return $this->stream->tell() >= $this->offset + $this->limit;
50 }
51
56 public function getSize()
57 {
58 if (null === ($length = $this->stream->getSize())) {
59 return null;
60 } elseif ($this->limit == -1) {
61 return $length - $this->offset;
62 } else {
63 return min($this->limit, $length - $this->offset);
64 }
65 }
66
71 public function seek($offset, $whence = SEEK_SET)
72 {
73 if ($whence !== SEEK_SET || $offset < 0) {
74 throw new \RuntimeException(sprintf(
75 'Cannot seek to offset % with whence %s',
76 $offset,
77 $whence
78 ));
79 }
80
82
83 if ($this->limit !== -1) {
84 if ($offset > $this->offset + $this->limit) {
85 $offset = $this->offset + $this->limit;
86 }
87 }
88
89 $this->stream->seek($offset);
90 }
91
96 public function tell()
97 {
98 return $this->stream->tell() - $this->offset;
99 }
100
108 public function setOffset($offset)
109 {
110 $current = $this->stream->tell();
111
112 if ($current !== $offset) {
113 // If the stream cannot seek to the offset position, then read to it
114 if ($this->stream->isSeekable()) {
115 $this->stream->seek($offset);
116 } elseif ($current > $offset) {
117 throw new \RuntimeException("Could not seek to stream offset $offset");
118 } else {
119 $this->stream->read($offset - $current);
120 }
121 }
122
123 $this->offset = $offset;
124 }
125
133 public function setLimit($limit)
134 {
135 $this->limit = $limit;
136 }
137
138 public function read($length)
139 {
140 if ($this->limit == -1) {
141 return $this->stream->read($length);
142 }
143
144 // Check if the current position is less than the total allowed
145 // bytes + original offset
146 $remaining = ($this->offset + $this->limit) - $this->stream->tell();
147 if ($remaining > 0) {
148 // Only return the amount of requested data, ensuring that the byte
149 // limit is not exceeded
150 return $this->stream->read(min($remaining, $length));
151 }
152
153 return '';
154 }
155}
sprintf('%.4f', $callTime)
An exception for terminatinating execution or to throw for unit testing.
Decorator used to return only a subset of a stream.
Definition: LimitStream.php:11
tell()
Give a relative tell() {Returns the current position of the file read/write pointer....
Definition: LimitStream.php:96
setOffset($offset)
Set the offset to start limiting from.
eof()
Returns true if the stream is at the end of the stream.
Definition: LimitStream.php:37
seek($offset, $whence=SEEK_SET)
Allow for a bounded seek on the read limited stream {Seek to a position in the stream....
Definition: LimitStream.php:71
read($length)
Read data from the stream.
setLimit($limit)
Set the limit of bytes that the decorator allows to be read from the stream.
getSize()
Returns the size of the limited subset of data {Get the size of the stream if known....
Definition: LimitStream.php:56
__construct(StreamInterface $stream, $limit=-1, $offset=0)
Definition: LimitStream.php:27
Describes a data stream.
$stream
PHP stream implementation.
if($state['core:TerminatedAssocId'] !==null) $remaining