namespace GuzzleHttp\Psr7;
use Psr\Http\Message\StreamInterface;
* Reads from multiple streams, one after the other.
* This is a read-only stream decorator.
class AppendStream implements StreamInterface
/** @var StreamInterface[] Streams being decorated */
private $seekable = true;
* @param StreamInterface[] $streams Streams to decorate. Each stream must
public function __construct(array $streams = [])
foreach ($streams as $stream) {
$this->addStream($stream);
public function __toString()
return $this->getContents();
} catch (\Exception $e) {
* Add a stream to the AppendStream
* @param StreamInterface $stream Stream to append. Must be readable.
* @throws \InvalidArgumentException if the stream is not readable
public function addStream(StreamInterface $stream)
if (!$stream->isReadable()) {
throw new \InvalidArgumentException('Each stream must be readable');
// The stream is only seekable if all streams are seekable
if (!$stream->isSeekable()) {
$this->streams[] = $stream;
public function getContents()
return Utils::copyToString($this);
* Closes each attached stream.
$this->pos = $this->current = 0;
foreach ($this->streams as $stream) {
* Detaches each attached stream.
* Returns null as it's not clear which underlying stream resource to return.
$this->pos = $this->current = 0;
foreach ($this->streams as $stream) {
* Tries to calculate the size by adding the size of each stream.
* If any of the streams do not return a valid number, then the size of the
* append stream cannot be determined and null is returned.
public function getSize()
foreach ($this->streams as $stream) {
return !$this->streams ||
($this->current >= count($this->streams) - 1 &&
$this->streams[$this->current]->eof());
* Attempts to seek to the given position. Only supports SEEK_SET.
public function seek($offset, $whence = SEEK_SET)
throw new \RuntimeException('This AppendStream is not seekable');
} elseif ($whence !== SEEK_SET) {
throw new \RuntimeException('The AppendStream can only seek with SEEK_SET');
$this->pos = $this->current = 0;
foreach ($this->streams as $i => $stream) {
} catch (\Exception $e) {
throw new \RuntimeException('Unable to seek stream '
. $i . ' of the AppendStream', 0, $e);
// Seek to the actual position by reading from each stream
while ($this->pos < $offset && !$this->eof()) {
$result = $this->read(min(8096, $offset - $this->pos));
* Reads from all of the appended streams until the length is met or EOF.
public function read($length)
$total = count($this->streams) - 1;
// Progress to the next stream if needed.
if ($progressToNext || $this->streams[$this->current]->eof()) {
if ($this->current === $total) {
$result = $this->streams[$this->current]->read($remaining);
// Using a loose comparison here to match on '', false, and null
$remaining = $length - strlen($buffer);
$this->pos += strlen($buffer);
public function isReadable()
public function isWritable()
public function isSeekable()
public function write($string)
throw new \RuntimeException('Cannot write to an AppendStream');
public function getMetadata($key = null)