Skip to content

Refactor BinaryTree and ExplodingInputStream - #831

Open
fkjellberg wants to merge 1 commit into
apache:masterfrom
fkjellberg:binarytree-refactor
Open

fkjellberg wants to merge 1 commit into
apache:masterfrom
fkjellberg:binarytree-refactor

Conversation

@fkjellberg

Copy link
Copy Markdown
Contributor

Thanks for your contribution to Apache Commons! Your help is appreciated!

Before you push a pull request, review this list:

  • Read the contribution guidelines for this project.
  • Read the ASF Generative Tooling Guidance if you use Artificial Intelligence (AI).
  • I used AI to create any part of, or all of, this pull request. Which AI tool was used to create this pull request, and to what extent did it contribute? I used Claude Opus 5.5 for code analysis, documentation and review.
  • Run a successful build using the default Maven goal with mvn; that's mvn on the command line by itself.
  • Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied. This may not always be possible, but it is a best practice.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Each commit in the pull request should have a meaningful subject line and body. Note that a maintainer may squash commits during the merge process.

This PR separates reading the code lengths from building the tree, to prepare for replacing BinaryTree with the HuffmanDecoder in a follow-up PR.

Changes

  • BinaryTree.decode(InputStream, int) is replaced by BinaryTree.fromCodeLengths(int[]). It builds the tree only from code lengths and checks that each length is in [1, 16].
  • Reading the run-length encoded code lengths moves to ExplodingInputStream.readCodeLengths(...), with readBinaryTree(...) as a convenience wrapper.
  • Variables renamed from "bit length" to "code length", and Javadoc added.

Fixes / behaviour changes

  • Fixed the code length sort loop, which looped over the number of values instead of the possible code lengths.
  • An encoded tree with fewer code lengths than expected (64 or 256) now throws ArchiveException. Before, the missing entries were silently ignored. This aligns with the implementation found in the Info-ZIP UnZip 6.0 utility.

Tests

  • New unit tests for fromCodeLengths (valid, empty and out-of-range lengths) and for readCodeLengths (valid input, empty, truncated, too few and too many values).
  • New end-to-end test for ExplodingInputStream that checks the decompressed output and the InputStreamStatistics counters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant