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