Kraken\Stream\Stream::read PHP Method

read() public method

public read ( $length = null )
    public function read($length = null)
    {
        if (!$this->readable) {
            return $this->throwAndEmitException(new ReadException('Stream is no longer readable.'));
        }
        if ($length === null) {
            $length = $this->bufferSize;
        }
        $ret = fread($this->resource, $length);
        if ($ret === false) {
            return $this->throwAndEmitException(new ReadException('Cannot read stream.'));
        } else {
            if ($ret !== '') {
                $this->emit('data', [$this, $ret]);
            }
        }
        return $ret;
    }

Usage Example

Beispiel #1
0
 public function testStream_WriteAndReadDataScenario()
 {
     $local = $this->basePath();
     $writer = new Stream(fopen("file://{$local}/temp", 'w+'));
     $reader = new Stream(fopen("file://{$local}/temp", 'r+'));
     $expectedData = "qwertyuiop\n";
     $capturedData = null;
     $readData = null;
     $reader->on('data', function ($origin, $data) use(&$capturedData) {
         $capturedData = $data;
     });
     $reader->on('error', $this->expectCallableNever());
     $reader->on('close', $this->expectCallableOnce());
     $writer->on('drain', $this->expectCallableOnce());
     $writer->on('error', $this->expectCallableNever());
     $writer->on('close', $this->expectCallableOnce());
     $writer->write($expectedData);
     $readData = $reader->read();
     $writer->close();
     $reader->close();
     $this->assertEquals($expectedData, $readData);
     $this->assertEquals($expectedData, $capturedData);
 }