|
Last change
on this file since 01a620f was 01a620f, checked in by Joachim Langenbach <joachim.langenbach@…>, 11 years ago |
|
Removed submodules for php externals and use composer to provide them and versioned them afterwards
|
-
Property mode
set to
100644
|
|
File size:
1.1 KB
|
| Rev | Line | |
|---|
| [01a620f] | 1 | <?php
|
|---|
| 2 | namespace GuzzleHttp\Psr7;
|
|---|
| 3 |
|
|---|
| 4 | use Psr\Http\Message\StreamInterface;
|
|---|
| 5 |
|
|---|
| 6 | /**
|
|---|
| 7 | * Stream decorator that begins dropping data once the size of the underlying
|
|---|
| 8 | * stream becomes too full.
|
|---|
| 9 | */
|
|---|
| 10 | class DroppingStream implements StreamInterface
|
|---|
| 11 | {
|
|---|
| 12 | use StreamDecoratorTrait;
|
|---|
| 13 |
|
|---|
| 14 | private $maxLength;
|
|---|
| 15 |
|
|---|
| 16 | /**
|
|---|
| 17 | * @param StreamInterface $stream Underlying stream to decorate.
|
|---|
| 18 | * @param int $maxLength Maximum size before dropping data.
|
|---|
| 19 | */
|
|---|
| 20 | public function __construct(StreamInterface $stream, $maxLength)
|
|---|
| 21 | {
|
|---|
| 22 | $this->stream = $stream;
|
|---|
| 23 | $this->maxLength = $maxLength;
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | public function write($string)
|
|---|
| 27 | {
|
|---|
| 28 | $diff = $this->maxLength - $this->stream->getSize();
|
|---|
| 29 |
|
|---|
| 30 | // Begin returning 0 when the underlying stream is too large.
|
|---|
| 31 | if ($diff <= 0) {
|
|---|
| 32 | return 0;
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | // Write the stream or a subset of the stream if needed.
|
|---|
| 36 | if (strlen($string) < $diff) {
|
|---|
| 37 | return $this->stream->write($string);
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | return $this->stream->write(substr($string, 0, $diff));
|
|---|
| 41 | }
|
|---|
| 42 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.