Java Reference
In-Depth Information
1 /**
2 * Writes an encoding table to an output stream.
3 * Format is character, count (as bytes).
4 * A zero count terminates the encoding table.
5 */
6 public void writeEncodingTable( DataOutputStream out ) throws IOException
7 {
8 for( int i = 0; i < BitUtils.DIFF_BYTES; i++ )
9 {
10 if( theCounts.getCount( i ) > 0 )
11 {
12 out.writeByte( i );
13 out.writeInt( theCounts.getCount( i ) );
14 }
15 }
16 out.writeByte( 0 );
17 out.writeInt( 0 );
18 }
19
20 /**
21 * Read the encoding table from an input stream in format
22 * given and then construct the Huffman tree.
23 * Stream will then be positioned to read compressed data.
24 */
25 public void readEncodingTable( DataInputStream in ) throws IOException
26 {
27 for( int i = 0; i < BitUtils.DIFF_BYTES; i++ )
28 theCounts.setCount( i, 0 );
29
30 int ch;
31 int num;
32
33 for( ; ; )
34 {
35 ch = in.readByte( );
36 num = in.readInt( );
37 if( num == 0 )
38 break;
39 theCounts.setCount( ch, num );
40 }
41
42 createTree( );
43 }
figure 12.21
Routines for reading and writing encoding tables
Search WWH ::




Custom Search