File indexing completed on 2025-01-26 05:29:13

0001 <?php
0002 namespace GuzzleHttp\Psr7;
0003 
0004 use Psr\Http\Message\StreamInterface;
0005 
0006 /**
0007  * Stream decorator that begins dropping data once the size of the underlying
0008  * stream becomes too full.
0009  */
0010 class DroppingStream implements StreamInterface
0011 {
0012     use StreamDecoratorTrait;
0013 
0014     private $maxLength;
0015 
0016     /**
0017      * @param StreamInterface $stream    Underlying stream to decorate.
0018      * @param int             $maxLength Maximum size before dropping data.
0019      */
0020     public function __construct(StreamInterface $stream, $maxLength)
0021     {
0022         $this->stream = $stream;
0023         $this->maxLength = $maxLength;
0024     }
0025 
0026     public function write($string)
0027     {
0028         $diff = $this->maxLength - $this->stream->getSize();
0029 
0030         // Begin returning 0 when the underlying stream is too large.
0031         if ($diff <= 0) {
0032             return 0;
0033         }
0034 
0035         // Write the stream or a subset of the stream if needed.
0036         if (strlen($string) < $diff) {
0037             return $this->stream->write($string);
0038         }
0039 
0040         return $this->stream->write(substr($string, 0, $diff));
0041     }
0042 }