List of usage examples for org.apache.hadoop.io.compress CompressionOutputStream write
@Override public abstract void write(byte[] b, int off, int len) throws IOException;
From source file:Compress.TestLZO.java
License:Open Source License
public static void main(String[] argv) throws IOException { System.out.println(System.getProperty("java.library.path")); Configuration conf = new Configuration(); conf.setInt("io.compression.codec.lzo.buffersize", 64 * 1024); LzoCodec codec = new LzoCodec(); codec.setConf(conf);/*from w ww. j a v a 2 s.co m*/ OutputStream out = new DataOutputBuffer(); CompressionOutputStream out2 = codec.createOutputStream(out); byte[] str2 = new byte[20]; int num = 10000; int before = 0; String part = "hello konten hello konten"; for (long i = 0; i < num; i++) { Util.long2bytes(str2, i); out2.write(str2, 0, 8); } out2.finish(); byte[] buffer = ((DataOutputBuffer) out).getData(); System.out.println("org len:" + num * 8 + ", compressed len:" + ((DataOutputBuffer) out).getLength()); InputStream in = new DataInputBuffer(); ((DataInputBuffer) in).reset(((DataOutputBuffer) out).getData(), 0, ((DataOutputBuffer) out).getLength()); CompressionInputStream in2 = codec.createInputStream(in); byte[] buf = new byte[100]; for (long i = 0; i < num; i++) { int count = 0; count = in2.read(buf, 0, 8); if (count > 0) { long value = Util.bytes2long(buf, 0, 8); if (value != i) { System.out.println(i + ",count:" + count + ",value:" + value); } else if (i > (num - 20)) { System.out.println(i + ",value:" + value); } } else { System.out.println("count:" + count + ", string " + i); break; } } in2.close(); System.out.println("test compress array..."); OutputStream out3 = new DataOutputBuffer(); CompressionOutputStream out4 = codec.createOutputStream(out3); DataOutputBuffer tout3 = new DataOutputBuffer(); for (long i = 0; i < num; i++) { Util.long2bytes(str2, i); out4.write(str2, 0, 8); } out4.finish(); buffer = ((DataOutputBuffer) out3).getData(); System.out.println("org len:" + num * 8 + ", compressed len:" + ((DataOutputBuffer) out3).getLength()); InputStream in3 = new DataInputBuffer(); ((DataInputBuffer) in3).reset(((DataOutputBuffer) out3).getData(), 0, ((DataOutputBuffer) out3).getLength()); CompressionInputStream in4 = codec.createInputStream(in3); for (long i = 0; i < num; i++) { int count = 0; count = in4.read(buf, 0, 8); if (count > 0) { long value = Util.bytes2long(buf, 0, 8); if (value != i) { System.out.println(i + ",count:" + count + ",value:" + value); } if (i > (num - 20)) { System.out.println(i + ",value:" + value); } } else { System.out.println("count:" + count + ", string " + i); break; } } in2.close(); }
From source file:org.apache.parquet.hadoop.TestSnappyCodec.java
License:Apache License
@Test public void TestSnappyStream() throws IOException { SnappyCodec codec = new SnappyCodec(); codec.setConf(new Configuration()); int blockSize = 1024; int inputSize = blockSize * 1024; byte[] input = new byte[inputSize]; for (int i = 0; i < inputSize; ++i) { input[i] = (byte) i; }// w w w. j a v a 2s . c om ByteArrayOutputStream compressedStream = new ByteArrayOutputStream(); CompressionOutputStream compressor = codec.createOutputStream(compressedStream); int bytesCompressed = 0; while (bytesCompressed < inputSize) { int len = Math.min(inputSize - bytesCompressed, blockSize); compressor.write(input, bytesCompressed, len); bytesCompressed += len; } compressor.finish(); byte[] rawCompressed = Snappy.compress(input); byte[] codecCompressed = compressedStream.toByteArray(); // Validate that the result from the codec is the same as if we compressed the // buffer directly. assertArrayEquals(rawCompressed, codecCompressed); ByteArrayInputStream inputStream = new ByteArrayInputStream(codecCompressed); CompressionInputStream decompressor = codec.createInputStream(inputStream); byte[] codecDecompressed = new byte[inputSize]; int bytesDecompressed = 0; int numBytes; while ((numBytes = decompressor.read(codecDecompressed, bytesDecompressed, blockSize)) != 0) { bytesDecompressed += numBytes; if (bytesDecompressed == inputSize) break; } byte[] rawDecompressed = Snappy.uncompress(rawCompressed); assertArrayEquals(input, rawDecompressed); assertArrayEquals(input, codecDecompressed); }