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.

9.1.1.10 Checking the state of a deflater<br />

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

The Deflater class also provides several methods that return information about the deflater's<br />

state. The getAdler() method returns the Adler-32 checksum of the uncompressed data. This<br />

is not a java.util.zip.Checksum object but the actual int value of the checksum:<br />

public native synchronized int getAdler()<br />

The getTotalIn() method returns the number of uncompressed bytes passed to the<br />

setInput() method:<br />

public native synchronized int getTotalIn()<br />

The getTotalOut() method returns the total number of compressed bytes output so far via<br />

deflate():<br />

public native synchronized int getTotalOut()<br />

For example, to print a running total of the compression achieved by the Deflater object<br />

def, you might do something like this:<br />

System.out.println((1.0 - def.getTotalOut()/def.getTotalIn())*100.0 +<br />

?% saved?);<br />

9.1.2 Inflating Data<br />

The Inflater class contains methods to decompress blocks of data compressed in the zip,<br />

gzip, or zlib formats. This data may have been produced by <strong>Java</strong>'s Deflater class or by some<br />

other program written in another language entirely, such as PKZip or gzip. Using an inflater is<br />

a little simpler than using a deflater, since there aren't a lot of settings to pick. Those were<br />

established when the data was compressed. There are seven steps to inflating data:<br />

1. Construct an Inflater object.<br />

2. Set the input with the compressed data to be inflated.<br />

3. Call needsDictionary() to determine if a preset dictionary is required.<br />

4. If needsDictionary() returns true, call getAdler() to get the Adler-32 checksum<br />

of the dictionary. Then invoke setDictionary() to set the dictionary data.<br />

5. Inflate the data repeatedly until inflate() returns zero.<br />

6. If needsInput() returns true, go back to step 2 to provide additional input data.<br />

7. The finished() method returns true.<br />

If you want to decompress more data with this Inflater object, reset it.<br />

You rarely use this class directly. Instead, you use an inflater indirectly through one of the<br />

decompressing stream classes like InflaterInputStream or InflaterOutputStream. These<br />

classes provide much more convenient programmer interfaces for stream-oriented<br />

decompression.<br />

148

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

Saved successfully!

Ooh no, something went wrong!