1
0
mirror of https://github.com/danog/byte-stream.git synced 2024-11-30 04:19:23 +01:00
Go to file
2017-05-17 12:45:35 +02:00
docs Add basic documentation 2017-05-17 12:45:35 +02:00
examples Fix code style 2017-05-14 14:41:04 +02:00
lib Add tests, fix ResourceOutputStream writable check 2017-05-16 23:27:48 +02:00
test Remove unused import 2017-05-17 09:39:32 +02:00
.editorconfig Add .editorconfig and fix code style 2017-05-07 22:14:45 +02:00
.gitattributes Initial commit 2016-08-10 16:52:25 -05:00
.gitignore Add tests, fix ResourceOutputStream writable check 2017-05-16 23:27:48 +02:00
.php_cs.dist Rename Gzip*Stream to Zlib*Stream and allow different encodings 2017-05-14 14:40:32 +02:00
.travis.yml Ignore platform requirements for php-cs-fixer 2017-05-07 22:23:08 +02:00
composer.json Update composer.json 2017-05-04 16:30:24 -05:00
LICENSE Update LICENSE year 2017-05-07 22:18:55 +02:00
Makefile Rename Gzip*Stream to Zlib*Stream and allow different encodings 2017-05-14 14:40:32 +02:00
phpunit.xml.dist Add Message tests 2017-04-13 09:05:37 -05:00

Streams

Streams are an abstraction over ordered sequences of bytes. This package provides the fundamental interfaces InputStream and OutputStream.

InputStream

InputStream offers a single method: read(). It returns a promise that gets either resolved with a string or null. null indicates that the stream has ended.

Example

This example shows a simple InputStream consumption that buffers the complete stream contents inside a coroutine.

$inputStream = ...;
$buffer = "";

while (($chunk = yield $inputStream->read()) !== null) {
    $buffer .= $chunk;
}

// do something with $buffer

Note: While buffering a stream that way is extremely straightforward, you might want to use yield new Message($inputStream) to buffer a complete InputStream, making it even easier.

Implementations

This package offers some basic implementations, other libraries might provide even more implementations, such as amphp/socket.

OutputStream

OutputStream offers two methods: write() and end().

write()

write() writes the given string to the stream. The returned Promise might be used to wait for completion. Waiting for completion allows writing only as fast as the underlying stream can write and potentially send over a network. TCP streams will resolve the returned Promise immediately as long as the write buffer isn't full.

The write order is always ensured, even if the writer doesn't wait on the promise.

Tip: Use Amp\Promise\rethrow on the returned Promise if you do not wait on it to get notified about write errors instead of silently doing nothing on errors.

end()

end() marks the stream as ended, optionally writing a last data chunk before. TCP streams might close the underlying stream for writing, but MUST NOT close it. Instead, all resources should be freed and actual resource handles be closed by PHP's garbage collection process.

Example

This example uses the previous example to read from a stream and simply writes all data to an OutputStream.

$inputStream = ...;
$outputStream = ...;
$buffer = "";

while (($chunk = yield $inputStream->read()) !== null) {
    yield $outputStream->write($chunk);
}

yield $outputStream->end();