PluginProbe
Media Cloud Sync / 1.3.11
Media Cloud Sync v1.3.11
1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 All 35 releases
media-cloud-sync / includes / sdk / s3 / GuzzleHttp / Psr7 / BufferStream.php

BufferStream.php in Media Cloud Sync 1.3.11, at includes/sdk/s3/GuzzleHttp/Psr7/BufferStream.php

122 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare (strict_types=1);
4 namespace Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7;
5
6 use Dudlewebs\WPMCS\s3\Psr\Http\Message\StreamInterface;
7 /**
8 * Provides a buffer stream that can be written to to fill a buffer, and read
9 * from to remove bytes from the buffer.
10 *
11 * This stream returns a "hwm" metadata value that tells upstream consumers
12 * what the configured high water mark of the stream is, or the maximum
13 * preferred size of the buffer.
14 */
15 final class BufferStream implements StreamInterface
16 {
17 /** @var int */
18 private $hwm;
19 /** @var string */
20 private $buffer = '';
21 /**
22 * @param int $hwm High water mark, representing the preferred maximum
23 * buffer size. If the size of the buffer exceeds the high
24 * water mark, then calls to write will continue to succeed
25 * but will return 0 to inform writers to slow down
26 * until the buffer has been drained by reading from it.
27 */
28 public function __construct(int $hwm = 16384)
29 {
30 $this->hwm = $hwm;
31 }
32 public function __toString() : string
33 {
34 return $this->getContents();
35 }
36 public function getContents() : string
37 {
38 $buffer = $this->buffer;
39 $this->buffer = '';
40 return $buffer;
41 }
42 public function close() : void
43 {
44 $this->buffer = '';
45 }
46 public function detach()
47 {
48 $this->close();
49 return null;
50 }
51 public function getSize() : ?int
52 {
53 return \strlen($this->buffer);
54 }
55 public function isReadable() : bool
56 {
57 return \true;
58 }
59 public function isWritable() : bool
60 {
61 return \true;
62 }
63 public function isSeekable() : bool
64 {
65 return \false;
66 }
67 public function rewind() : void
68 {
69 $this->seek(0);
70 }
71 public function seek($offset, $whence = \SEEK_SET) : void
72 {
73 throw new \RuntimeException('Cannot seek a BufferStream');
74 }
75 public function eof() : bool
76 {
77 return \strlen($this->buffer) === 0;
78 }
79 public function tell() : int
80 {
81 throw new \RuntimeException('Cannot determine the position of a BufferStream');
82 }
83 /**
84 * Reads data from the buffer.
85 */
86 public function read($length) : string
87 {
88 $currentLength = \strlen($this->buffer);
89 if ($length >= $currentLength) {
90 // No need to slice the buffer because we don't have enough data.
91 $result = $this->buffer;
92 $this->buffer = '';
93 } else {
94 // Slice up the result to provide a subset of the buffer.
95 $result = \substr($this->buffer, 0, $length);
96 $this->buffer = \substr($this->buffer, $length);
97 }
98 return $result;
99 }
100 /**
101 * Writes data to the buffer.
102 */
103 public function write($string) : int
104 {
105 $this->buffer .= $string;
106 if (\strlen($this->buffer) >= $this->hwm) {
107 return 0;
108 }
109 return \strlen($string);
110 }
111 /**
112 * @return mixed
113 */
114 public function getMetadata($key = null)
115 {
116 if ($key === 'hwm') {
117 return $this->hwm;
118 }
119 return $key ? null : [];
120 }
121 }
122