23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Java</strong> I/O<br />

stream itself, without having to worry about the mechanics of deflation. Similarly, it would be<br />

useful to have an input stream class that could read from a compressed file but return the<br />

uncompressed data. <strong>Java</strong>, in fact, has several classes that do exactly this. The<br />

java.util.zip.DeflaterOutputStream class is a filter stream that compresses the data it<br />

receives in deflated format before writing it out to the underlying stream. The<br />

java.util.zip.InflaterInputStream class inflates deflated data before passing it to the<br />

reading program. java.util.zip.GZIPInputStream and<br />

java.util.zip.GZIPOutputStream do the same thing except with the gzip format.<br />

9.2.1 The DeflaterOutputStream Class<br />

DeflaterOutputStream is a filter stream that deflates data before writing it onto the<br />

underlying stream:<br />

public class DeflaterOutputStream extends FilterOutputStream<br />

Each stream uses a protected Deflater object called def to compress data stored in a<br />

protected internal buffer called buf:<br />

protected Deflater def;<br />

protected byte[] buf;<br />

The same deflater must not be used in multiple streams at the same time, though <strong>Java</strong> takes no<br />

steps to guarantee this.<br />

The underlying output stream that receives the deflated data, the deflater object def, and the<br />

length of the byte array buf are all set by one of the three DeflaterOutputStream<br />

constructors:<br />

public DeflaterOutputStream(OutputStream out, Deflater def, int<br />

bufferLength)<br />

public DeflaterOutputStream(OutputStream out, Deflater def)<br />

public DeflaterOutputStream(OutputStream out)<br />

The underlying output stream must be specified. The buffer length defaults to 512 bytes, and<br />

the Deflater defaults to the default compression level, strategy, and method. Of course, the<br />

DeflaterOutputStream has all the usual output stream methods like write(), flush(), and<br />

close(). It overrides three of these methods, but as a client programmer, you don't use them<br />

any differently than you would in any other output stream:<br />

public void write(int b) throws <strong>IO</strong>Exception<br />

public void write(byte[] data, int offset, int length) throws <strong>IO</strong>Exception<br />

public void close() throws <strong>IO</strong>Exception<br />

There's also one new method, finish(), which finishes writing the compressed data onto the<br />

underlying output stream but does not close the underlying stream. You should call finish()<br />

instead of close() if there are multiple filters chained to the stream:<br />

public void finish() throws <strong>IO</strong>Exception<br />

The close() method finishes writing the compressed data onto the underlying stream and<br />

then closes it:<br />

153

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!