/** * Reads a value from the specified bit stream. * * @param stream * @return the value decoded, or -1 if the end of the stream is reached */ public int read(final BitStream stream) throws IOException { int currentIndex = 0; while (true) { final int bit = stream.nextBit(); if (bit == -1) { return -1; } final int childIndex = 2 * currentIndex + 1 + bit; final int value = tree[childIndex]; if (value == NODE) { // consume the next bit currentIndex = childIndex; } else if (value != UNDEFINED) { return value; } else { throw new IOException("The child " + bit + " of node at index " + currentIndex + " is not defined"); } } }
/** * Returns the integer value formed by the n next bits (up to 8 bits). * * @param n the number of bits read (up to 8) * @return The value formed by the n bits, or -1 if the end of the stream has been reached */ long nextBits(final int n) throws IOException { return readBits(n); }
/** * @since 1.17 */ @Override public long getCompressedCount() { return bits.getBytesRead() + treeSizes; }
init(); final int bit = bits.nextBit(); if (bit == 1) { literal = literalTree.read(bits); } else { literal = bits.nextByte(); final int distanceLow = (int) bits.nextBits(distanceLowSize); final int distanceHigh = distanceTree.read(bits); if (distanceHigh == -1 && distanceLow <= 0) { length += bits.nextBits(8);
/** * 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); } }
init(); final int bit = bits.nextBit(); if (bit == 1) { literal = literalTree.read(bits); } else { literal = bits.nextByte(); final int distanceLow = (int) bits.nextBits(distanceLowSize); final int distanceHigh = distanceTree.read(bits); if (distanceHigh == -1 && distanceLow <= 0) { length += bits.nextBits(8);
/** * 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); } }
/** * Returns the next bit. * * @return The next bit (0 or 1) or -1 if the end of the stream has been reached */ int nextBit() throws IOException { return (int) readBits(1); }
/** * Reads a value from the specified bit stream. * * @param stream * @return the value decoded, or -1 if the end of the stream is reached */ public int read(final BitStream stream) throws IOException { int currentIndex = 0; while (true) { final int bit = stream.nextBit(); if (bit == -1) { return -1; } final int childIndex = 2 * currentIndex + 1 + bit; final int value = tree[childIndex]; if (value == NODE) { // consume the next bit currentIndex = childIndex; } else if (value != UNDEFINED) { return value; } else { throw new IOException("The child " + bit + " of node at index " + currentIndex + " is not defined"); } } }
int nextByte() throws IOException { return (int) readBits(8); } }
/** * Returns the next bit. * * @return The next bit (0 or 1) or -1 if the end of the stream has been reached */ int nextBit() throws IOException { return (int) readBits(1); }
/** * Returns the integer value formed by the n next bits (up to 8 bits). * * @param n the number of bits read (up to 8) * @return The value formed by the n bits, or -1 if the end of the stream has been reached */ long nextBits(final int n) throws IOException { return readBits(n); }
int nextByte() throws IOException { return (int) readBits(8); } }