@@ 9-67 (lines=59) @@ | ||
6 | use React\Stream\Util; |
|
7 | use React\Stream\WritableStreamInterface; |
|
8 | ||
9 | final class WritableStreamBase64Decode implements WritableStreamInterface |
|
10 | { |
|
11 | use EventEmitterTrait; |
|
12 | ||
13 | /** |
|
14 | * @var WritableStreamInterface |
|
15 | */ |
|
16 | private $stream; |
|
17 | ||
18 | /** |
|
19 | * @var string |
|
20 | */ |
|
21 | private $buffer = ''; |
|
22 | ||
23 | /** |
|
24 | * WritableStreamHash constructor. |
|
25 | * @param WritableStreamInterface $stream |
|
26 | */ |
|
27 | public function __construct(WritableStreamInterface $stream) |
|
28 | { |
|
29 | $this->stream = $stream; |
|
30 | Util::forwardEvents($stream, $this, ['error', 'drain', 'end', 'close']); |
|
31 | } |
|
32 | ||
33 | public function isWritable() |
|
34 | { |
|
35 | return $this->stream->isWritable(); |
|
36 | } |
|
37 | ||
38 | public function write($data) |
|
39 | { |
|
40 | $this->buffer .= $data; |
|
41 | ||
42 | return $this->stream->write($this->processBuffer()); |
|
43 | } |
|
44 | ||
45 | public function end($data = null) |
|
46 | { |
|
47 | $this->buffer .= $data; |
|
48 | $this->stream->end( |
|
49 | $this->processBuffer() . base64_decode($this->buffer, true) |
|
50 | ); |
|
51 | $this->buffer = ''; |
|
52 | } |
|
53 | ||
54 | public function close() |
|
55 | { |
|
56 | $this->stream->close(); |
|
57 | } |
|
58 | ||
59 | private function processBuffer(): string |
|
60 | { |
|
61 | $length = strlen($this->buffer); |
|
62 | $buffer = base64_decode(substr($this->buffer, 0, $length - $length % 4), true); |
|
63 | $this->buffer = substr($this->buffer, $length - $length % 4); |
|
64 | ||
65 | return $buffer; |
|
66 | } |
|
67 | } |
|
68 |
@@ 9-67 (lines=59) @@ | ||
6 | use React\Stream\Util; |
|
7 | use React\Stream\WritableStreamInterface; |
|
8 | ||
9 | final class WritableStreamBase64Encode implements WritableStreamInterface |
|
10 | { |
|
11 | use EventEmitterTrait; |
|
12 | ||
13 | /** |
|
14 | * @var WritableStreamInterface |
|
15 | */ |
|
16 | private $stream; |
|
17 | ||
18 | /** |
|
19 | * @var string |
|
20 | */ |
|
21 | private $buffer = ''; |
|
22 | ||
23 | /** |
|
24 | * WritableStreamHash constructor. |
|
25 | * @param WritableStreamInterface $stream |
|
26 | */ |
|
27 | public function __construct(WritableStreamInterface $stream) |
|
28 | { |
|
29 | $this->stream = $stream; |
|
30 | Util::forwardEvents($stream, $this, ['error', 'drain', 'end', 'close']); |
|
31 | } |
|
32 | ||
33 | public function isWritable() |
|
34 | { |
|
35 | return $this->stream->isWritable(); |
|
36 | } |
|
37 | ||
38 | public function write($data) |
|
39 | { |
|
40 | $this->buffer .= $data; |
|
41 | ||
42 | return $this->stream->write($this->processBuffer()); |
|
43 | } |
|
44 | ||
45 | public function end($data = null) |
|
46 | { |
|
47 | $this->buffer .= $data; |
|
48 | $this->stream->end( |
|
49 | $this->processBuffer() . base64_encode($this->buffer) |
|
50 | ); |
|
51 | $this->buffer = ''; |
|
52 | } |
|
53 | ||
54 | public function close() |
|
55 | { |
|
56 | $this->stream->close(); |
|
57 | } |
|
58 | ||
59 | private function processBuffer(): string |
|
60 | { |
|
61 | $length = strlen($this->buffer); |
|
62 | $buffer = base64_encode(substr($this->buffer, 0, $length - $length % 3)); |
|
63 | $this->buffer = substr($this->buffer, $length - $length % 3); |
|
64 | ||
65 | return $buffer; |
|
66 | } |
|
67 | } |
|
68 |