3 * @see https://github.com/zendframework/zend-diactoros for the canonical source repository
4 * @copyright Copyright (c) 2015-2017 Zend Technologies USA Inc. (http://www.zend.com)
5 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
8 namespace Zend\Diactoros
;
10 use Psr\Http\Message\StreamInterface
;
16 * Class RelativeStream
18 * Wrapper for default Stream class, representing subpart (starting from given offset) of initial stream.
19 * It can be used to avoid copying full stream, conserving memory.
20 * @example see Zend\Diactoros\AbstractSerializer::splitStream()
22 final class RelativeStream
implements StreamInterface
25 * @var StreamInterface
27 private $decoratedStream;
37 * @param StreamInterface $decoratedStream
40 public function __construct(StreamInterface
$decoratedStream, $offset)
42 $this->decoratedStream
= $decoratedStream;
43 $this->offset
= (int)$offset;
49 public function __toString()
51 if ($this->isSeekable()) {
54 return $this->getContents();
60 public function close()
62 $this->decoratedStream
->close();
68 public function detach()
70 return $this->decoratedStream
->detach();
76 public function getSize()
78 return $this->decoratedStream
->getSize() - $this->offset
;
84 public function tell()
86 return $this->decoratedStream
->tell() - $this->offset
;
94 return $this->decoratedStream
->eof();
100 public function isSeekable()
102 return $this->decoratedStream
->isSeekable();
108 public function seek($offset, $whence = SEEK_SET
)
110 if ($whence == SEEK_SET
) {
111 return $this->decoratedStream
->seek($offset +
$this->offset
, $whence);
113 return $this->decoratedStream
->seek($offset, $whence);
119 public function rewind()
121 return $this->seek(0);
127 public function isWritable()
129 return $this->decoratedStream
->isWritable();
135 public function write($string)
137 if ($this->tell() < 0) {
138 throw new RuntimeException('Invalid pointer position');
140 return $this->decoratedStream
->write($string);
146 public function isReadable()
148 return $this->decoratedStream
->isReadable();
154 public function read($length)
156 if ($this->tell() < 0) {
157 throw new RuntimeException('Invalid pointer position');
159 return $this->decoratedStream
->read($length);
165 public function getContents()
167 if ($this->tell() < 0) {
168 throw new RuntimeException('Invalid pointer position');
170 return $this->decoratedStream
->getContents();
176 public function getMetadata($key = null)
178 return $this->decoratedStream
->getMetadata($key);