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 />

public GZIPInputStream(InputStream in) throws <strong>IO</strong>Exception<br />

public GZIPInputStream(InputStream in, int bufferLength) throws <strong>IO</strong>Exception<br />

Since this is a filter stream, both constructors take an underlying input stream as an argument.<br />

The second constructor also accepts a length for the buffer into which the compressed data<br />

will be read. Otherwise, GZIPInputStream has the usual methods of an input stream: read(),<br />

skip(), close(), mark(), reset(), and so on. Marking and resetting are not supported. Two<br />

methods are overridden, read() and close():<br />

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

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

These methods work exactly like the superclass methods they override. The only thing you<br />

need to be aware of is that the read() method blocks until sufficient data is available in the<br />

buffer to allow decompression.<br />

GZIPInputStream has two protected fields that may be accessed from subclasses. The crc<br />

field provides a cyclic redundancy code for that portion of the data that has been decoded.<br />

CRC32 objects are discussed later in this chapter. The eos field is a boolean indicating<br />

whether the end of the stream has been reached. It's initially set to false. It becomes true<br />

once the end of the compressed data has been reached:<br />

protected CRC32 crc;<br />

protected boolean eos;<br />

Finally, there's one not very useful mnemonic constant, GZIPInputStream.GZIP_MAGIC. All<br />

valid gzip files must begin with this number, which helps to identify the file's type:<br />

public static final int GZIP_MAGIC = 0x8B1F;<br />

Example 9.6 shows how easy it is to decompress gzipped data with GZIPInputStream. The<br />

main() method reads a series of filenames from the command line. A FileInputStream<br />

object is created for each file and a GZIPInputStream is chained to that. The data is read<br />

from the file, and the decompressed data is written into a new file with the same name minus<br />

the .gz suffix. (A more robust implementation would handle the case where the suffix is not<br />

.gz .) You can test this program with files gzipped by Example 9.5 and with files gzipped by<br />

the FSF's Gzip program.<br />

Example 9.6. The GUnzipper<br />

import java.io.*;<br />

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

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

public class GUnzipper {<br />

public static void main(String[] args) {<br />

for (int i = 0; i < args.length; i++) {<br />

if (args[i].toLowerCase().endsWith(GZipper.GZIP_SUFFIX)) {<br />

try {<br />

FileInputStream fin = new FileInputStream(args[i]);<br />

GZIPInputStream gzin = new GZIPInputStream(fin);<br />

158

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

Saved successfully!

Ooh no, something went wrong!