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

to store the dictionary file, along with the compressed data, in an archive. Another possibility<br />

is that programs that read and write many very similar files may always use the same<br />

dictionary built into both the compression and decompression programs.<br />

9.1.2.5 Inflate the data<br />

Once setInput() has filled the input buffer with data, it is inflated through one of two<br />

inflate() methods:<br />

public int inflate(byte[] output) throws DataFormatException<br />

public native synchronized int inflate(byte[] output, int offset, int<br />

length)<br />

throws DataFormatException<br />

The first method fills the output array with the uncompressed data. The second fills the<br />

specified subarray, beginning at offset and continuing for length bytes, with the<br />

uncompressed data. The actual number of uncompressed bytes written into the array is<br />

returned. If one of these methods returns 0, then you should check needsInput() to see if<br />

you need to call setInput() again to insert more compressed input data:<br />

public boolean needsInput()<br />

When more data is needed, needsInput()returns true. At this point you call setInput()<br />

again to feed in more compressed input data, then call inflate(), then repeat the process<br />

until there is no more input data to be decompressed. If more data is not needed after<br />

inflate() returns zero, this should mean that decompression is finished, and the<br />

finished() method should return true:<br />

public synchronized boolean finished()<br />

The inflate() methods throw a java.util.zip.DataFormatException if they encounter<br />

invalid data, generally indicating a corrupted input stream. This is a direct subclass of<br />

java.lang.Exception, not an <strong>IO</strong>Exception.<br />

9.1.2.6 Reset the inflater<br />

This completes the sequence of method invocations required to decompress data. If you'd like<br />

to use the same settings to decompress more data with the same Inflater object, you can<br />

invoke its reset() method:<br />

public native synchronized void reset()<br />

Otherwise, you call end() to throw away any unprocessed input and free the resources used<br />

by the native code:<br />

public native synchronized void end()<br />

The finalize() method calls end() before the inflater is garbage-collected, even if you<br />

forget to invoke it explicitly:<br />

protected void finalize()<br />

150

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

Saved successfully!

Ooh no, something went wrong!