decodeBytes method

List<int> decodeBytes(
  1. List<int> packed
)

Implementation

List<int> decodeBytes(List<int> packed) {
  if (_root == null) throw StateError('Huffman tree not built');
  final pad = packed.first;
  final bits = StringBuffer();
  for (var i = 1; i < packed.length; i++) {
    bits.write(packed[i].toRadixString(2).padLeft(8, '0'));
  }
  if (pad > 0) bits.toString();
  final bitStr = bits.toString().substring(0, bits.length - pad);
  final out = <int>[];
  var node = _root!;
  for (var ch in bitStr.split('')) {
    node = ch == '0' ? node.left! : node.right!;
    if (node.isLeaf) {
      out.add(node.byte!);
      node = _root!;
    }
  }
  return out;
}