Java Reference
In-Depth Information
compression stream classes
All that is left to do is to write a compression and uncompression stream
wrapper and then a main that calls them. We repeat our earlier disclaimer
about skimping on error checking so that we can illustrate the basic algorith-
mic ideas.
The HZIPOutputStream class is shown in Figure 12.23. The constructor
initiates a DataOutputStream , on which we can write the compressed stream.
We also maintain a ByteArrayOutputStream . Each call to write appends onto
the ByteArrayOutputStream . When close is called, the actual compressed
stream is written.
The close routine extracts all the byte s that have been stored in the
ByteArrayOutputStream for reading at line 26. It then constructs a CharCounter
object at line 29 and a HuffmanTree object at line 32. Since CharCounter needs
an InputStream , we construct a ByteArrayInputStream from the array of bytes
that were just extracted. At line 33 we write out the encoding table.
At this point we are ready to do the main encoding. We create a bit-output
stream object at line 35. The rest of the algorithm repeatedly gets a character
and writes its code (line 38). There is a tricky piece of code at line 38: The int
passed to getCode may be confused with EOF if we simply use the byte because
the high bit can be interpreted as a sign bit. Thus we use a bit mask. When we
exit the loop, we have reached the end of file, so we write out the end-of-file
code at line 39. The BitOutputStream close flushes any remaining bits to the
output file, so an explicit call to flush is not needed.
The HZIPInputStream class is next, in Figure 12.24. The constructor creates
a DataInputStream and constructs a HuffmanTree object by reading the encoding
table (lines 15 and 16) from the compressed stream. We then create a bit-input
stream at line 18. The dirty work is done in the read method.
The bits object, declared at line 23, represents the (Huffman) code that
we are currently examining. Each time we read a bit at line 29, we add the bit
to the end of the Huffman code (at line 33). We then look up the Huffman
code at line 34. If it is incomplete, we continue the loop (lines 35 and 36). If
there is an illegal Huffman code, we throw an IOException (lines 37 to 38). If
we reach the end-of-file code, we return -1, as is standard for read (lines 39
and 40); otherwise, we have a match, so we return the character that matches
the Huffman code (line 42).
 
Search WWH ::




Custom Search