sclass HuffmanByteArray is BitIO { settable byte[] data; gettable int[] histogram; gettable HuffmanTree tree; // options settable bool startWithMagicCode; public void readWrite(BitHead head) { if (head.writeMode()) { histogram = byteHistogramArray(data); new HuffmanTreeMaker tm; var tree = tm.importByteHistogram(histogram); tree = tm!; } // Part 1 (magic code) if (startWithMagicCode) head.exchangeASCII("HUFF"); // Part 2 (Huffman character tree) tree.readWrite(head); // Part 3a (size) head.exchangeInt(-> data.length, size -> data = new byte[size]); // Part 3 (text) if (head.writeMode()) { fOr (b : data) tree.writeCharacter(b, head); //head.trailingBitCount(true); } if (head.readMode()) { for i over data: data[i] = tree.readSymbol(head).literal; } } }