Files
cunkebao_v3/Server/vendor/guzzlehttp/psr7/src/BufferStream.php

148 lines
3.1 KiB
PHP
Raw Normal View History

2025-03-12 12:21:57 +08:00
<?php
2025-03-24 14:59:19 +08:00
declare(strict_types=1);
2025-03-12 12:21:57 +08:00
namespace GuzzleHttp\Psr7;
use Psr\Http\Message\StreamInterface;
/**
* Provides a buffer stream that can be written to to fill a buffer, and read
* from to remove bytes from the buffer.
*
* This stream returns a "hwm" metadata value that tells upstream consumers
* what the configured high water mark of the stream is, or the maximum
* preferred size of the buffer.
*/
2025-03-24 14:59:19 +08:00
final class BufferStream implements StreamInterface
2025-03-12 12:21:57 +08:00
{
2025-03-24 14:59:19 +08:00
/** @var int */
2025-03-12 12:21:57 +08:00
private $hwm;
2025-03-24 14:59:19 +08:00
/** @var string */
2025-03-12 12:21:57 +08:00
private $buffer = '';
/**
* @param int $hwm High water mark, representing the preferred maximum
* buffer size. If the size of the buffer exceeds the high
* water mark, then calls to write will continue to succeed
2025-03-24 14:59:19 +08:00
* but will return 0 to inform writers to slow down
2025-03-12 12:21:57 +08:00
* until the buffer has been drained by reading from it.
*/
2025-03-24 14:59:19 +08:00
public function __construct(int $hwm = 16384)
2025-03-12 12:21:57 +08:00
{
$this->hwm = $hwm;
}
2025-03-24 14:59:19 +08:00
public function __toString(): string
2025-03-12 12:21:57 +08:00
{
return $this->getContents();
}
2025-03-24 14:59:19 +08:00
public function getContents(): string
2025-03-12 12:21:57 +08:00
{
$buffer = $this->buffer;
$this->buffer = '';
return $buffer;
}
2025-03-24 14:59:19 +08:00
public function close(): void
2025-03-12 12:21:57 +08:00
{
$this->buffer = '';
}
public function detach()
{
$this->close();
return null;
}
2025-03-24 14:59:19 +08:00
public function getSize(): ?int
2025-03-12 12:21:57 +08:00
{
return strlen($this->buffer);
}
2025-03-24 14:59:19 +08:00
public function isReadable(): bool
2025-03-12 12:21:57 +08:00
{
return true;
}
2025-03-24 14:59:19 +08:00
public function isWritable(): bool
2025-03-12 12:21:57 +08:00
{
return true;
}
2025-03-24 14:59:19 +08:00
public function isSeekable(): bool
2025-03-12 12:21:57 +08:00
{
return false;
}
2025-03-24 14:59:19 +08:00
public function rewind(): void
2025-03-12 12:21:57 +08:00
{
$this->seek(0);
}
2025-03-24 14:59:19 +08:00
public function seek($offset, $whence = SEEK_SET): void
2025-03-12 12:21:57 +08:00
{
throw new \RuntimeException('Cannot seek a BufferStream');
}
2025-03-24 14:59:19 +08:00
public function eof(): bool
2025-03-12 12:21:57 +08:00
{
return strlen($this->buffer) === 0;
}
2025-03-24 14:59:19 +08:00
public function tell(): int
2025-03-12 12:21:57 +08:00
{
throw new \RuntimeException('Cannot determine the position of a BufferStream');
}
/**
* Reads data from the buffer.
*/
2025-03-24 14:59:19 +08:00
public function read($length): string
2025-03-12 12:21:57 +08:00
{
$currentLength = strlen($this->buffer);
if ($length >= $currentLength) {
// No need to slice the buffer because we don't have enough data.
$result = $this->buffer;
$this->buffer = '';
} else {
// Slice up the result to provide a subset of the buffer.
$result = substr($this->buffer, 0, $length);
$this->buffer = substr($this->buffer, $length);
}
return $result;
}
/**
* Writes data to the buffer.
*/
2025-03-24 14:59:19 +08:00
public function write($string): int
2025-03-12 12:21:57 +08:00
{
$this->buffer .= $string;
if (strlen($this->buffer) >= $this->hwm) {
2025-03-24 14:59:19 +08:00
return 0;
2025-03-12 12:21:57 +08:00
}
return strlen($string);
}
2025-03-24 14:59:19 +08:00
/**
* @return mixed
*/
2025-03-12 12:21:57 +08:00
public function getMetadata($key = null)
{
2025-03-24 14:59:19 +08:00
if ($key === 'hwm') {
2025-03-12 12:21:57 +08:00
return $this->hwm;
}
return $key ? null : [];
}
}