/** * {@inheritdoc} */ public function seek($offset, $whence = SEEK_SET) { if ($whence == SEEK_SET) { return $this->decoratedStream->seek($offset + $this->offset, $whence); } return $this->decoratedStream->seek($offset, $whence); }
/** * Converts an stream into an string and returns the result. The position of * the pointer will not change if the stream is seekable. Note this copies * the complete content of the stream into the memory * * @param \Psr\Http\Message\StreamInterface $stream * @return string */ public static function toString(StreamInterface $stream) { if (!$stream->isReadable()) { return ''; } if ($stream->isSeekable()) { $pos = $stream->tell(); if ($pos > 0) { $stream->seek(0); } $content = $stream->getContents(); $stream->seek($pos); } else { $content = $stream->getContents(); } return $content; }
public function testReadAndWrite() { $this->assertEquals(0, $this->stream->getSize()); $this->stream->write("Hello World, And All Developers!"); $this->assertEquals(32, $this->stream->getSize()); // size $this->assertEquals(32, $this->stream->tell()); // pointer $this->stream->rewind(); $this->assertEquals(0, $this->stream->tell()); $this->assertFalse($this->stream->eof()); $this->assertEquals("Hell", $this->stream->read(4)); $this->assertEquals("o World, ", $this->stream->read(9)); $this->assertEquals("And All Developers!", $this->stream->getContents()); $this->assertTrue($this->stream->eof()); $this->stream->seek(12); $this->assertEquals(6, $this->stream->write('Hum...')); $this->assertEquals("ll Developers!", $this->stream->getContents()); $this->assertEquals("Hello World,Hum...ll Developers!", $this->stream->__toString()); }
private function cropContent(StreamInterface $stream = null) { if (null === $stream) { return ''; } if ($stream->getSize() <= $this->maxBodySize) { return (string) $stream; } $stream->seek(0); return '(partial content)' . $stream->read($this->maxBodySize) . '(...)'; }
/** * Calculate a hash of a Stream * * @param StreamInterface $stream Stream to calculate the hash for * @param string $algo Hash algorithm (e.g. md5, crc32, etc) * @param bool $rawOutput Whether or not to use raw output * * @return string Returns the hash of the stream * @throws \RuntimeException on error. */ function hash(StreamInterface $stream, $algo, $rawOutput = false) { $pos = $stream->tell(); if ($pos > 0) { $stream->rewind(); } $ctx = hash_init($algo); while (!$stream->eof()) { hash_update($ctx, $stream->read(1048576)); } $out = hash_final($ctx, (bool) $rawOutput); $stream->seek($pos); return $out; }
public function stream_seek($offset, $whence) { $this->stream->seek($offset, $whence); return true; }
/** * {@inheritdoc} */ public function seek($offset, $whence = SEEK_SET) { $this->stream->seek($offset, $whence); }
public function testWrapsWrites() { $this->b->seek(0, SEEK_END); $this->b->write('foo'); $this->assertEquals('foofoo', (string) $this->a); }
/** * addFile_from_Psr7Stream * * dds an open stream to the archive uncompressed * * @param String $name - path of file in archive (including directory). * @param Resource $stream - contents of file as a stream resource * @param array $opt - Hash of options for file (optional, see "File Options" below). * * File Options: * time - Last-modified timestamp (seconds since the epoch) of * this file. Defaults to the current time. * comment - Comment related to this file. * * Examples: * * // create a temporary file stream and write text to it * $fp = tmpfile(); * fwrite($fp, 'The quick brown fox jumped over the lazy dog.'); * * // add a file named 'streamfile.txt' from the content of the stream * $x->addFile_from_stream('streamfile.txt', $fp); * * @return void */ public function addFileFromPsr7Stream($name, \Psr\Http\Message\StreamInterface $stream, $opt = array()) { $name = $this->filterFilename($name); $block_size = 1048576; // process in 1 megabyte chunks $algo = 'crc32b'; $meth = static::METHOD_STORE; // calculate header attributes $stream->seek(0, SEEK_END); $zlen = $len = $stream->tell(); // send file header $hlen = $this->addFileHeader($name, $opt, $meth); // Stream data and calculate CRC32 $stream->rewind(); $hash_ctx = hash_init($algo); while (!$stream->eof()) { $data = $stream->read($block_size); hash_update($hash_ctx, $data); // send data $this->send($data); } $crc = hexdec(hash_final($hash_ctx)); // send file footer + CDR record $this->addFileFooter($name, $opt, $meth, $crc, $zlen, $len, $hlen); }