Core\Compress::decompressor
Core\Compress::decompressor(
Core\Compress::Codec $codec,
uint $maxBytes = 67108864,
uint $maxRatio = 1000,
): Core\Compress\DecompressorStarts a decompression you feed in pieces, instead of all at once.
You get back a Core\Compress\Decompressor. Call add with each piece of the compressed data, as many times as you need, and then call finish once. finish returns the original bytes. Where you split the pieces never changes the result, so a piece may end in the middle of anything.
The result is tainted bytes. The data came from somewhere else, and unpacking it does not make it safe to trust, so Novis keeps track of that for you.
The limit works the same way it does for Core\Compress::decompress, and it is fixed when you open the stream. $maxBytes is the largest result you accept and $maxRatio is the most bytes of result per byte you fed in. finish measures the whole stream against them, not each piece, so ten pieces cannot get ten times the room.
Good to know: a stream ends once. After finish, both add and finish throw a RuntimeError.
Parameters
$codecCore\Compress::CodecThe format the parts are in. The method does not detect the format. Data in another format throws an error.$maxBytesuintdefault 67108864The most output the whole stream produces, in bytes. It is checked against[limits] max_decompressedwhen the stream starts. The limit counts all parts together. It is not a limit for eachadd.$maxRatiouintdefault 1000The most bytes of output for each byte added to the stream. This is the second part of the size limit. It is measured against all the data thataddreceived.
Return value
Returns Core\Compress\Decompressor. A new, open stream with no data in it yet. It has the size limit it is checked against.
Examples
Read a frame in pieces
<?nvs// Shows how to read compressed data that arrives in pieces. You add each piece,// then call `finish` once to get the original bytes.
bytes $frame = Core\Compress::compress(Core\Str::repeat("one row of the export\n", 40), Core\Compress::Codec::Gzip);int $size = Core\Bytes::length($frame) as int;
var $stream = Core\Compress::decompressor(Core\Compress::Codec::Gzip);$stream->add(Core\Bytes::slice($frame, 0, 20));$stream->add(Core\Bytes::slice($frame, 20, $size - 20));
// `finish` returns `tainted bytes`. The data came from somewhere else, and// unpacking it does not make it safe to trust.tainted bytes $rows = $stream->finish();echo 'read ', Core\Bytes::length($rows), " bytes\n";
// The split is not part of the data. The first piece ends in the middle of the// frame's header, and one call over the whole frame reads the same 880 bytes.bytes $atOnce = Core\Compress::decompress($frame, Core\Compress::Codec::Gzip);echo 'one call reads ', Core\Bytes::length($atOnce), " bytes\n";read 880 bytesone call reads 880 bytesThe limit is the whole streams
<?nvs// Shows the limit a stream is opened with. `finish` measures everything you// added against it, so adding the data in more pieces does not buy more room.
bytes $frame = Core\Compress::compress(Core\Str::repeat("<p>the same line</p>\n", 500), Core\Compress::Codec::Zlib);int $size = Core\Bytes::length($frame) as int;
// The result is 10500 bytes, and this stream accepts 20000.var $roomy = Core\Compress::decompressor(Core\Compress::Codec::Zlib, 20000);$roomy->add($frame);echo 'read ', Core\Bytes::length($roomy->finish()), " bytes\n";
// The same frame, added one byte at a time, into a stream that accepts 1000.var $tight = Core\Compress::decompressor(Core\Compress::Codec::Zlib, 1000);int $at = 0;while ($at < $size) { $tight->add(Core\Bytes::slice($frame, $at, 1)); $at = $at + 1;}
try { $tight->finish(); echo "the limit did not hold\n";} catch (ParseError $tooLarge) { echo "1000 bytes was not enough room, however many pieces it arrived in\n";}read 10500 bytes1000 bytes was not enough room, however many pieces it arrived inA download that arrives in parts
<?nvs// Shows what a program does with a compressed download: it adds each part as it// arrives, and says up front how much it is willing to unpack.
// In a real program each of these is a part that just arrived over the network.bytes $whole = Core\Compress::compress(Core\Str::repeat("1042,Berlin,39.90\n", 300), Core\Compress::Codec::Gzip);int $size = Core\Bytes::length($whole) as int;array<bytes> $parts = [ Core\Bytes::slice($whole, 0, 30), Core\Bytes::slice($whole, 30, 30), Core\Bytes::slice($whole, 60, $size - 60),];
// At most one mebibyte of result, and at most 200 bytes of result for each byte// that arrives. Both are fixed now and cannot be raised later.var $download = Core\Compress::decompressor(Core\Compress::Codec::Gzip, 1048576, 200);foreach ($parts as bytes $part) { $download->add($part);}
try { tainted bytes $csv = $download->finish(); echo 'accepted ', Core\Bytes::length($csv), " bytes\n";} catch (ParseError $refused) { echo "the download was refused\n";}accepted 5400 bytes