/** * Initializes a new instance of that class. * * @param string $path The path of the file. * @param string $mode The mode * * @throws IOException File could not be opened. */ public function __construct($path, $mode = 'r+') { $path = ClrString::asString($path); $res = \fopen((string) $path, ClrString::valueToString($mode, false)); if (false === $res) { $this->throwIOException('Could not open file!'); } $this->_file = $path; parent::__construct($res, true); }
public function testSeek2() { $file = realpath(__DIR__ . DIRECTORY_SEPARATOR . 'test.txt'); $this->assertNotFalse($file); $res = \fopen($file, 'r'); $this->assertTrue(\is_resource($res)); $s = new Stream($res); try { $this->assertTrue($s->canRead()); $this->assertTrue($s->canSeek()); $this->assertFalse($s->canWrite()); $arr = []; $this->assertSame(1, $s->seek(1)); $this->assertSame(2, $s->seek(1, \SEEK_CUR)); while (null !== ($data = $s->read(5))) { $arr[] = (string) $data; } $this->assertEquals(3, count($arr)); $this->assertSame('12345', $arr[0]); $this->assertSame('6789A', $arr[1]); $this->assertSame('BCDEF', $arr[2]); } finally { $s->dispose(); } }
/** * {@inheritDoc} */ public final function writeTo(&$target, int $bufferSize = 1024) : IString { if (null === $target) { throw new ArgumentNullException('target'); } if ($bufferSize < 1) { throw new ArgumentOutOfRangeException($bufferSize, 'bufferSize'); } if ($target instanceof IString) { $target = $target->append($this->_wrappedValue); } else { static::using(function (IStream $src, IStream $dest, int $buffSize) { $src->copyTo($dest, $buffSize); }, new MemoryStream($this->_wrappedValue), Stream::asStream($target), $bufferSize); } return $this; }