-
Notifications
You must be signed in to change notification settings - Fork 0
topics.streams
[ [doc://topics/io I/O Basics] | [doc://topics/iocollections Collections] | [doc://topics/streams Streams] ]
Input streams are defined by the io.streams.InputStream
interface and provide methods for reading.
$stream= ...;
while ($stream->available()) {
$chunk= $stream->read();
// ...
}
$stream->close();
Input stream implementations are
io.streams.FileInputStream
and
io.streams.MemoryInputStream
.
Input streams are defined by the io.streams.OutputStream
interface and provide methods for writing.
$stream= ...;
$stream->write($chunk1);
$stream->write($chunk2);
$stream->close();
Output stream implementations are
io.streams.MemoryOutputStream
,
io.streams.FileOutputStream
and
io.streams.ConsoleOutputStream
.
Stream aggregates that support buffering are
io.streams.BufferedInputStream
and
io.streams.BufferedInputStream
.
$stream= new BufferedInputStream(new FileInputStream(...));
while ($stream->available()) {
$chunk= $stream->read();
// ...
}
$stream->close();
Streams that supporting seeking implement the io.streams.Seekable
interface.
The io.streams.Streams
class provides static methods
to wrap stream objects into PHP file handles. This way, legacy APIs can
be made to work with streams.
// Reading
$fi= Streams::readableFd(new MemoryInputStream('Hello'));
$hello= fgets($fi, 1024);
fclose($fi);
// Writing
$fd= Streams::writeableFd(new FileOutputStream(new File('out')));
fwrite($fd, 'Contents');
fclose($fd);
The streams API was first introduced in [rfc://0088 RFC 0088 - Streams API] in November 2006.