- md5
- 261e8c8dfcf9ebd364a1ecb332b5d670
- sha1
- 264fd5978cc3d96c2a3c0c3f89d35e41e764bf07
- sha256
- fc3463984c1a7539b086273727d87607923613206d9afd485f43437d24bb98e2
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
<?php
declare(strict_types=1);
namespace CBOR;
use InvalidArgumentException;
use RuntimeException;
use function sprintf;
use function strlen;
final class StringStream implements Stream
{
/**
* @var resource
*/
private $resource;
public function __construct(string $data)
{
$resource = fopen('php://memory', 'rb+');
if ($resource === false) {
throw new RuntimeException('Unable to open the memory');
}
$result = fwrite($resource, $data);
if ($result === false) {
throw new RuntimeException('Unable to write the memory');
}
$result = rewind($resource);
if ($result === false) {
throw new RuntimeException('Unable to rewind the memory');
}
$this->resource = $resource;
}
public static function create(string $data): self
{
return new self($data);
}
public function read(int $length): string
{
if ($length === 0) {
return '';
}
$alreadyRead = 0;
$data = '';
while ($alreadyRead < $length) {
$left = $length - $alreadyRead;
$sizeToRead = $left < 1024 && $left > 0 ? $left : 1024;
$newData = fread($this->resource, $sizeToRead);
$alreadyRead += $sizeToRead;
if ($newData === false) {
throw new RuntimeException('Unable to read the memory');
}
if (strlen($newData) < $sizeToRead) {
throw new InvalidArgumentException(sprintf(
'Out of range. Expected: %d, read: %d.',
$length,
strlen($data)
));
}
$data .= $newData;
}
if (strlen($data) !== $length) {
throw new InvalidArgumentException(sprintf(
'Out of range. Expected: %d, read: %d.',
$length,
strlen($data)
));
}
return $data;
}
}
The same content is in 76 releases, Joomla 5.4.0-beta1 to 6.3.0-alpha1. See them all, or every version of this file.