Skip to content

Core\Compress\Decompressor::add

$decompressor->add(bytes $data): void$decompressor->add(
    bytes $data,
): void

Adds one piece of compressed data to a stream you opened with Core\Compress::decompressor.

You can call add as many times as you need, and each piece is bytes. Nothing is read yet. The stream keeps the pieces in the order you added them, and finish decompresses all of them together. This is how you read data that arrives in parts, such as a download or a request body.

add never looks inside the piece, so data that is not compressed at all is accepted here and refused by finish. The size limit belongs to the whole stream as well: splitting the data into more pieces does not allow a larger result.

Good to know: a stream ends once. After finish, add throws a RuntimeError, and you open a new stream for the next download.

Parameters

  • $databytesThe next bytes of the compressed data. Nothing is decompressed yet, so this call never throws a size error. finish checks the size limit for the whole stream.

Return value

Returns void. Nothing. The size limit counts the whole stream, not each call.

Errors

  • RuntimeErrorThe stream is already finished. Its data cannot change, so start a new stream.

Examples

Add the blocks of a download

<?nvs
// Shows how to read a compressed download that arrives in blocks. You add each
// block as it comes in, and `finish` reads all of them together.
bytes $report = Core\Str::repeat("order 1042 shipped to Berlin\n", 200) as bytes;
bytes $download = Core\Compress::compress($report, Core\Compress::Codec::Gzip);
int $size = Core\Bytes::length($download) as int;
int $block = 64;
var $reader = Core\Compress::decompressor(Core\Compress::Codec::Gzip);
int $at = 0;
while ($at < $size) {
// The last block is shorter than the others, so take what is left.
int $take = $size - $at;
if ($take > $block) {
$take = $block;
}
$reader->add(Core\Bytes::slice($download, $at, $take));
$at = $at + $take;
}
// The result is `tainted bytes`, which means data that came from outside your
// program. Novis tracks it so you cannot use it somewhere unsafe by accident.
tainted bytes $back = $reader->finish();
echo 'the download was ', $size, " bytes\n";
echo 'the report is ', Core\Bytes::length($back), " bytes\n";
Output
the download was 82 bytes
the report is 5800 bytes

The pieces may be any size

<?nvs
// Shows that the size of the pieces does not matter. The same data added as
// one piece and one byte at a time reads back the same.
bytes $frame = Core\Compress::compress(Core\Str::repeat("<p>a line</p>\n", 100), Core\Compress::Codec::Zlib);
int $size = Core\Bytes::length($frame) as int;
var $wholeFrame = Core\Compress::decompressor(Core\Compress::Codec::Zlib);
$wholeFrame->add($frame);
tainted bytes $atOnce = $wholeFrame->finish();
var $byByte = Core\Compress::decompressor(Core\Compress::Codec::Zlib);
int $at = 0;
while ($at < $size) {
$byByte->add(Core\Bytes::slice($frame, $at, 1));
$at = $at + 1;
}
tainted bytes $inPieces = $byByte->finish();
echo 'one piece gave ', Core\Bytes::length($atOnce), " bytes\n";
echo $size, ' pieces gave ', Core\Bytes::length($inPieces), " bytes\n";
echo 'the same data: ', $atOnce == $inPieces ? 'yes' : 'no', "\n";
Output
one piece gave 1400 bytes
46 pieces gave 1400 bytes
the same data: yes

A download that stopped halfway

<?nvs
// Shows what a program sees when a download breaks off. `add` accepts every
// piece, and `finish` is where the incomplete data is refused.
bytes $frame = Core\Compress::compress(Core\Str::repeat("backup row\n", 200), Core\Compress::Codec::Zstd);
int $half = (Core\Bytes::length($frame) / 2) as int;
var $broken = Core\Compress::decompressor(Core\Compress::Codec::Zstd);
$broken->add(Core\Bytes::slice($frame, 0, $half));
echo "the first half was added\n";
// The stream has half a frame. `finish` throws a `ParseError`, so a broken
// download is something your program answers instead of a wrong result.
try {
$broken->finish();
echo "half a download was read\n";
} catch (ParseError $incomplete) {
echo "half a download could not be read\n";
}
Output
the first half was added
half a download could not be read