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

legally unencumbered—that is, not covered by any patents—lossless data-compression library<br />

for use on virtually any computer hardware and operating system."<br />

Without going into excessive detail, zip, gzip, and zlib all compress data in more or less the<br />

same way. Repeated bit sequences in the input data are replaced with pointers back to the first<br />

occurrence of that bit sequence. Other tricks are used, but this is basically how these<br />

compression schemes work and has certain implications for compression and decompression<br />

code. First, you can't randomly access data in a compressed file. To decompress the nth byte<br />

of data, you must first decompress bytes 1 through n-1 of the data. Second, a single twiddled<br />

bit doesn't just change the meaning of the byte it's part of. It also changes the meaning of<br />

bytes that come after it in the data, since subsequent bytes may be stored as copies of the<br />

previous bytes. Therefore, compressed files are much more susceptible to corruption than<br />

uncompressed files. For more general information about compression and archiving<br />

algorithms and formats, the comp.compression FAQ is a good place to start. See<br />

http://www.faqs.org/faqs/compression-faq/part1/preamble.html.<br />

9.1.1 Deflating Data<br />

The Deflater class contains methods to compress blocks of data. You can choose the<br />

compression format, the level of compression, and the compression strategy. There are nine<br />

steps to deflating data with the Deflater class:<br />

1. Construct a Deflater object.<br />

2. Choose the strategy (optional).<br />

3. Set the compression level (optional).<br />

4. Preset the dictionary (optional).<br />

5. Set the input.<br />

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

7. If more input is available, go back to step 5 to provide additional input data.<br />

Otherwise, go to step 8.<br />

8. Finish the data.<br />

9. If there are more streams to be deflated, reset the deflater.<br />

More often than not, you don't use this class directly. Instead, you use a Deflater object<br />

indirectly through one of the compressing stream classes like DeflaterInputStream or<br />

DeflaterOutputStream. These classes provide more convenient programmer interfaces for<br />

stream-oriented compression than the raw Deflater methods.<br />

9.1.1.1 Constructing deflaters<br />

There are three Deflater() constructors:<br />

public Deflater(int level, boolean useGzip)<br />

public Deflater(int level)<br />

public Deflater()<br />

The most general constructor allows you to set the level of compression and the format used.<br />

Compression level is specified as an int between and 9. is no compression; 9 is maximum<br />

compression. Generally, the higher the compression level, the smaller the output will be and<br />

the longer the compression will take. Four mnemonic constants are available to select<br />

particular levels of compression. These are:<br />

141

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

Saved successfully!

Ooh no, something went wrong!