📄 deflaterhuffman.java
字号:
/* DeflaterHuffman.java -- Copyright (C) 2001, 2004, 2005 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package java.util.zip;/** * This is the DeflaterHuffman class. * * This class is <i>not</i> thread safe. This is inherent in the API, due * to the split of deflate and setInput. * * @author Jochen Hoenicke * @date Jan 6, 2000 */class DeflaterHuffman{ private static final int BUFSIZE = 1 << (DeflaterConstants.DEFAULT_MEM_LEVEL + 6); private static final int LITERAL_NUM = 286; private static final int DIST_NUM = 30; private static final int BITLEN_NUM = 19; private static final int REP_3_6 = 16; private static final int REP_3_10 = 17; private static final int REP_11_138 = 18; private static final int EOF_SYMBOL = 256; private static final int[] BL_ORDER = { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 }; private static final String bit4Reverse = "\000\010\004\014\002\012\006\016\001\011\005\015\003\013\007\017"; class Tree { short[] freqs; short[] codes; byte[] length; int[] bl_counts; int minNumCodes, numCodes; int maxLength; Tree(int elems, int minCodes, int maxLength) { this.minNumCodes = minCodes; this.maxLength = maxLength; freqs = new short[elems]; bl_counts = new int[maxLength]; } void reset() { for (int i = 0; i < freqs.length; i++) freqs[i] = 0; codes = null; length = null; } final void writeSymbol(int code) { if (DeflaterConstants.DEBUGGING) { freqs[code]--;// System.err.print("writeSymbol("+freqs.length+","+code+"): "); } pending.writeBits(codes[code] & 0xffff, length[code]); } final void checkEmpty() { boolean empty = true; for (int i = 0; i < freqs.length; i++) if (freqs[i] != 0) { System.err.println("freqs["+i+"] == "+freqs[i]); empty = false; } if (!empty) throw new InternalError(); System.err.println("checkEmpty suceeded!"); } void setStaticCodes(short[] stCodes, byte[] stLength) { codes = stCodes; length = stLength; } public void buildCodes() { int[] nextCode = new int[maxLength]; int code = 0; codes = new short[freqs.length]; if (DeflaterConstants.DEBUGGING) System.err.println("buildCodes: "+freqs.length); for (int bits = 0; bits < maxLength; bits++) { nextCode[bits] = code; code += bl_counts[bits] << (15 - bits); if (DeflaterConstants.DEBUGGING) System.err.println("bits: "+(bits+1)+" count: "+bl_counts[bits] +" nextCode: "+Integer.toHexString(code)); } if (DeflaterConstants.DEBUGGING && code != 65536) throw new RuntimeException("Inconsistent bl_counts!"); for (int i=0; i < numCodes; i++) { int bits = length[i]; if (bits > 0) { if (DeflaterConstants.DEBUGGING) System.err.println("codes["+i+"] = rev(" +Integer.toHexString(nextCode[bits-1])+")," +bits); codes[i] = bitReverse(nextCode[bits-1]); nextCode[bits-1] += 1 << (16 - bits); } } } private void buildLength(int childs[]) { this.length = new byte [freqs.length]; int numNodes = childs.length / 2; int numLeafs = (numNodes + 1) / 2; int overflow = 0; for (int i = 0; i < maxLength; i++) bl_counts[i] = 0; /* First calculate optimal bit lengths */ int lengths[] = new int[numNodes]; lengths[numNodes-1] = 0; for (int i = numNodes - 1; i >= 0; i--) { if (childs[2*i+1] != -1) { int bitLength = lengths[i] + 1; if (bitLength > maxLength) { bitLength = maxLength; overflow++; } lengths[childs[2*i]] = lengths[childs[2*i+1]] = bitLength; } else { /* A leaf node */ int bitLength = lengths[i]; bl_counts[bitLength - 1]++; this.length[childs[2*i]] = (byte) lengths[i]; } } if (DeflaterConstants.DEBUGGING) { System.err.println("Tree "+freqs.length+" lengths:"); for (int i=0; i < numLeafs; i++) System.err.println("Node "+childs[2*i]+" freq: "+freqs[childs[2*i]] + " len: "+length[childs[2*i]]); } if (overflow == 0) return; int incrBitLen = maxLength - 1; do { /* Find the first bit length which could increase: */ while (bl_counts[--incrBitLen] == 0) ; /* Move this node one down and remove a corresponding * amount of overflow nodes. */ do { bl_counts[incrBitLen]--; bl_counts[++incrBitLen]++; overflow -= 1 << (maxLength - 1 - incrBitLen); } while (overflow > 0 && incrBitLen < maxLength - 1); } while (overflow > 0); /* We may have overshot above. Move some nodes from maxLength to * maxLength-1 in that case. */ bl_counts[maxLength-1] += overflow; bl_counts[maxLength-2] -= overflow; /* Now recompute all bit lengths, scanning in increasing * frequency. It is simpler to reconstruct all lengths instead of * fixing only the wrong ones. This idea is taken from 'ar' * written by Haruhiko Okumura. * * The nodes were inserted with decreasing frequency into the childs * array. */ int nodePtr = 2 * numLeafs; for (int bits = maxLength; bits != 0; bits--) { int n = bl_counts[bits-1]; while (n > 0) { int childPtr = 2*childs[nodePtr++]; if (childs[childPtr + 1] == -1) { /* We found another leaf */ length[childs[childPtr]] = (byte) bits; n--; } } } if (DeflaterConstants.DEBUGGING) { System.err.println("*** After overflow elimination. ***"); for (int i=0; i < numLeafs; i++) System.err.println("Node "+childs[2*i]+" freq: "+freqs[childs[2*i]] + " len: "+length[childs[2*i]]); } } void buildTree() { int numSymbols = freqs.length; /* heap is a priority queue, sorted by frequency, least frequent * nodes first. The heap is a binary tree, with the property, that * the parent node is smaller than both child nodes. This assures * that the smallest node is the first parent. * * The binary tree is encoded in an array: 0 is root node and * the nodes 2*n+1, 2*n+2 are the child nodes of node n. */ int[] heap = new int[numSymbols]; int heapLen = 0; int maxCode = 0; for (int n = 0; n < numSymbols; n++) { int freq = freqs[n]; if (freq != 0) { /* Insert n into heap */ int pos = heapLen++; int ppos; while (pos > 0 && freqs[heap[ppos = (pos - 1) / 2]] > freq) { heap[pos] = heap[ppos]; pos = ppos; } heap[pos] = n; maxCode = n; } } /* We could encode a single literal with 0 bits but then we * don't see the literals. Therefore we force at least two * literals to avoid this case. We don't care about order in * this case, both literals get a 1 bit code. */ while (heapLen < 2) { int node = maxCode < 2 ? ++maxCode : 0; heap[heapLen++] = node; } numCodes = Math.max(maxCode + 1, minNumCodes); int numLeafs = heapLen; int[] childs = new int[4*heapLen - 2]; int[] values = new int[2*heapLen - 1]; int numNodes = numLeafs; for (int i = 0; i < heapLen; i++) { int node = heap[i]; childs[2*i] = node; childs[2*i+1] = -1; values[i] = freqs[node] << 8; heap[i] = i; } /* Construct the Huffman tree by repeatedly combining the least two * frequent nodes. */ do { int first = heap[0]; int last = heap[--heapLen]; /* Propagate the hole to the leafs of the heap */ int ppos = 0; int path = 1; while (path < heapLen) { if (path + 1 < heapLen && values[heap[path]] > values[heap[path+1]]) path++; heap[ppos] = heap[path]; ppos = path; path = path * 2 + 1; } /* Now propagate the last element down along path. Normally * it shouldn't go too deep. */ int lastVal = values[last]; while ((path = ppos) > 0 && values[heap[ppos = (path - 1)/2]] > lastVal) heap[path] = heap[ppos]; heap[path] = last; int second = heap[0]; /* Create a new node father of first and second */ last = numNodes++; childs[2*last] = first; childs[2*last+1] = second; int mindepth = Math.min(values[first] & 0xff, values[second] & 0xff); values[last] = lastVal = values[first] + values[second] - mindepth + 1; /* Again, propagate the hole to the leafs */ ppos = 0; path = 1; while (path < heapLen) { if (path + 1 < heapLen && values[heap[path]] > values[heap[path+1]]) path++; heap[ppos] = heap[path]; ppos = path; path = ppos * 2 + 1; } /* Now propagate the new element down along path */ while ((path = ppos) > 0 && values[heap[ppos = (path - 1)/2]] > lastVal) heap[path] = heap[ppos]; heap[path] = last; } while (heapLen > 1); if (heap[0] != childs.length / 2 - 1) throw new RuntimeException("Weird!"); buildLength(childs); } int getEncodedLength() { int len = 0; for (int i = 0; i < freqs.length; i++) len += freqs[i] * length[i]; return len; } void calcBLFreq(Tree blTree) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -