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

if it doesn't, it will compress within a few percent of the optimal strategy, so it's rarely worth<br />

agonizing over which is the best solution.<br />

9.1.1.3 Set the compression level<br />

The deflater compresses by trying to match the data it's looking at now to data it's already<br />

seen earlier in the stream. The compression level determines how far back in the stream the<br />

deflater looks for a match. The farther back it looks, the more likely it is to find a match and<br />

the larger the run of bytes it can replace with a simple pointer. However, the farther back it<br />

looks, the longer it takes as well. Thus, compression level is a trade-off between speed and<br />

file size. The tighter you compress, the more time it takes. Generally, the compression level is<br />

set in the constructor, but you can change it after the deflater is constructed by using the<br />

setLevel() method:<br />

public synchronized void setLevel(int Level)<br />

As with the Deflater() constructors, the compression level should be an int between and 9<br />

(no compression to maximum compression) or perhaps -1, signifying the default compression<br />

level. Any other value will cause an IllegalArgumentException. It's good coding style to<br />

use one of the mnemonic constants Deflater.NO_COMPRESS<strong>IO</strong>N (0), Deflater.BEST_SPEED<br />

(1), Deflater.BEST_COMPRESS<strong>IO</strong>N (9), or Deflater.DEFAULT_COMPRESS<strong>IO</strong>N (-1) instead of<br />

an explicit value.<br />

In limited testing with small files, I haven't found the difference between best speed and best<br />

compression to be noticeable, either in file size or the time it takes to compress or<br />

decompress. You may occasionally want to set the level to no compression (0) if you're<br />

deflating already compressed files like GIF, JPEG, or PNG images before storing them in an<br />

archive. These file formats have built-in compression algorithms specifically designed for the<br />

type of data they contain, and the general-purpose deflation algorithm provided here is<br />

unlikely to compress them further. [2] It may even increase their size.<br />

9.1.1.4 Set the dictionary<br />

You can think of the deflater as building a dictionary of phrases as it reads the text. The first<br />

time it sees a phrase, it puts the phrase in the dictionary. The second time it sees the phrase, it<br />

replaces the phrase with its position in the dictionary. However, it can't do this until it's seen<br />

the phrase at least once, so data early in the stream isn't compressed very well compared to<br />

data that occurs later in the stream. On rare occasion, when you have a good idea that certain<br />

byte sequences appear in the data very frequently, you can preset the dictionary used for<br />

compression. You would fill the dictionary with the frequently repeated data in the text. For<br />

instance, if your text is composed completely of ASCII digits and assorted whitespace (tabs,<br />

carriage returns, and so forth) you could put those characters in your dictionary. This allows<br />

the early part of the stream to compress as well as later parts.<br />

There are two setDictionary() methods. The first uses the entire byte array passed as an<br />

argument as the dictionary. The second uses the subarray of data starting at offset and<br />

continuing for length bytes.<br />

public void setDictionary(byte[] data)<br />

2 In fact, the deflation algorithm described here is the exact algorithm used by PNG images; it was first invented specifically for the PNG file format.<br />

143

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

Saved successfully!

Ooh no, something went wrong!