final BinaryTree tree = new BinaryTree(maxLength); tree.addLeaf(0, Integer.reverse(codes[k] << 16), bitLength, k);
/** * Reads the encoded binary trees and prepares the bit stream. * * @throws IOException */ private void init() throws IOException { if (bits == null) { try (CountingInputStream i = new CountingInputStream(in) { @Override public void close() { // we do not want to close in } }) { if (numberOfTrees == 3) { literalTree = BinaryTree.decode(i, 256); } lengthTree = BinaryTree.decode(i, 64); distanceTree = BinaryTree.decode(i, 64); treeSizes += i.getBytesRead(); } bits = new BitStream(in); } }
/** * Adds a leaf to the tree. * * @param node the index of the node where the path is appended * @param path the path to the leaf (bits are parsed from the right to the left) * @param depth the number of nodes in the path * @param value the value of the leaf (must be positive) */ public void addLeaf(final int node, final int path, final int depth, final int value) { if (depth == 0) { // end of the path reached, add the value to the current node if (tree[node] == UNDEFINED) { tree[node] = value; } else { throw new IllegalArgumentException("Tree value at index " + node + " has already been assigned (" + tree[node] + ")"); } } else { // mark the current node as a non leaf node tree[node] = NODE; // move down the path recursively final int nextChild = 2 * node + 1 + (path & 1); addLeaf(nextChild, path >>> 1, depth - 1, value); } }
/** * Adds a leaf to the tree. * * @param node the index of the node where the path is appended * @param path the path to the leaf (bits are parsed from the right to the left) * @param depth the number of nodes in the path * @param value the value of the leaf (must be positive) */ public void addLeaf(final int node, final int path, final int depth, final int value) { if (depth == 0) { // end of the path reached, add the value to the current node if (tree[node] == UNDEFINED) { tree[node] = value; } else { throw new IllegalArgumentException("Tree value at index " + node + " has already been assigned (" + tree[node] + ")"); } } else { // mark the current node as a non leaf node tree[node] = NODE; // move down the path recursively final int nextChild = 2 * node + 1 + (path & 1); addLeaf(nextChild, path >>> 1, depth - 1, value); } }
final BinaryTree tree = new BinaryTree(maxLength); tree.addLeaf(0, Integer.reverse(codes[k] << 16), bitLength, k);
/** * Reads the encoded binary trees and prepares the bit stream. * * @throws IOException */ private void init() throws IOException { if (bits == null) { if (numberOfTrees == 3) { literalTree = BinaryTree.decode(in, 256); } lengthTree = BinaryTree.decode(in, 64); distanceTree = BinaryTree.decode(in, 64); bits = new BitStream(in); } }