Compressing and Decompressing Streams

The Inflater and Deflater classes are a little raw for easy digestion. It would be more convenient to write uncompressed data onto an output stream and have it compressed by the stream itself, without having to worry about the mechanics of deflation. Similarly, it would be useful to have an input stream class that could read from a compressed file but return the uncompressed data. Java, in fact, has several classes that do exactly this. The class is a filter stream that compresses the data it receives in deflated format before writing it out to the underlying stream. The class inflates deflated data before passing it to the reading program. and do the same thing except with the gzip format.

The DeflaterOutputStream Class

DeflaterOutputStream is a filter stream that deflates data before writing it onto the underlying stream:

public class DeflaterOutputStream extends FilterOutputStream

Each stream uses a protected Deflater object called def to compress data stored in a protected internal buffer called buf:

protected Deflater def;
protected byte[] buf;

The same deflater must not be used in multiple streams at the same time, though Java takes no steps to guarantee this.

The underlying output stream that receives the deflated data, the deflater object def, and the length of the byte array buf are all set by one of the three ...

Get Java I/O now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.