Core\Compress\Decompressor::finish
Closes a decompression stream and returns everything you added to it, decompressed.
You call finish once, after your last add. It joins your pieces in the order you added them, reads them as one block of compressed data, and returns the result as tainted bytes — data that came from outside your program, which Novis tracks so you cannot use it somewhere unsafe by accident.
This is where the data is checked. If the pieces are not a valid block of the format you chose, or if the result would be larger than the size limit the stream was opened with, finish throws a ParseError. It never returns a part of the result.
Good to know: the stream ends here, whether finish returned or threw. Both add and finish throw a RuntimeError on it from now on.
Return value
Returns tainted bytes. The decompressed data as tainted bytes. The result is never cut short. After the call, the stream is finished and its parts are freed. The bytes are tainted because they come from input the program does not control.
Errors
ParseErrorThe parts are not valid data in the stream's format, or the output would go over either limit the stream started with. The limit counts all parts together.RuntimeErrorThe stream is already finished. This is also true after afinishthat threw an error.
Examples
Read the whole download back
<?nvs// Shows what `finish` returns: every piece you added, joined and decompressed// in one block.
bytes $frame = Core\Compress::compress(Core\Str::repeat("order 1042\n", 100), Core\Compress::Codec::Zlib);int $half = (Core\Bytes::length($frame) / 2) as int;
var $reader = Core\Compress::decompressor(Core\Compress::Codec::Zlib);$reader->add(Core\Bytes::slice($frame, 0, $half));$reader->add(Core\Bytes::slice($frame, $half, Core\Bytes::length($frame) as int - $half));
// The result is `tainted bytes`, which means data that came from outside your// program. This prints 1100.tainted bytes $orders = $reader->finish();echo 'the download holds ', Core\Bytes::length($orders), " bytes\n";the download holds 1100 bytesA stream ends once
<?nvs// Shows what happens after a stream is finished. A stream reads one block of// data, and then it is over.
bytes $frame = Core\Compress::compress('the only block', Core\Compress::Codec::Zstd);
var $stream = Core\Compress::decompressor(Core\Compress::Codec::Zstd);$stream->add($frame);echo 'the block holds ', Core\Bytes::length($stream->finish()), " bytes\n";
// `add` throws a `RuntimeError` on a finished stream.try { $stream->add($frame); echo "the finished stream took another piece\n";} catch (RuntimeError $closed) { echo "the finished stream took no more pieces\n";}
// A second `finish` throws one too. Open a new stream for the next download.try { $stream->finish(); echo "the finished stream read a second block\n";} catch (RuntimeError $twice) { echo "the finished stream read no second block\n";}the block holds 14 bytesthe finished stream took no more piecesthe finished stream read no second blockRead an upload inside a try
<?nvs// Shows what a server does with a compressed upload: it sets a size limit when// it opens the stream, and finishes inside a `try`.
bytes $upload = Core\Compress::compress(Core\Str::repeat("name,email\n", 2000), Core\Compress::Codec::Gzip);
// This upload holds 22000 bytes, and the server accepts 8000.var $stream = Core\Compress::decompressor(Core\Compress::Codec::Gzip, 8000);$stream->add($upload);
try { tainted bytes $rows = $stream->finish(); echo 'the upload holds ', Core\Bytes::length($rows), " bytes\n";} catch (ParseError $tooLarge) { echo "the upload is larger than this server accepts\n";}the upload is larger than this server accepts