Final Class Yiisoft\ResponseDownload\ByteRangeStream
| Inheritance | Yiisoft\ResponseDownload\ByteRangeStream |
|---|---|
| Implements | Psr\Http\Message\StreamInterface |
Public Methods
Method Details
| public mixed __construct ( \Psr\Http\Message\StreamInterface $stream, integer $start, integer $end ) | ||
| $stream | \Psr\Http\Message\StreamInterface | |
| $start | integer | |
| $end | integer | |
public function __construct(
private readonly StreamInterface $stream,
private readonly int $start,
int $end,
) {
if ($start < 0 || $end < $start) {
throw new RuntimeException('Invalid byte range.');
}
if (!$stream->isSeekable()) {
throw new RuntimeException('Stream is not seekable.');
}
if (!$stream->isReadable()) {
throw new RuntimeException('Stream is not readable.');
}
$this->size = $end - $start + 1;
$this->stream->seek($start);
}
| public string __toString ( ) |
public function __toString(): string
{
try {
$this->rewind();
return $this->getContents();
} catch (Throwable) {
return '';
}
}
| public boolean eof ( ) |
public function eof(): bool
{
return $this->position >= $this->size || $this->stream->eof();
}
| public string getContents ( ) |
public function getContents(): string
{
$contents = [];
while (!$this->eof()) {
$chunk = $this->read(8192);
if ($chunk === '') {
break;
}
$contents[] = $chunk;
}
return implode('', $contents);
}
| public mixed getMetadata ( ?string $key = null ) | ||
| $key | ?string | |
public function getMetadata(?string $key = null)
{
return $this->stream->getMetadata($key);
}
| public string read ( integer $length ) | ||
| $length | integer | |
public function read(int $length): string
{
if ($length < 0) {
throw new RuntimeException('Length parameter cannot be negative.');
}
if ($length === 0 || $this->eof()) {
return '';
}
$remaining = $this->size - $this->position;
$contents = $this->stream->read(min($length, $remaining));
$this->position += strlen($contents);
return $contents;
}
| public void seek ( integer $offset, integer $whence = SEEK_SET ) | ||
| $offset | integer | |
| $whence | integer | |
public function seek(int $offset, int $whence = SEEK_SET): void
{
$position = match ($whence) {
SEEK_SET => $offset,
SEEK_CUR => $this->position + $offset,
SEEK_END => $this->size + $offset,
default => throw new RuntimeException('Invalid seek mode.'),
};
if ($position < 0 || $position > $this->size) {
throw new RuntimeException('Invalid seek offset.');
}
$this->stream->seek($this->start + $position);
$this->position = $position;
}
Signup or Login in order to comment.