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

50 lines
1.2 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;
/**
* Stream decorator that begins dropping data once the size of the underlying
* stream becomes too full.
*/
2025-03-24 14:59:19 +08:00
final class DroppingStream implements StreamInterface
2025-03-12 12:21:57 +08:00
{
use StreamDecoratorTrait;
2025-03-24 14:59:19 +08:00
/** @var int */
2025-03-12 12:21:57 +08:00
private $maxLength;
2025-03-24 14:59:19 +08:00
/** @var StreamInterface */
private $stream;
2025-03-12 12:21:57 +08:00
/**
* @param StreamInterface $stream Underlying stream to decorate.
* @param int $maxLength Maximum size before dropping data.
*/
2025-03-24 14:59:19 +08:00
public function __construct(StreamInterface $stream, int $maxLength)
2025-03-12 12:21:57 +08:00
{
$this->stream = $stream;
$this->maxLength = $maxLength;
}
2025-03-24 14:59:19 +08:00
public function write($string): int
2025-03-12 12:21:57 +08:00
{
$diff = $this->maxLength - $this->stream->getSize();
// Begin returning 0 when the underlying stream is too large.
if ($diff <= 0) {
return 0;
}
// Write the stream or a subset of the stream if needed.
if (strlen($string) < $diff) {
return $this->stream->write($string);
}
return $this->stream->write(substr($string, 0, $diff));
}
}