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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

Each inflater input stream uses a protected Inflater object called inf to decompress data<br />

that is stored in a protected internal byte array called buf. There's also a protected int field<br />

called len that (unreliably) stores the number of bytes currently in the buffer, as opposed to<br />

the length of the buffer itself.<br />

protected Inflater inf;<br />

protected byte[] buf;<br />

protected int len;<br />

The same Inflater object must not be used in multiple streams at the same time.<br />

The underlying input stream from which deflated data is read, the Inflater object inf, and<br />

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

constructors:<br />

public InflaterInputStream(InputStream in, Inflater inf, int bufferLength)<br />

public InflaterInputStream(InputStream in, Inflater inf)<br />

public InflaterInputStream(InputStream in)<br />

The underlying input stream must be specified, while the buffer length defaults to 512 bytes,<br />

and the Inflater defaults to an inflater for deflated streams (as opposed to zipped or gzipped<br />

streams). Of course, the InflaterInputStream has all the usual input stream methods like<br />

read(), available(), and close(). It overrides the following three methods:<br />

public int read() throws <strong>IO</strong>Exception<br />

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

public long skip(long n) throws <strong>IO</strong>Exception<br />

For the most part, you use these the same way you'd use any read() or skip() method.<br />

However, it's occasionally useful to know that the read method throws a new subclass of<br />

<strong>IO</strong>Exception, java.util.zip.ZipException, if the problem is that the data doesn't adhere<br />

to the expected format. You should also know that read(), skip(), and all other input stream<br />

methods count the uncompressed bytes, not the compressed raw bytes that were actually read.<br />

There's also one new protected method, fill(), which reads compressed data from the<br />

underlying input stream into buf, sets len to the number of bytes read, and then sets inf's<br />

input to the appropriate subarray of buf:<br />

protected void fill() throws <strong>IO</strong>Exception<br />

Example 9.4 is a simple character-mode program that inflates files. When it is combined with<br />

Example 9.3, you've now got a simple compression system. Filenames are read from the<br />

command line. A file input stream is opened from each file that ends in .df l, and this stream is<br />

chained to an inflater input stream. A file output stream is opened to that same file minus the<br />

.dfl extension. Finally, a stream copier pours the data from the input file through the inflating<br />

stream into the output file.<br />

Example 9.4. The FileInflater Program<br />

import java.io.*;<br />

import java.util.zip.*;<br />

import com.macfaq.io.*;<br />

155

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

Saved successfully!

Ooh no, something went wrong!