/
DecodingInputStream.php
65 lines (49 loc) · 1.65 KB
/
DecodingInputStream.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
namespace Kelunik\StreamingBase64;
use Amp\ByteStream\InputStream;
use Amp\Promise;
use function Amp\call;
class DecodingInputStream implements InputStream {
const TYPE_BASE64 = 0;
const TYPE_BASE64URL = 1;
private $source;
private $type;
private $buffer;
public function __construct(InputStream $source, int $type = self::TYPE_BASE64) {
$this->source = $source;
$this->type = $type;
$this->buffer = "";
if ($type < 0 || $type > 1) {
throw new \Error("Invalid type ({$type})");
}
}
/** @inheritdoc */
public function read(): Promise {
return call(function () {
if ($this->buffer === null) {
return null;
}
$data = yield $this->source->read();
if ($data === null) {
$buffer = $this->buffer;
$this->buffer = null;
$this->source = null;
if ($this->type === self::TYPE_BASE64) {
return \base64_decode($buffer);
}
return \base64_decode(\strtr($buffer, "-_", "+/"));
}
$this->buffer .= $data;
$length = \strlen($this->buffer);
$buffer = \substr($this->buffer, 0, $length - $length % 4);
$this->buffer = \substr($this->buffer, $length - $length % 4);
if ($buffer !== "") {
if ($this->type === self::TYPE_BASE64) {
return \base64_decode($buffer);
}
return \base64_decode(\strtr($buffer, "-_", "+/"));
}
return "";
});
}
}