public class Huffman
extends java.lang.Object
implements java.io.Serializable
Modifier and Type | Class and Description |
---|---|
(package private) class |
Huffman.Node
Inner class for the node of Huffman tree.
|
Modifier and Type | Field and Description |
---|---|
(package private) Huffman.Node[] |
leaves
Coding table represented as an array of leaves of the code tree
|
(package private) Huffman.Node |
root
Root of the code tree
|
static long |
serialVersionUID
serialization version id
|
Constructor and Description |
---|
Huffman(byte[] original)
Constructor to build the Huffman code for a given bytearray.
|
Modifier and Type | Method and Description |
---|---|
(package private) static byte[] |
addOne(int len,
byte[] code)
Append bit 1 to the code (and increase the bitlength by 1).
|
(package private) static byte[] |
addZero(int len,
byte[] code)
Append zero bit to the code (and increase the bitlength by 1).
|
int |
bitLength()
Length of encoded data in bits.
|
byte[] |
decode(byte[] encodedData)
Decoding the byte array using this prefixcode.
|
byte[] |
encode(byte[] origData)
Encoding the byte array using this prefixcode.
|
static Huffman |
fromByteArray(byte[] codeTree)
Deserialization of the code tree from bytearray.
|
static byte[] |
fromFile(java.lang.String fileName)
Decoding from the file.
|
(package private) static boolean |
getBit(int index,
byte[] barray)
Reading a bit from the bitarray.
|
static void |
hpack(java.lang.String sourceFile,
java.lang.String packed_File)
Compressing a file.
|
static void |
hunpack(java.lang.String packed_File,
java.lang.String unpackedFile)
Decompressing a file.
|
static void |
main(java.lang.String[] params)
Main method.
|
(package private) static Huffman.Node |
removeSmallest(java.util.List<Huffman.Node> nodelist)
Finding a tree with minimal root frequency from the rootlist.
|
(package private) static void |
setBit(int index,
byte[] barray,
boolean value)
Set a bit value in bitarray.
|
byte[] |
toByteArray()
Serialization of this prefixcode.
|
static void |
toFile(byte[] origData,
java.lang.String fileName)
Encoding and packing together the code tree and encoded data.
|
java.lang.String |
toString()
Conversion to string for debugging.
|
public static final long serialVersionUID
Huffman.Node[] leaves
Huffman.Node root
Huffman(byte[] original)
original
- source datastatic Huffman.Node removeSmallest(java.util.List<Huffman.Node> nodelist)
nodelist
- queue of roots of subtreesstatic byte[] addZero(int len, byte[] code)
len
- length of the (old) code in bitscode
- old codestatic byte[] addOne(int len, byte[] code)
len
- length of the (old) code in bitscode
- old codestatic void setBit(int index, byte[] barray, boolean value)
index
- bit index starting from 0barray
- data interpreted as an array of bitsvalue
- value of the bit to setstatic boolean getBit(int index, byte[] barray)
index
- bit index starting from zerobarray
- data interpreted as bitarraypublic int bitLength()
public java.lang.String toString()
toString
in class java.lang.Object
public byte[] encode(byte[] origData)
origData
- original datapublic byte[] decode(byte[] encodedData)
encodedData
- encoded datapublic byte[] toByteArray()
public static Huffman fromByteArray(byte[] codeTree)
codeTree
- bytearray that contains the code treepublic static void toFile(byte[] origData, java.lang.String fileName)
origData
- original data to be encodedfileName
- output file namepublic static byte[] fromFile(java.lang.String fileName)
fileName
- input file name that contains code tree and encoded datapublic static void hpack(java.lang.String sourceFile, java.lang.String packed_File)
sourceFile
- input file name (original data to be compressed)packed_File
- output file name (contains both tree and encoded data)public static void hunpack(java.lang.String packed_File, java.lang.String unpackedFile)
packed_File
- input file name (contains both tree and encoded data)unpackedFile
- output file name (restored data)public static void main(java.lang.String[] params)