phasync
Streams and files / StringStream

Class

StringStream

A readable, seekable PSR-7 stream over a string that is known in full.

Introduction#

Never blocks.

Class synopsis#

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

Methods#

MethodDescription
__constructCreates a stream with $contents.
__toStringReturns the whole content, and moves the read position to its end.
closeCloses the stream and any underlying resources.
detachSeparates any underlying resources from the stream.
getSizeGet the size of the stream if known.
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.
readRead data from the stream.
getContentsReturns the remaining contents in a string
getMetadataGet stream metadata as an associative array or retrieve a specific key.

Examples#

Example 1
$stream = new StringStream('Hello');
$stream->read(2);        // "He"
$stream->getSize();      // 5

See also#

  • UnbufferedStreamA PSR-7 stream for a body that is still being produced, read once and in order.
  • BufferedStreamA seekable PSR-7 stream for a body that is still being produced, kept in memory and then in a temporary file.