encode method

List<int> encode (
  1. List<int> data
)

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.BZH_SIGNATURE);
  bw.writeByte(BZip2.HDR_0 + blockSize100k);

  _nblockMax = 100000 * blockSize100k - 19;
  _workFactor = 30;
  var combinedCRC = 0;

  var n = 100000 * blockSize100k;
  _arr1 = Uint32List(n);
  _arr2 = Uint32List(n + BZ_N_OVERSHOOT);
  _ftab = Uint32List(65537);
  _block = Uint8List.view(_arr2.buffer);
  _mtfv = Uint16List.view(_arr1.buffer);
  _unseqToSeq = Uint8List(256);
  _blockNo = 0;
  _origPtr = 0;

  _selector = Uint8List(BZ_MAX_SELECTORS);
  _selectorMtf = Uint8List(BZ_MAX_SELECTORS);
  _len = List<Uint8List>(BZ_N_GROUPS);
  _code = List<Int32List>(BZ_N_GROUPS);
  _rfreq = List<Int32List>(BZ_N_GROUPS);

  for (var i = 0; i < BZ_N_GROUPS; ++i) {
    _len[i] = Uint8List(BZ_MAX_ALPHA_SIZE);
    _code[i] = Int32List(BZ_MAX_ALPHA_SIZE);
    _rfreq[i] = Int32List(BZ_MAX_ALPHA_SIZE);
  }

  _lenPack = List<Uint32List>(BZ_MAX_ALPHA_SIZE);
  for (var i = 0; i < BZ_MAX_ALPHA_SIZE; ++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.EOS_MAGIC);
  bw.writeUint32(combinedCRC);
  bw.flush();

  return output.getBytes();
}