decodeBuffer method Null safety

List<int> decodeBuffer(
  1. InputStreamBase _input,
  2. {bool verify = false,
  3. OutputStreamBase? output}
)

Implementation

List<int> decodeBuffer(InputStreamBase _input,
    {bool verify = false, OutputStreamBase? output}) {
  output ??= OutputStream();
  final br = Bz2BitReader(_input);

  _groupPos = 0;
  _groupNo = 0;
  _gSel = 0;
  _gMinlen = 0;

  if (br.readByte() != BZip2.BZH_SIGNATURE[0] ||
      br.readByte() != BZip2.BZH_SIGNATURE[1] ||
      br.readByte() != BZip2.BZH_SIGNATURE[2]) {
    throw ArchiveException('Invalid Signature');
  }

  _blockSize100k = br.readByte() - BZip2.HDR_0;
  if (_blockSize100k < 0 || _blockSize100k > 9) {
    throw ArchiveException('Invalid BlockSize');
  }

  _tt = Uint32List(_blockSize100k * 100000);

  var combinedCrc = 0;

  while (true) {
    final type = _readBlockType(br);
    if (type == BLOCK_COMPRESSED) {
      var storedBlockCrc = 0;
      storedBlockCrc = (storedBlockCrc << 8) | br.readByte();
      storedBlockCrc = (storedBlockCrc << 8) | br.readByte();
      storedBlockCrc = (storedBlockCrc << 8) | br.readByte();
      storedBlockCrc = (storedBlockCrc << 8) | br.readByte();

      var blockCrc = _readCompressed(br, output);
      blockCrc = BZip2.finalizeCrc(blockCrc);

      if (verify && blockCrc != storedBlockCrc) {
        throw ArchiveException('Invalid block checksum.');
      }
      combinedCrc = ((combinedCrc << 1) | (combinedCrc >> 31)) & 0xffffffff;
      combinedCrc ^= blockCrc;
    } else if (type == BLOCK_EOS) {
      var storedCrc = 0;
      storedCrc = (storedCrc << 8) | br.readByte();
      storedCrc = (storedCrc << 8) | br.readByte();
      storedCrc = (storedCrc << 8) | br.readByte();
      storedCrc = (storedCrc << 8) | br.readByte();

      if (verify && storedCrc != combinedCrc) {
        throw ArchiveException(
            'Invalid combined checksum: ${combinedCrc} : ${storedCrc}');
      }

      if (output is! OutputStream) {
        return [];
      }

      return output.getBytes();
    }
  }
}