Core\Compress\Compressor::finish
$compressor->finish(): bytesCloses a compression stream and returns the compressed bytes of everything you added to it.
You call finish once, after your last add. It returns one block of bytes: your pieces joined in the order you added them, compressed in the format you chose when you opened the stream. These are the same bytes Core\Compress::compress returns for that data, so the way you split it up never changes the result.
The stream ends here. It frees the pieces it was holding, and both add and finish throw a RuntimeError on it from now on. Open a new stream for the next block of data.
Return value
Returns bytes. The compressed data, as bytes. After the call, the stream is finished and its parts are freed. The result is not tainted, even when the input is. Compressed data is not read as text anywhere.
Errors
RuntimeErrorThe stream is already finished. A secondfinishthrows this error and returns nothing.
Examples
Finish once and get the bytes
<?nvs// Shows what `finish` returns: the compressed bytes of every piece you added,// joined in the order you added them.
var $stream = Core\Compress::compressor(Core\Compress::Codec::Zlib);$stream->add("id,city\n");$stream->add("1042,Berlin\n");$stream->add("1043,Lisbon\n");bytes $frame = $stream->finish();
echo 'the table is ', Core\Bytes::length($frame), " bytes\n";
// These are the same bytes `Core\Compress::compress` returns for the three// pieces joined together.bytes $joined = "id,city\n1042,Berlin\n1043,Lisbon\n" as bytes;bytes $atOnce = Core\Compress::compress($joined, Core\Compress::Codec::Zlib);echo 'the same bytes: ', Core\Hash::equals($frame, $atOnce) ? 'yes' : 'no', "\n";the table is 37 bytesthe same bytes: yesA stream ends once
<?nvs// Shows what happens after a stream is finished. A stream writes one block,// and then it is over.
var $stream = Core\Compress::compressor(Core\Compress::Codec::Zstd);$stream->add('the first and only block');bytes $frame = $stream->finish();echo 'the block is ', Core\Bytes::length($frame), " bytes\n";
// `add` throws a `RuntimeError` on a finished stream.try { $stream->add('too late'); 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 block.try { $stream->finish(); echo "the finished stream wrote a second block\n";} catch (RuntimeError $twice) { echo "the finished stream wrote no second block\n";}the block is 37 bytesthe finished stream took no more piecesthe finished stream wrote no second blockOnly send the answer compressed when it is smaller
<?nvs// Shows what a web server does before it sends a compressed answer: it// finishes the stream and compares the two sizes first.
string $answer = '{"ok":true}';var $stream = Core\Compress::compressor(Core\Compress::Codec::Gzip);$stream->add($answer);bytes $packed = $stream->finish();
uint $plain = Core\Bytes::length($answer as bytes);uint $compressed = Core\Bytes::length($packed);echo 'plain: ', $plain, " bytes\n";echo 'compressed: ', $compressed, " bytes\n";
// A short answer gets bigger when it is compressed, because every format// writes a header of its own. Send this one as it is.echo $compressed < $plain ? "send it compressed\n" : "send it as it is\n";plain: 11 bytescompressed: 31 bytessend it as it is