Files
wp-multisite-waas/dependencies/amphp/byte-stream/lib/Base64/Base64EncodingOutputStream.php
2024-11-30 18:24:12 -07:00

32 lines
992 B
PHP

<?php
namespace WP_Ultimo\Dependencies\Amp\ByteStream\Base64;
use WP_Ultimo\Dependencies\Amp\ByteStream\OutputStream;
use WP_Ultimo\Dependencies\Amp\Promise;
final class Base64EncodingOutputStream implements OutputStream
{
/** @var OutputStream */
private $destination;
/** @var string */
private $buffer = '';
public function __construct(OutputStream $destination)
{
$this->destination = $destination;
}
public function write(string $data) : Promise
{
$this->buffer .= $data;
$length = \strlen($this->buffer);
$chunk = \base64_encode(\substr($this->buffer, 0, $length - $length % 3));
$this->buffer = \substr($this->buffer, $length - $length % 3);
return $this->destination->write($chunk);
}
public function end(string $finalData = "") : Promise
{
$chunk = \base64_encode($this->buffer . $finalData);
$this->buffer = '';
return $this->destination->end($chunk);
}
}