List of usage examples for org.apache.hadoop.io.compress BlockCompressorStream BlockCompressorStream
public BlockCompressorStream(OutputStream out, Compressor compressor, int bufferSize, int compressionOverhead)
From source file:io.github.dlmarion.clowncar.hdfs.TestBloscCompressorDecompressor.java
License:Apache License
@Test public void testCompressorDecompressorLogicWithCompressionStreams() { DataOutputStream deflateOut = null; DataInputStream inflateIn = null; int BYTE_SIZE = 1024 * 100; byte[] bytes = generate(BYTE_SIZE); int bufferSize = 262144; int compressionOverhead = (bufferSize / 6) + 32; try {/*from w ww . ja va2 s.co m*/ Configuration conf = new Configuration(false); conf.set(BloscCompressor.COMPRESSOR_NAME_KEY, compressor); conf.set(BloscCompressor.COMPRESSION_LEVEL_KEY, Integer.toString(level)); conf.set(BloscCompressor.BYTES_FOR_TYPE_KEY, Integer.toString(Integer.BYTES)); conf.set(BloscCompressor.SHUFFLE_TYPE_KEY, Integer.toString(shuffle)); conf.set(BloscCompressor.NUM_THREADS_KEY, Integer.toString(threads)); DataOutputBuffer compressedDataBuffer = new DataOutputBuffer(); CompressionOutputStream deflateFilter = new BlockCompressorStream(compressedDataBuffer, new BloscCompressor(bufferSize, conf), bufferSize, compressionOverhead); deflateOut = new DataOutputStream(new BufferedOutputStream(deflateFilter)); deflateOut.write(bytes, 0, bytes.length); deflateOut.flush(); deflateFilter.finish(); DataInputBuffer deCompressedDataBuffer = new DataInputBuffer(); deCompressedDataBuffer.reset(compressedDataBuffer.getData(), 0, compressedDataBuffer.getLength()); CompressionInputStream inflateFilter = new BlockDecompressorStream(deCompressedDataBuffer, new BloscDecompressor(bufferSize), bufferSize); inflateIn = new DataInputStream(new BufferedInputStream(inflateFilter)); byte[] result = new byte[BYTE_SIZE]; inflateIn.read(result); assertArrayEquals("original array not equals compress/decompressed array", result, bytes); } catch (IOException e) { e.printStackTrace(); fail("testBloscCompressorDecopressorLogicWithCompressionStreams ex error !!!"); } finally { try { if (deflateOut != null) deflateOut.close(); if (inflateIn != null) inflateIn.close(); } catch (Exception e) { } } }
From source file:org.anarres.lzo.hadoop.codec.LzoCodec.java
License:Open Source License
@Override public CompressionOutputStream createOutputStream(OutputStream out, Compressor compressor) throws IOException { /**/*from ww w .j a va 2s . c om*/ * <b>http://www.oberhumer.com/opensource/lzo/lzofaq.php</b> * * How much can my data expand during compression ? * ================================================ * LZO will expand incompressible data by a little amount. * I still haven't computed the exact values, but I suggest using * these formulas for a worst-case expansion calculation: * * Algorithm LZO1, LZO1A, LZO1B, LZO1C, LZO1F, LZO1X, LZO1Y, LZO1Z: * ---------------------------------------------------------------- * output_block_size = input_block_size + (input_block_size / 16) + 64 + 3 * * This is about 106% for a large block size. * * Algorithm LZO2A: * ---------------- * output_block_size = input_block_size + (input_block_size / 8) + 128 + 3 */ // Create the lzo output-stream Configuration conf = getConf(); LzoCompressor.CompressionStrategy strategy = getCompressionStrategy(conf); int bufferSize = getBufferSize(conf); int compressionOverhead = strategy.name().contains("LZO1") ? (bufferSize >> 4) + 64 + 3 : (bufferSize >> 3) + 128 + 3; return new BlockCompressorStream(out, compressor, bufferSize, compressionOverhead); }