Implementation
List<int> encode(List<int> data) {
input = InputStream(data, byteOrder: BIG_ENDIAN);
final output = OutputStream(byteOrder: BIG_ENDIAN);
bw = Bz2BitWriter(output);
final blockSize100k = 9;
bw.writeBytes(BZip2.bzhSignature);
bw.writeByte(BZip2.hdr0 + blockSize100k);
_nblockMax = 100000 * blockSize100k - 19;
_workFactor = 30;
var combinedCRC = 0;
var n = 100000 * blockSize100k;
_arr1 = Uint32List(n);
_arr2 = Uint32List(n + _bzNOvershoot);
_ftab = Uint32List(65537);
_block = Uint8List.view(_arr2.buffer);
_mtfv = Uint16List.view(_arr1.buffer);
_unseqToSeq = Uint8List(256);
_blockNo = 0;
_origPtr = 0;
_selector = Uint8List(_bzMaxSelectors);
_selectorMtf = Uint8List(_bzMaxSelectors);
_len = List<Uint8List>.filled(_bzNGroups, BZip2.emptyUint8List);
_code = List<Int32List>.filled(_bzNGroups, BZip2.emptyInt32List);
_rfreq = List<Int32List>.filled(_bzNGroups, BZip2.emptyInt32List);
for (var i = 0; i < _bzNGroups; ++i) {
_len[i] = Uint8List(_bzMaxAlphaSize);
_code[i] = Int32List(_bzMaxAlphaSize);
_rfreq[i] = Int32List(_bzMaxAlphaSize);
}
_lenPack =
List<Uint32List>.filled(_bzMaxAlphaSize, BZip2.emptyUint32List);
for (var i = 0; i < _bzMaxAlphaSize; ++i) {
_lenPack[i] = Uint32List(4);
}
// Write blocks
while (!input.isEOS) {
var blockCRC = _writeBlock();
combinedCRC = ((combinedCRC << 1) | (combinedCRC >> 31)) & 0xffffffff;
combinedCRC ^= blockCRC;
_blockNo++;
}
bw.writeBytes(BZip2.eosMagic);
bw.writeUint32(combinedCRC);
bw.flush();
return output.getBytes();
}