Java Reference
In-Depth Information
1 import java.io.IOException;
2 import java.io.InputStream;
3 import java.io.DataInputStream;
4
5 /**
6 * HZIPInputStream wraps an input stream. read returns an
7 * uncompressed byte from the wrapped input stream.
8 */
9 public class HZIPInputStream extends InputStream
10 {
11 public HZIPInputStream( InputStream in ) throws IOException
12 {
13 DataInputStream din = new DataInputStream( in );
14
15 codeTree = new HuffmanTree( );
16 codeTree.readEncodingTable( din );
17
18 bin = new BitInputStream( in );
19 }
20
21 public int read( ) throws IOException
22 {
23 String bits = "";
24 int bit;
25 int decode;
26
27 while( true )
28 {
29 bit = bin.readBit( );
30 if( bit == -1 )
31 throw new IOException( "Unexpected EOF" );
32
33 bits += bit;
34 decode = codeTree.getChar( bits );
35 if( decode == HuffmanTree.INCOMPLETE_CODE )
36 continue;
37 else if( decode == HuffmanTree.ERROR )
38 throw new IOException( "Decoding error" );
39 else if( decode == HuffmanTree.END )
40 return -1;
41 else
42 return decode;
43 }
44 }
45
46 public void close( ) throws IOException
47 { bin.close( ); }
48
49 private BitInputStream bin;
50 private HuffmanTree codeTree;
51 }
figure 12.24
The HZIPInputStream
class
 
Search WWH ::




Custom Search