Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
LimitStream.php
001 <?php
002
003 namespace GuzzleHttp\Psr7;
004
005 use Psr\Http\Message\StreamInterface;
006
007 /**
008 * Decorator used to return only a subset of a stream.
009 *
010 * @final
011 */
012 class LimitStream implements StreamInterface
013 {
014 use StreamDecoratorTrait;
015
016 /** @var int Offset to start reading from */
017 private $offset;
018
019 /** @var int Limit the number of bytes that can be read */
020 private $limit;
021
022 /**
023 * @param StreamInterface $stream Stream to wrap
024 * @param int $limit Total number of bytes to allow to be read
025 * from the stream. Pass -1 for no limit.
026 * @param int $offset Position to seek to before reading (only
027 * works on seekable streams).
028 */
029 public function __construct(
030 StreamInterface $stream,
031 $limit = -1,
032 $offset = 0
033 ) {
034 $this->stream = $stream;
035 $this->setLimit($limit);
036 $this->setOffset($offset);
037 }
038
039 public function eof()
040 {
041 // Always return true if the underlying stream is EOF
042 if ($this->stream->eof()) {
043 return true;
044 }
045
046 // No limit and the underlying stream is not at EOF
047 if ($this->limit == -1) {
048 return false;
049 }
050
051 return $this->stream->tell() >= $this->offset + $this->limit;
052 }
053
054 /**
055 * Returns the size of the limited subset of data
056 * {@inheritdoc}
057 */
058 public function getSize()
059 {
060 if (null === ($length = $this->stream->getSize())) {
061 return null;
062 } elseif ($this->limit == -1) {
063 return $length - $this->offset;
064 } else {
065 return min($this->limit, $length - $this->offset);
066 }
067 }
068
069 /**
070 * Allow for a bounded seek on the read limited stream
071 * {@inheritdoc}
072 */
073 public function seek($offset, $whence = SEEK_SET)
074 {
075 if ($whence !== SEEK_SET || $offset < 0) {
076 throw new \RuntimeException(sprintf(
077 'Cannot seek to offset %s with whence %s',
078 $offset,
079 $whence
080 ));
081 }
082
083 $offset += $this->offset;
084
085 if ($this->limit !== -1) {
086 if ($offset > $this->offset + $this->limit) {
087 $offset = $this->offset + $this->limit;
088 }
089 }
090
091 $this->stream->seek($offset);
092 }
093
094 /**
095 * Give a relative tell()
096 * {@inheritdoc}
097 */
098 public function tell()
099 {
100 return $this->stream->tell() - $this->offset;
101 }
102
103 /**
104 * Set the offset to start limiting from
105 *
106 * @param int $offset Offset to seek to and begin byte limiting from
107 *
108 * @throws \RuntimeException if the stream cannot be seeked.
109 */
110 public function setOffset($offset)
111 {
112 $current = $this->stream->tell();
113
114 if ($current !== $offset) {
115 // If the stream cannot seek to the offset position, then read to it
116 if ($this->stream->isSeekable()) {
117 $this->stream->seek($offset);
118 } elseif ($current > $offset) {
119 throw new \RuntimeException("Could not seek to stream offset $offset");
120 } else {
121 $this->stream->read($offset - $current);
122 }
123 }
124
125 $this->offset = $offset;
126 }
127
128 /**
129 * Set the limit of bytes that the decorator allows to be read from the
130 * stream.
131 *
132 * @param int $limit Number of bytes to allow to be read from the stream.
133 * Use -1 for no limit.
134 */
135 public function setLimit($limit)
136 {
137 $this->limit = $limit;
138 }
139
140 public function read($length)
141 {
142 if ($this->limit == -1) {
143 return $this->stream->read($length);
144 }
145
146 // Check if the current position is less than the total allowed
147 // bytes + original offset
148 $remaining = ($this->offset + $this->limit) - $this->stream->tell();
149 if ($remaining > 0) {
150 // Only return the amount of requested data, ensuring that the byte
151 // limit is not exceeded
152 return $this->stream->read(min($remaining, $length));
153 }
154
155 return '';
156 }
157 }
158