| @@ 10-72 (lines=63) @@ | ||
| 7 | use React\Stream\Util; |
|
| 8 | use React\Stream\WritableStreamInterface; |
|
| 9 | ||
| 10 | final class ReadableStreamBase64Decode extends EventEmitter implements ReadableStreamInterface |
|
| 11 | { |
|
| 12 | /** |
|
| 13 | * @var WritableStreamInterface |
|
| 14 | */ |
|
| 15 | private $stream; |
|
| 16 | ||
| 17 | /** |
|
| 18 | * @var string |
|
| 19 | */ |
|
| 20 | private $buffer = ''; |
|
| 21 | ||
| 22 | /** |
|
| 23 | * @param ReadableStreamInterface $stream |
|
| 24 | */ |
|
| 25 | public function __construct(ReadableStreamInterface $stream) |
|
| 26 | { |
|
| 27 | $this->stream = $stream; |
|
| 28 | $this->stream->on('data', function ($data): void { |
|
| 29 | $this->buffer .= $data; |
|
| 30 | $this->emit('data', [$this->processBuffer()]); |
|
| 31 | }); |
|
| 32 | $this->stream->once('end', function (): void { |
|
| 33 | $this->emit('data', [\base64_decode($this->buffer, true)]); |
|
| 34 | $this->emit('end'); |
|
| 35 | }); |
|
| 36 | Util::forwardEvents($stream, $this, ['error', 'close']); |
|
| 37 | } |
|
| 38 | ||
| 39 | public function isReadable() |
|
| 40 | { |
|
| 41 | return $this->stream->isReadable(); |
|
| 42 | } |
|
| 43 | ||
| 44 | public function pause() |
|
| 45 | { |
|
| 46 | return $this->stream->pause(); |
|
| 47 | } |
|
| 48 | ||
| 49 | public function resume() |
|
| 50 | { |
|
| 51 | return $this->stream->resume(); |
|
| 52 | } |
|
| 53 | ||
| 54 | public function pipe(WritableStreamInterface $dest, array $options = []) |
|
| 55 | { |
|
| 56 | return Util::pipe($this, $dest, $options); |
|
| 57 | } |
|
| 58 | ||
| 59 | public function close(): void |
|
| 60 | { |
|
| 61 | $this->stream->close(); |
|
| 62 | } |
|
| 63 | ||
| 64 | private function processBuffer(): string |
|
| 65 | { |
|
| 66 | $length = \strlen($this->buffer); |
|
| 67 | $buffer = \base64_decode(\substr($this->buffer, 0, $length - $length % 4), true); |
|
| 68 | $this->buffer = \substr($this->buffer, $length - $length % 4); |
|
| 69 | ||
| 70 | return $buffer; |
|
| 71 | } |
|
| 72 | } |
|
| 73 | ||
| @@ 10-72 (lines=63) @@ | ||
| 7 | use React\Stream\Util; |
|
| 8 | use React\Stream\WritableStreamInterface; |
|
| 9 | ||
| 10 | final class ReadableStreamBase64Encode extends EventEmitter implements ReadableStreamInterface |
|
| 11 | { |
|
| 12 | /** |
|
| 13 | * @var WritableStreamInterface |
|
| 14 | */ |
|
| 15 | private $stream; |
|
| 16 | ||
| 17 | /** |
|
| 18 | * @var string |
|
| 19 | */ |
|
| 20 | private $buffer = ''; |
|
| 21 | ||
| 22 | /** |
|
| 23 | * @param ReadableStreamInterface $stream |
|
| 24 | */ |
|
| 25 | public function __construct(ReadableStreamInterface $stream) |
|
| 26 | { |
|
| 27 | $this->stream = $stream; |
|
| 28 | $this->stream->on('data', function ($data): void { |
|
| 29 | $this->buffer .= $data; |
|
| 30 | $this->emit('data', [$this->processBuffer()]); |
|
| 31 | }); |
|
| 32 | $this->stream->once('end', function (): void { |
|
| 33 | $this->emit('data', [\base64_encode($this->buffer)]); |
|
| 34 | $this->emit('end'); |
|
| 35 | }); |
|
| 36 | Util::forwardEvents($stream, $this, ['error', 'close']); |
|
| 37 | } |
|
| 38 | ||
| 39 | public function isReadable() |
|
| 40 | { |
|
| 41 | return $this->stream->isReadable(); |
|
| 42 | } |
|
| 43 | ||
| 44 | public function pause() |
|
| 45 | { |
|
| 46 | return $this->stream->pause(); |
|
| 47 | } |
|
| 48 | ||
| 49 | public function resume() |
|
| 50 | { |
|
| 51 | return $this->stream->resume(); |
|
| 52 | } |
|
| 53 | ||
| 54 | public function pipe(WritableStreamInterface $dest, array $options = []) |
|
| 55 | { |
|
| 56 | return Util::pipe($this, $dest, $options); |
|
| 57 | } |
|
| 58 | ||
| 59 | public function close(): void |
|
| 60 | { |
|
| 61 | $this->stream->close(); |
|
| 62 | } |
|
| 63 | ||
| 64 | private function processBuffer(): string |
|
| 65 | { |
|
| 66 | $length = \strlen($this->buffer); |
|
| 67 | $buffer = \base64_encode(\substr($this->buffer, 0, $length - $length % 3)); |
|
| 68 | $this->buffer = \substr($this->buffer, $length - $length % 3); |
|
| 69 | ||
| 70 | return $buffer; |
|
| 71 | } |
|
| 72 | } |
|
| 73 | ||