1
0
mirror of https://github.com/danog/byte-stream.git synced 2024-11-26 11:54:54 +01:00

Add chunksize setter to Resource*Stream classes (#55)

Fixes #50.
This commit is contained in:
Gabriel Ostrolucký 2019-03-31 16:51:36 +02:00 committed by Niklas Keller
parent 2120bdd46b
commit d5cd42a765
3 changed files with 29 additions and 4 deletions

View File

@ -62,7 +62,7 @@ final class ResourceInputStream implements InputStream
\stream_set_read_buffer($stream, 0);
$this->resource = $stream;
$this->chunkSize = $chunkSize;
$this->chunkSize = &$chunkSize;
$deferred = &$this->deferred;
$readable = &$this->readable;
@ -70,7 +70,7 @@ final class ResourceInputStream implements InputStream
$this->watcher = Loop::onReadable($this->resource, static function ($watcher, $stream) use (
&$deferred,
&$readable,
$chunkSize,
&$chunkSize,
$useSingleRead
) {
if ($useSingleRead) {
@ -204,6 +204,11 @@ final class ResourceInputStream implements InputStream
return $this->resource;
}
public function setChunkSize(int $chunkSize)
{
$this->chunkSize = $chunkSize;
}
/**
* References the read watcher, so the loop keeps running in case there's an active read.
*

View File

@ -51,13 +51,13 @@ final class ResourceOutputStream implements OutputStream
\stream_set_write_buffer($stream, 0);
$this->resource = $stream;
$this->chunkSize = $chunkSize;
$this->chunkSize = &$chunkSize;
$writes = $this->writes = new \SplQueue;
$writable = &$this->writable;
$resource = &$this->resource;
$this->watcher = Loop::onWritable($stream, static function ($watcher, $stream) use ($writes, $chunkSize, &$writable, &$resource) {
$this->watcher = Loop::onWritable($stream, static function ($watcher, $stream) use ($writes, &$chunkSize, &$writable, &$resource) {
static $emptyWrites = 0;
try {
@ -274,6 +274,11 @@ final class ResourceOutputStream implements OutputStream
return $this->resource;
}
public function setChunkSize(int $chunkSize)
{
$this->chunkSize = $chunkSize;
}
public function __destruct()
{
if ($this->resource !== null) {

View File

@ -274,4 +274,19 @@ class ResourceStreamTest extends TestCase
$this->assertStringEqualsFile(__FILE__, $buffer);
});
}
public function testSetChunkSize()
{
Loop::run(function () {
list($a, $b) = $this->getStreamPair();
$a->setChunkSize(1);
$b->setChunkSize(1);
$this->assertSame(3, yield $a->write('foo'));
$this->assertSame('f', yield $b->read());
$b->setChunkSize(3);
$this->assertSame('oo', yield $b->read());
});
}
}