Skip to content

Commit

Permalink
Slightly better header encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
NeRdTheNed committed Aug 19, 2023
1 parent 644fb8d commit 9c85db9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,15 @@ public void recodeHuffman() {
}

final Huffman newLit = new Huffman(new HuffmanTree(litFreq, 15).getTable());
final Huffman newDist = handleZero ? new Huffman(new HuffmanTable(1)) : new Huffman(new HuffmanTree(distFreq, 15).getTable());
// TODO This code isn't good
final boolean handleOne = !handleZero && (MIN_DIST_CODES <= 1) && (Util.checkNonZero(distFreq) <= 1);
final Huffman newDist = (handleZero || handleOne) ? new Huffman(new HuffmanTable(handleZero ? 1 : realLastNonZeroDist)) : new Huffman(new HuffmanTree(distFreq, 15).getTable());

if (handleOne) {
newDist.table.codeLen[realLastNonZeroDist - 1] = 1;
newDist.table.code[realLastNonZeroDist - 1] = 0;
}

recodeToHuffman(newLit, newDist);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,32 @@ public static boolean checkAllZero(byte[] bytes) {
return true;
}

/** Count number of non-zero values in the array */
public static int checkNonZero(byte[] bytes) {
int count = 0;

for (final byte b : bytes) {
if (b != 0) {
count++;
}
}

return count;
}

/** Count number of non-zero values in the array */
public static int checkNonZero(int[] ints) {
int count = 0;

for (final int i : ints) {
if (i != 0) {
count++;
}
}

return count;
}

/** Creates a new array with the contents of both arrays */
public static int[] combine(int[] a1, int[] a2) {
final int[] combined = Arrays.copyOf(a1, a1.length + a2.length);
Expand Down

0 comments on commit 9c85db9

Please sign in to comment.