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.

public native synchronized void setDictionary(byte[] data,<br />

int offset, int length)<br />

Presetting a dictionary is never necessary and requires detailed<br />

understanding of both the compression format used and the data to be<br />

compressed. Putting the wrong data in your dictionary can actually<br />

increase the file size. Unless you're a compression expert and you really<br />

need every last byte of space you can save, I recommend letting the<br />

deflater build the dictionary adaptively as the data is compressed.<br />

I started with a highly compressible 44,392-byte text file (the output of<br />

running FileDumper2.java on itself in decimal mode). Without<br />

presetting the dictionary, it deflated to 3,859 bytes. My first attempt to<br />

preset the dictionary to the ASCII digits, space, and \r\n actually<br />

increased that size to 3,863 bytes. After carefully examining the data<br />

and custom-designing a dictionary to fit it, I was able to deflate the data<br />

to 3,852 bytes, saving a whopping 7 extra bytes or 0.18%. Of course,<br />

the dictionary itself occupied 112 bytes, so it's truly arguable whether I<br />

really saved anything.<br />

Exact details are likely to vary from file to file. The only real possible<br />

gain is for very short, very predictable files where zlib may not have<br />

enough data to build a good dictionary before the end of stream is<br />

reached. However, zlib uses a pretty good algorithm for building an<br />

adaptive dictionary, and you're unlikely to do significantly better by<br />

hand. I recommend you not worry about setting a dictionary, and simply<br />

let the deflater build one for you.<br />

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

If Inflater.inflate() decompresses the data later, the Inflater.getAdler() method will<br />

return the Adler-32 checksum of the dictionary needed for decompression. However, you'll<br />

need some other means to pass the dictionary itself between the deflater and the inflater. It is<br />

not stored with the deflated file.<br />

9.1.1.5 Set the input<br />

Next you must set the input data to be deflated with one of the setInput() methods:<br />

public void setInput(byte[] input)<br />

public synchronized void setInput(byte[] input, int offset, int length)<br />

The first method prepares the entire array to be deflated. The second method prepares the<br />

specified subarray of data starting at offset and continuing for length bytes.<br />

9.1.1.6 Deflate the data repeatedly until needsInput( ) returns true<br />

Finally, you're ready to deflate the data. Once setInput() has filled the input buffer with<br />

data, it is deflated through one of two deflate() methods:<br />

public int deflate(byte[] output)<br />

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

length)<br />

144

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

Saved successfully!

Ooh no, something went wrong!