CoreFiles by mySites.guru
StringStream.php61 lines, 1,300 bytes, from Joomla_5.4.11-Stable-Full_Package.zip
Raw History Compare with 5.4.10
md5
1f3000d8391825f0f8cd2e714e662cd0
sha1
703c2768fd0eac7d5efa63a0e8561a70813bd486
sha256
a949dc78a90fe6c0ac5c4f3144169f108b0d581d28b1828cfc9482ef9971d6d6
<?php

declare(strict_types=1);

namespace Webauthn;

use CBOR\Stream;
use function fclose;
use function fopen;
use function fread;
use function fwrite;
use function rewind;
use Webauthn\Exception\InvalidDataException;

final class StringStream implements Stream
{
    /**
     * @var resource
     */
    private $data;

    private readonly int $length;

    private int $totalRead = 0;

    public function __construct(string $data)
    {
        $this->length = mb_strlen($data, '8bit');
        $resource = fopen('php://memory', 'rb+');
        fwrite($resource, $data);
        rewind($resource);
        $this->data = $resource;
    }

    public function read(int $length): string
    {
        if ($length <= 0) {
            return '';
        }
        $read = fread($this->data, $length);
        $bytesRead = mb_strlen($read, '8bit');
        mb_strlen($read, '8bit') === $length || throw InvalidDataException::create(null, sprintf(
            'Out of range. Expected: %d, read: %d.',
            $length,
            $bytesRead
        ));
        $this->totalRead += $bytesRead;

        return $read;
    }

    public function close(): void
    {
        fclose($this->data);
    }

    public function isEOF(): bool
    {
        return $this->totalRead === $this->length;
    }
}

The same content is in 157 releases, Joomla 5.0.0-alpha1 to 6.2.1. See them all, or every version of this file.