Java Reference
In-Depth Information
byte[] input = get a data filled byte array;
compressor.setInput(input);
You call the finish() method to indicate that you have supplied all the input data.
compressor.finish();
You call the deflate() method to compress the input data. It accepts a byte array as its argument. It fills the byte
array with the compressed data and returns the number of bytes in the byte array it has filled. After every call to the
deflate() method, you need to call the finished() method to check if the compression process is over. Typically,
you would place this check in a loop as follows:
// Try to read the compressed data 1024 bytes at a time
byte[] readBuffer = new byte[1024];
int readCount = 0;
while(!compressor.finished())
readCount = compressor.deflate(readBuffer);
/* At this point, the readBuffer array has the compressed data
from index 0 to readCount - 1.
*/
}
You call the end() method to release any resources the Deflater object has held.
// Indicates that the compression process is over
compressor.end();
Follow the following steps to decompress data in a byte array. The steps are just the reverse of what you did to
compress a byte array.
1. Create an Inflater object.
2. Set the input data to be decompressed using the setInput() method.
3. Call the inflate() method to decompress the input data.
4. Call the end() method to end the decompression process.
You can create an object of the Inflater class using one of its constructors.
// Uses the no-args constructor
Inflater decompressor = new Inflater();
If the compressed data is in GZIP or PKZIP format, you use another constructor and pass true as its argument.
// Creates a decompressor to decompress data that is
// in GZIP or PKZIP format
Inflater decompressor = new Inflater(true);
 
Search WWH ::




Custom Search