3 * Zend Framework (http://framework.zend.com/)
5 * @see http://github.com/zendframework/zend-diactoros for the canonical source repository
6 * @copyright Copyright (c) 2015-2016 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
10 namespace Zend\Diactoros;
12 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()
52 return $this->getContents();
58 public function close()
60 $this->decoratedStream->close();
66 public function detach()
68 return $this->decoratedStream->detach();
74 public function getSize()
76 return $this->decoratedStream->getSize() - $this->offset;
82 public function tell()
84 return $this->decoratedStream->tell() - $this->offset;
92 return $this->decoratedStream->eof();
98 public function isSeekable()
100 return $this->decoratedStream->isSeekable();
106 public function seek($offset, $whence = SEEK_SET)
108 if ($whence == SEEK_SET) {
109 return $this->decoratedStream->seek($offset + $this->offset, $whence);
111 return $this->decoratedStream->seek($offset, $whence);
117 public function rewind()
119 return $this->seek(0);
125 public function isWritable()
127 return $this->decoratedStream->isWritable();
133 public function write($string)
135 if ($this->tell() < 0) {
136 throw new RuntimeException('Invalid pointer position');
138 return $this->decoratedStream->write($string);
144 public function isReadable()
146 return $this->decoratedStream->isReadable();
152 public function read($length)
154 if ($this->tell() < 0) {
155 throw new RuntimeException('Invalid pointer position');
157 return $this->decoratedStream->read($length);
163 public function getContents()
165 if ($this->tell() < 0) {
166 throw new RuntimeException('Invalid pointer position');
168 return $this->decoratedStream->getContents();
174 public function getMetadata($key = null)
176 return $this->decoratedStream->getMetadata($key);