Example usage for org.apache.commons.compress.compressors.bzip2 BZip2CompressorOutputStream write

List of usage examples for org.apache.commons.compress.compressors.bzip2 BZip2CompressorOutputStream write

Introduction

In this page you can find the example usage for org.apache.commons.compress.compressors.bzip2 BZip2CompressorOutputStream write.

Prototype

public void write(final int b) throws IOException 

Source Link

Usage

From source file:io.digibyte.tools.util.BRCompressor.java

public static byte[] bz2Compress(byte[] data) throws IOException {
    if (data == null)
        return null;
    byte[] compressedData = null;
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream(data.length);
    try {//from w ww .  ja va 2s .c  o  m
        BZip2CompressorOutputStream bout = new BZip2CompressorOutputStream(byteStream);
        try {
            bout.write(data);
        } catch (Exception e) {
            throw e;
        } finally {
            try {
                bout.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        compressedData = byteStream.toByteArray();
    } finally {
        try {
            byteStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return compressedData;

}

From source file:com.breadwallet.tools.util.BRCompressor.java

public static byte[] bz2Compress(byte[] data) {
    if (data == null)
        return null;
    byte[] compressedData = null;
    try {//from   ww w .  j a va 2s  .  c o  m
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream(data.length);
        try {
            BZip2CompressorOutputStream bout = new BZip2CompressorOutputStream(byteStream);
            try {
                bout.write(data);
            } finally {
                try {
                    bout.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } finally {
            try {
                byteStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        compressedData = byteStream.toByteArray();
    } catch (Exception e) {
        e.printStackTrace();
        FirebaseCrash.report(e);
    }
    return compressedData;

}

From source file:io.netty.handler.codec.compression.Bzip2DecoderTest.java

private static void testDecompression(final byte[] data) throws Exception {
    for (int blockSize = MIN_BLOCK_SIZE; blockSize <= MAX_BLOCK_SIZE; blockSize++) {
        final EmbeddedChannel channel = new EmbeddedChannel(new Bzip2Decoder());

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        BZip2CompressorOutputStream bZip2Os = new BZip2CompressorOutputStream(os, blockSize);
        bZip2Os.write(data);
        bZip2Os.close();// w ww.j a  v a 2  s.  c o m

        ByteBuf compressed = Unpooled.wrappedBuffer(os.toByteArray());
        channel.writeInbound(compressed);

        ByteBuf uncompressed = Unpooled.buffer();
        ByteBuf msg;
        while ((msg = channel.readInbound()) != null) {
            uncompressed.writeBytes(msg);
            msg.release();
        }
        final byte[] result = new byte[uncompressed.readableBytes()];
        uncompressed.readBytes(result);
        uncompressed.release();

        assertArrayEquals(data, result);
    }
}

From source file:com.simiacryptus.text.CompressionUtil.java

/**
 * Encode bz byte [ ].//from  ww w . j a v  a  2  s. c  o m
 *
 * @param data the data
 * @return the byte [ ]
 */
public static byte[] encodeBZ(byte[] data) {
    try {
        int blockSize = 4;
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        BZip2CompressorOutputStream compresser = new BZip2CompressorOutputStream(output, blockSize);
        compresser.write(data);
        compresser.close();
        byte[] bytes = output.toByteArray();
        return bytes;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:kr.debop4j.core.compress.BZip2Compressor.java

@Override
protected byte[] doCompress(byte[] plain) throws IOException {

    @Cleanup/*from  w w  w  .j ava2  s.co  m*/
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    @Cleanup
    BZip2CompressorOutputStream bzip2 = new BZip2CompressorOutputStream(bos);

    bzip2.write(plain);
    bzip2.close();

    return bos.toByteArray();
}

From source file:com.chenshu.compress.CompressOldTest.java

@Benchmark
public int commonsBZip2Compress() {
    ByteArrayOutputStream bout = null;
    BZip2CompressorOutputStream bzip2out = null;
    try {//from   ww w  .j  av a2 s . c  om
        bout = new ByteArrayOutputStream(data.length);
        bzip2out = new BZip2CompressorOutputStream(bout, level);
        bzip2out.write(data);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (bzip2out != null) {
            try {
                bzip2out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (bout != null) {
            try {
                bout.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    byte[] bs = bout.toByteArray();
    return bs.length;
}

From source file:io.netty.handler.codec.compression.Bzip2DecoderTest.java

@Test
public void testDecompressionOfBatchedFlowOfData() throws Exception {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    BZip2CompressorOutputStream bZip2Os = new BZip2CompressorOutputStream(os,
            rand.nextInt(MIN_BLOCK_SIZE, MAX_BLOCK_SIZE + 1));
    bZip2Os.write(BYTES_LARGE);
    bZip2Os.close();// ww  w.ja  v  a 2  s . co  m

    final byte[] compressedArray = os.toByteArray();
    int written = 0, length = rand.nextInt(100);
    while (written + length < compressedArray.length) {
        ByteBuf compressed = Unpooled.wrappedBuffer(compressedArray, written, length);
        channel.writeInbound(compressed);
        written += length;
        length = rand.nextInt(100);
    }
    ByteBuf compressed = Unpooled.wrappedBuffer(compressedArray, written, compressedArray.length - written);
    channel.writeInbound(compressed);

    ByteBuf uncompressed = Unpooled.buffer();
    ByteBuf msg;
    while ((msg = channel.readInbound()) != null) {
        uncompressed.writeBytes(msg);
        msg.release();
    }
    final byte[] result = new byte[uncompressed.readableBytes()];
    uncompressed.readBytes(result);
    uncompressed.release();

    assertArrayEquals(BYTES_LARGE, result);
}

From source file:de.dentrassi.pm.deb.aspect.internal.RepoBuilder.java

private byte[] compressBzip2(final byte[] data) throws IOException {
    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    final BZip2CompressorOutputStream b2os = new BZip2CompressorOutputStream(bos);

    b2os.write(data);

    b2os.close();/* ww  w.j a v  a 2  s.  c o m*/
    return bos.toByteArray();
}

From source file:org.apache.camel.processor.aggregate.BZip2DataFormatTest.java

protected byte[] getCompressedBody() throws IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    BZip2CompressorOutputStream zout = new BZip2CompressorOutputStream(bout);
    try {/*from w  ww . j  av  a 2s. co  m*/
        zout.write(getPlainTextBody().getBytes("UTF-8"));
    } finally {
        IOHelper.close(zout);
    }
    return bout.toByteArray();
}

From source file:org.apache.trevni.BZip2Codec.java

@Override
ByteBuffer compress(ByteBuffer uncompressedData) throws IOException {
    ByteArrayOutputStream baos = getOutputBuffer(uncompressedData.remaining());
    BZip2CompressorOutputStream outputStream = new BZip2CompressorOutputStream(baos);

    try {/*  w ww .  j a  va 2  s .co  m*/
        outputStream.write(uncompressedData.array());
    } finally {
        outputStream.close();
    }

    ByteBuffer result = ByteBuffer.wrap(baos.toByteArray());
    return result;
}