mirror of
https://github.com/danog/byte-stream.git
synced 2024-11-26 11:54:54 +01:00
parent
2120bdd46b
commit
d5cd42a765
@ -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.
|
||||
*
|
||||
|
@ -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) {
|
||||
|
@ -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());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user