This is an automated email from the ASF dual-hosted git repository. garydgregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit 774c85b72025c77618d8f4fb01af17f473d82d78 Author: Gary Gregory <[email protected]> AuthorDate: Fri Jul 31 10:30:31 2026 -0400 Sort members --- ...stractLhStaticHuffmanCompressorInputStream.java | 218 ++++++++++----------- 1 file changed, 109 insertions(+), 109 deletions(-) diff --git a/src/main/java/org/apache/commons/compress/compressors/lha/AbstractLhStaticHuffmanCompressorInputStream.java b/src/main/java/org/apache/commons/compress/compressors/lha/AbstractLhStaticHuffmanCompressorInputStream.java index 47339c919..3940f9011 100644 --- a/src/main/java/org/apache/commons/compress/compressors/lha/AbstractLhStaticHuffmanCompressorInputStream.java +++ b/src/main/java/org/apache/commons/compress/compressors/lha/AbstractLhStaticHuffmanCompressorInputStream.java @@ -94,6 +94,55 @@ public void close() throws IOException { } } + /** + * Fill the sliding dictionary with more data. + * + * @throws IOException if an I/O error occurs + */ + private void fillBuffer() throws IOException { + if (this.blockSize == -1) { + // End of stream + return; + } else if (this.blockSize == 0) { + // Start to read the next block + + // Read the block size (number of commands to read) + this.blockSize = (int) bin.readBits(16); + if (this.blockSize == -1) { + // End of stream + return; + } + + final BinaryTree commandDecodingTree = readCommandDecodingTree(); + + this.commandTree = readCommandTree(commandDecodingTree); + + this.distanceTree = readDistanceTree(); + } + + this.blockSize--; + + final int command = commandTree.read(bin); + if (command == -1) { + throw new CompressorException("Unexpected end of stream"); + } else if (command < NUMBER_OF_LITERAL_CODES) { + // Literal command, just write the byte to the buffer + buffer.put(command); + } else { + // Copy command, read the distance and calculate the length from the command + final int distance = readDistance(); + final int length = command - NUMBER_OF_LITERAL_CODES + getCopyThreshold(); + + // Copy the data from the sliding dictionary and add to the buffer + buffer.copy(distance + 1, length); + } + } + + @Override + public long getCompressedCount() { + return bin.getBytesRead(); + } + /** * Gets the threshold for copying data from the sliding dictionary. This is the minimum * possible number of bytes that will be part of a copy command. @@ -127,15 +176,6 @@ int getDictionarySize() { */ abstract int getDistanceBits(); - /** - * Gets the maximum number of distance codes in the distance tree. - * - * @return the maximum number of distance codes - */ - int getMaxNumberOfDistanceCodes() { - return getDictionaryBits() + 1; - } - /** * Gets the maximum match length for the copy command. * @@ -155,9 +195,13 @@ int getMaxNumberOfCommands() { return NUMBER_OF_LITERAL_CODES + getMaxMatchLength() - getCopyThreshold() + 1; } - @Override - public long getCompressedCount() { - return bin.getBytesRead(); + /** + * Gets the maximum number of distance codes in the distance tree. + * + * @return the maximum number of distance codes + */ + int getMaxNumberOfDistanceCodes() { + return getDictionaryBits() + 1; } @Override @@ -180,47 +224,47 @@ public int read() throws IOException { } /** - * Fill the sliding dictionary with more data. + * Read the specified number of bits from the underlying stream throwing CompressorException + * if the end of the stream is reached before reading the requested number of bits. * - * @throws IOException if an I/O error occurs + * @param count the number of bits to read + * @return the bits concatenated as an int using the stream's byte order + * @throws IOException if an I/O error occurs. */ - private void fillBuffer() throws IOException { - if (this.blockSize == -1) { - // End of stream - return; - } else if (this.blockSize == 0) { - // Start to read the next block + private int readBits(final int count) throws IOException { + final long value = bin.readBits(count); + if (value < 0) { + throw new CompressorException("Unexpected end of stream"); + } - // Read the block size (number of commands to read) - this.blockSize = (int) bin.readBits(16); - if (this.blockSize == -1) { - // End of stream - return; - } + return (int) value; + } - final BinaryTree commandDecodingTree = readCommandDecodingTree(); + /** + * Read code length (depth in tree). Usually 0-7 but could be higher and if so, + * count the number of following consecutive one bits and add to the length. + * + * @return code length + * @throws IOException if an I/O error occurs + */ + int readCodeLength() throws IOException { + int len = readBits(CODE_LENGTH_BITS); + if (len == 0x07) { + int bit = bin.readBit(); + while (bit == 1) { + if (++len > MAX_CODE_LENGTH) { + throw new CompressorException("Code length overflow"); + } - this.commandTree = readCommandTree(commandDecodingTree); + bit = bin.readBit(); + } - this.distanceTree = readDistanceTree(); + if (bit == -1) { + throw new CompressorException("Unexpected end of stream"); + } } - this.blockSize--; - - final int command = commandTree.read(bin); - if (command == -1) { - throw new CompressorException("Unexpected end of stream"); - } else if (command < NUMBER_OF_LITERAL_CODES) { - // Literal command, just write the byte to the buffer - buffer.put(command); - } else { - // Copy command, read the distance and calculate the length from the command - final int distance = readDistance(); - final int length = command - NUMBER_OF_LITERAL_CODES + getCopyThreshold(); - - // Copy the data from the sliding dictionary and add to the buffer - buffer.copy(distance + 1, length); - } + return len; } /** @@ -255,33 +299,6 @@ BinaryTree readCommandDecodingTree() throws IOException { } } - /** - * Read code length (depth in tree). Usually 0-7 but could be higher and if so, - * count the number of following consecutive one bits and add to the length. - * - * @return code length - * @throws IOException if an I/O error occurs - */ - int readCodeLength() throws IOException { - int len = readBits(CODE_LENGTH_BITS); - if (len == 0x07) { - int bit = bin.readBit(); - while (bit == 1) { - if (++len > MAX_CODE_LENGTH) { - throw new CompressorException("Code length overflow"); - } - - bit = bin.readBit(); - } - - if (bit == -1) { - throw new CompressorException("Unexpected end of stream"); - } - } - - return len; - } - /** * Read the command tree which is used to decode the commands (literals or copy commands). * @@ -325,32 +342,6 @@ BinaryTree readCommandTree(final BinaryTree commandDecodingTree) throws IOExcept } } - /** - * Read the distance tree which is used to decode the distance of the copy command. - * - * @return the distance tree - * @throws IOException if an I/O error occurs - */ - private BinaryTree readDistanceTree() throws IOException { - // Number of code lengths to read - final int numCodeLengths = readBits(getDistanceBits()); - - if (numCodeLengths > getMaxNumberOfDistanceCodes()) { - throw new CompressorException("Code length table has invalid size (%d > %d)", numCodeLengths, getMaxNumberOfDistanceCodes()); - } else if (numCodeLengths == 0) { - // If numCodeLengths is zero, we read a single code length of getDistanceBits() bits and use as root of the tree - return new BinaryTree(readBits(getDistanceBits())); - } else { - // Read all code lengths - final int[] codeLengths = new int[numCodeLengths]; - for (int index = 0; index < numCodeLengths; index++) { - codeLengths[index] = readCodeLength(); - } - - return new BinaryTree(codeLengths); - } - } - /** * Read the distance by first decoding the number of bits to read from the distance tree * and then reading the actual distance value from the bit input stream. @@ -377,19 +368,28 @@ private int readDistance() throws IOException { } /** - * Read the specified number of bits from the underlying stream throwing CompressorException - * if the end of the stream is reached before reading the requested number of bits. + * Read the distance tree which is used to decode the distance of the copy command. * - * @param count the number of bits to read - * @return the bits concatenated as an int using the stream's byte order - * @throws IOException if an I/O error occurs. + * @return the distance tree + * @throws IOException if an I/O error occurs */ - private int readBits(final int count) throws IOException { - final long value = bin.readBits(count); - if (value < 0) { - throw new CompressorException("Unexpected end of stream"); - } + private BinaryTree readDistanceTree() throws IOException { + // Number of code lengths to read + final int numCodeLengths = readBits(getDistanceBits()); - return (int) value; + if (numCodeLengths > getMaxNumberOfDistanceCodes()) { + throw new CompressorException("Code length table has invalid size (%d > %d)", numCodeLengths, getMaxNumberOfDistanceCodes()); + } else if (numCodeLengths == 0) { + // If numCodeLengths is zero, we read a single code length of getDistanceBits() bits and use as root of the tree + return new BinaryTree(readBits(getDistanceBits())); + } else { + // Read all code lengths + final int[] codeLengths = new int[numCodeLengths]; + for (int index = 0; index < numCodeLengths; index++) { + codeLengths[index] = readCodeLength(); + } + + return new BinaryTree(codeLengths); + } } }
