Java Reference
In-Depth Information
1
import java.io.IOException;
2
import java.io.OutputStream;
3
import java.io.DataOutputStream;
4
import java.io.ByteArrayInputStream;
5
import java.io.ByteArrayOutputStream;
6
7
/**
8
* Writes to HZIPOutputStream are compressed and
9
* sent to the output stream being wrapped.
10
* No writing is actually done until close.
11
*/
12
public class HZIPOutputStream extends OutputStream
13
{
14
public HZIPOutputStream( OutputStream out ) throws IOException
15
{
16
dout = new DataOutputStream( out );
17
}
18
19
public void write( int ch ) throws IOException
20
{
21
byteOut.write( ch );
22
}
23
24
public void close( ) throws IOException
25
{
26
byte [ ] theInput = byteOut.toByteArray( );
27
ByteArrayInputStream byteIn = new ByteArrayInputStream( theInput );
28
29
CharCounter countObj = new CharCounter( byteIn );
30
byteIn.close( );
31
32
HuffmanTree codeTree = new HuffmanTree( countObj );
33
codeTree.writeEncodingTable( dout );
34
35
BitOutputStream bout = new BitOutputStream( dout );
36
37
for( int i = 0; i < theInput.length; i++ )
38
bout.writeBits( codeTree.getCode( theInput[ i ] & 0xff ) );
39
bout.writeBits( codeTree.getCode( BitUtils.EOF ) );
40
41
bout.close( );
42
byteOut.close( );
43
}
44
45
private ByteArrayOutputStream byteOut = new ByteArrayOutputStream( );
46
private DataOutputStream dout;
47
}
figure 12.23
The
HZIPOutputStream
class
Search WWH ::
Custom Search