2 namespace GuzzleHttp\Psr7;
4 use Psr\Http\Message\StreamInterface;
7 * Stream decorator that begins dropping data once the size of the underlying
8 * stream becomes too full.
10 class DroppingStream implements StreamInterface
12 use StreamDecoratorTrait;
17 * @param StreamInterface $stream Underlying stream to decorate.
18 * @param int $maxLength Maximum size before dropping data.
20 public function __construct(StreamInterface $stream, $maxLength)
22 $this->stream = $stream;
23 $this->maxLength = $maxLength;
26 public function write($string)
28 $diff = $this->maxLength - $this->stream->getSize();
30 // Begin returning 0 when the underlying stream is too large.
35 // Write the stream or a subset of the stream if needed.
36 if (strlen($string) < $diff) {
37 return $this->stream->write($string);
40 return $this->stream->write(substr($string, 0, $diff));