phasync
Streams and files / BufferedStream

Class

BufferedStream

A seekable PSR-7 stream for a body that is still being produced, kept in memory and then in a temporary file.

Introduction#

One coroutine appends with BufferedStream::append and ends with BufferedStream::end. Content is kept in memory up to $bufferSize bytes, and then moved to a temporary file. Nothing is discarded when it has been read, so memory or disk use grows with the content; use UnbufferedStream for a body that is not needed after it was read. getSize() waits until end().

Class synopsis#

class BufferedStream implements StreamInterface, \Stringable {
    /* Methods */
    public BufferedStream::__construct(
        int $bufferSize = 2097152,
        float $deadlockTimeout = 60.0
    )
    public BufferedStream::__toString(): string
    public BufferedStream::close(): void
    public BufferedStream::detach()
    public BufferedStream::getSize(): ?int
    public BufferedStream::tell(): int
    public BufferedStream::eof(): bool
    public BufferedStream::isSeekable(): bool
    public BufferedStream::seek($offset, $whence = SEEK_SET): void
    public BufferedStream::rewind(): void
    public BufferedStream::isWritable(): bool
    public BufferedStream::write($string): int
    public BufferedStream::isReadable(): bool
    public BufferedStream::read($length): string
    public BufferedStream::getContents(): string
    public BufferedStream::getMetadata($key = null)
    public BufferedStream::append(string $chunk): void
    public BufferedStream::end(): void
}

Methods#

MethodDescription
__constructCreates an empty stream.
__toStringReturns the content from the start, or a message starting with "Stream Error" when that fails.
closeWaits for end(), then closes the stream.
detachWaits for end(), then returns the content as a file resource and marks the stream detached.
getSizeReturns the size of the content, once end() has been called: it waits until then.
tellReturns the current position of the file read/write pointer
eofReturns true if the stream is at the end of the stream.
isSeekableReturns whether or not the stream is seekable.
seekSeek to a position in the stream.
rewindSeek to the beginning of the stream.
isWritableReturns whether or not the stream is writable.
writeWrite data to the stream.
isReadableReturns whether or not the stream is readable.
readReturns up to $length bytes, waiting while the content is not yet appended and the stream has not ended.
getContentsWaits for end(), then returns the remaining content.
getMetadataGet stream metadata as an associative array or retrieve a specific key.
appendAdds $chunk to the end of the stream, without waiting for a reader.
endDeclares that nothing more will be appended, which fixes the size of the stream.

Examples#

Example 1
$stream = new BufferedStream();

phasync::go(function () use ($stream) {
    $stream->append('A chunk');
    $stream->end();
});

$size = $stream->getSize();   // waits for end(): 7

See also#

  • UnbufferedStreamA PSR-7 stream for a body that is still being produced, read once and in order.
  • StringStreamA readable, seekable PSR-7 stream over a string that is known in full.