• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

azjezz / psl / 23100454484

15 Mar 2026 01:16AM UTC coverage: 95.628% (-2.8%) from 98.421%
23100454484

Pull #629

github

azjezz
feat(encoding): introduce streaming IO handles for Base64, QuotedPrintable, and Hex

Signed-off-by: azjezz <azjezz@protonmail.com>
Pull Request #629: feat(encoding): introduce streaming IO handles for Base64, QuotedPrintable, and Hex

479 of 797 new or added lines in 13 files covered. (60.1%)

2 existing lines in 1 file now uncovered.

10455 of 10933 relevant lines covered (95.63%)

32.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

70.59
/src/Psl/Encoding/Base64/EncodingWriteHandle.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Psl\Encoding\Base64;
6

7
use Psl\Async\CancellationTokenInterface;
8
use Psl\Async\NullCancellationToken;
9
use Psl\IO;
10

11
use function strlen;
12
use function substr;
13

14
/**
15
 * A write handle that accepts raw binary data, buffers until {@see CHUNK_SIZE} (57) byte chunks
16
 * are available, base64-encodes each chunk, and writes to the inner handle with {@see LINE_ENDING}.
17
 */
18
final class EncodingWriteHandle implements IO\WriteHandleInterface
19
{
20
    use IO\WriteHandleConvenienceMethodsTrait;
21

22
    private string $remainder = '';
23

24
    public function __construct(
25
        private readonly IO\WriteHandleInterface $handle,
26
        private readonly Variant $variant = Variant::Standard,
27
        private readonly bool $padding = true,
28
    ) {}
2✔
29

30
    /**
31
     * {@inheritDoc}
32
     */
33
    public function tryWrite(string $bytes): int
34
    {
35
        $length = strlen($bytes);
2✔
36
        $data = $this->remainder . $bytes;
2✔
37

38
        $data_length = strlen($data);
2✔
39
        while ($data_length >= CHUNK_SIZE) {
2✔
NEW
40
            $chunk = substr($data, 0, CHUNK_SIZE);
×
NEW
41
            $data = substr($data, CHUNK_SIZE);
×
NEW
42
            $data_length -= CHUNK_SIZE;
×
43

NEW
44
            $encoded = encode($chunk, $this->variant, $this->padding) . LINE_ENDING;
×
NEW
45
            $this->handle->writeAll($encoded);
×
46
        }
47

48
        $this->remainder = $data;
2✔
49
        return $length;
2✔
50
    }
51

52
    /**
53
     * {@inheritDoc}
54
     */
55
    public function write(string $bytes, CancellationTokenInterface $cancellation = new NullCancellationToken()): int
56
    {
57
        return $this->tryWrite($bytes);
2✔
58
    }
59

60
    /**
61
     * Flush any remaining buffered raw data through the encoder to the inner handle.
62
     */
63
    public function flush(): void
64
    {
65
        if ($this->remainder !== '') {
2✔
66
            $encoded = encode($this->remainder, $this->variant, $this->padding) . LINE_ENDING;
2✔
67
            $this->remainder = '';
2✔
68
            $this->handle->writeAll($encoded);
2✔
69
        }
70
    }
71
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc