Initial Commit
This commit is contained in:
37
dependencies/amphp/byte-stream/lib/Base64/Base64EncodingInputStream.php
vendored
Normal file
37
dependencies/amphp/byte-stream/lib/Base64/Base64EncodingInputStream.php
vendored
Normal file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\ByteStream\Base64;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\ByteStream\InputStream;
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
use function WP_Ultimo\Dependencies\Amp\call;
|
||||
final class Base64EncodingInputStream implements InputStream
|
||||
{
|
||||
/** @var InputStream */
|
||||
private $source;
|
||||
/** @var string|null */
|
||||
private $buffer = '';
|
||||
public function __construct(InputStream $source)
|
||||
{
|
||||
$this->source = $source;
|
||||
}
|
||||
public function read() : Promise
|
||||
{
|
||||
return call(function () {
|
||||
$chunk = (yield $this->source->read());
|
||||
if ($chunk === null) {
|
||||
if ($this->buffer === null) {
|
||||
return null;
|
||||
}
|
||||
$chunk = \base64_encode($this->buffer);
|
||||
$this->buffer = null;
|
||||
return $chunk;
|
||||
}
|
||||
$this->buffer .= $chunk;
|
||||
$length = \strlen($this->buffer);
|
||||
$chunk = \base64_encode(\substr($this->buffer, 0, $length - $length % 3));
|
||||
$this->buffer = \substr($this->buffer, $length - $length % 3);
|
||||
return $chunk;
|
||||
});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user