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#
| Method | Description |
|---|---|
__construct | Creates an empty stream. |
__toString | Returns the content from the start, or a message starting with "Stream Error" when that fails. |
close | Waits for end(), then closes the stream. |
detach | Waits for end(), then returns the content as a file resource and marks the stream detached. |
getSize | Returns the size of the content, once end() has been called: it waits until then. |
tell | Returns the current position of the file read/write pointer |
eof | Returns true if the stream is at the end of the stream. |
isSeekable | Returns whether or not the stream is seekable. |
seek | Seek to a position in the stream. |
rewind | Seek to the beginning of the stream. |
isWritable | Returns whether or not the stream is writable. |
write | Write data to the stream. |
isReadable | Returns whether or not the stream is readable. |
read | Returns up to $length bytes, waiting while the content is not yet appended and the stream has not ended. |
getContents | Waits for end(), then returns the remaining content. |
getMetadata | Get stream metadata as an associative array or retrieve a specific key. |
append | Adds $chunk to the end of the stream, without waiting for a reader. |
end | Declares that nothing more will be appended, which fixes the size of the stream. |
Examples#
$stream = new BufferedStream();
phasync::go(function () use ($stream) {
$stream->append('A chunk');
$stream->end();
});
$size = $stream->getSize(); // waits for end(): 7See 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.