Database Reference
In-Depth Information
Compressing and decompressing streams with CompressionCodec
CompressionCodec has two methods that allow you to easily compress or decompress
data. To compress data being written to an output stream, use the createOut-
putStream(OutputStream out) method to create a CompressionOut-
putStream to which you write your uncompressed data to have it written in compressed
form to the underlying stream. Conversely, to decompress data being read from an input
stream, call createInputStream(InputStream in) to obtain a Compres-
sionInputStream , which allows you to read uncompressed data from the underlying
stream.
CompressionOutputStream and CompressionInputStream are similar to
java.util.zip.DeflaterOutputStream and
java.util.zip.DeflaterInputStream , except that both of the former provide
the ability to reset their underlying compressor or decompressor. This is important for ap-
plications that compress sections of the data stream as separate blocks, such as in a
SequenceFile , described in SequenceFile .
Example 5-1 illustrates how to use the API to compress data read from standard input and
write it to standard output.
Example 5-1. A program to compress data read from standard input and write it to stand-
ard output
public class StreamCompressor {
public static void main ( String [] args ) throws Exception {
String codecClassname = args [ 0 ];
Class <?> codecClass = Class . forName ( codecClassname );
Configuration conf = new Configuration ();
CompressionCodec codec = ( CompressionCodec )
ReflectionUtils . newInstance ( codecClass , conf );
CompressionOutputStream out = codec . createOutputStream ( System . out );
IOUtils . copyBytes ( System . in , out , 4096 , false );
out . finish ();
}
}
The application expects the fully qualified name of the CompressionCodec imple-
mentation as the first command-line argument. We use ReflectionUtils to construct
a new instance of the codec, then obtain a compression wrapper around System.out .
Then we call the utility method copyBytes() on IOUtils to copy the input to the
Search WWH ::




Custom Search