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

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

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Usage

From source file:Bzip2Compress.java

public static void main(final String[] args) throws Exception {
    if (2 != args.length) {
        System.out.println("java Bzip2Compress <input> <output>");
        System.exit(1);// w  w  w.  j  a v a2 s. c  o  m
    }

    final File source = new File(args[0]);
    final File destination = new File(args[1]);
    final BZip2CompressorOutputStream output = new BZip2CompressorOutputStream(
            new FileOutputStream(destination));
    final FileInputStream input = new FileInputStream(source);
    copy(input, output);
    input.close();
    output.close();
}

From source file:CompressTransfer.java

/**
 * ?/*from   www  .  ja  v a2s.co  m*/
 *
 * @param is
 * @param os
 * @throws Exception
 */
public static void compress(InputStream is, OutputStream os) throws Exception {

    BZip2CompressorOutputStream gos = new BZip2CompressorOutputStream(os);

    int count;
    byte data[] = new byte[BUFFER];
    while ((count = is.read(data, 0, BUFFER)) != -1) {
        gos.write(data, 0, count);
    }

    gos.finish();

    gos.flush();
    gos.close();
}

From source file:CompressTransfer.java

public static void transfer(FileInputStream srcIs, FileOutputStream destOut) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    BZip2CompressorOutputStream gos = new BZip2CompressorOutputStream(destOut);
    int count;//from  w  w w.  j  a  va 2  s .co m
    byte data[] = new byte[BUFFER];
    while ((count = srcIs.read(data, 0, BUFFER)) != -1) {
        //out.write(data, 0, count);
        gos.write(data, 0, count);
    }
    gos.flush();
    gos.close();
}

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);/* w ww .jav a2  s.c  o  m*/
        bZip2Os.close();

        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.breadwallet.tools.util.BRCompressor.java

public static byte[] bz2Compress(byte[] data) {
    if (data == null)
        return null;
    byte[] compressedData = null;
    try {//www.  j a v a 2s  .  co 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.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 ww  w. ja v a  2 s .  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.simiacryptus.text.CompressionUtil.java

/**
 * Encode bz byte [ ].//from www.ja  v  a  2 s .  co  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:com.yahoo.ycsb.db.RedisClient.java

public static String compress(String st) {
    if (compress != null && compress.equals("y")) {
        if (compressAlgo != null && (compressAlgo.equals("lz4") || compressAlgo.equals("lz4hc"))) {
            try {
                byte[] data = st.getBytes("ISO-8859-1");
                LZ4Compressor compressor;
                if (compressAlgo.equals("lz4")) {
                    compressor = lz4factory.fastCompressor();
                } else {
                    compressor = lz4factory.highCompressor();
                }//from w  w  w. j a  v a 2 s. com
                final int decompressedLength = data.length;
                int maxCompressedLength = compressor.maxCompressedLength(decompressedLength);
                byte[] compressed = new byte[maxCompressedLength];
                int compressedLength = compressor.compress(data, 0, decompressedLength, compressed, 0,
                        maxCompressedLength);
                byte[] compressed2 = Arrays.copyOf(compressed, compressedLength);
                String ret = decompressedLength + "|" + new String(compressed2, "ISO-8859-1");
                return ret;
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (compressAlgo != null && compressAlgo.equals("bzip2")) {
            try {
                ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(st.getBytes("ISO-8859-1"));
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                BZip2CompressorOutputStream bzOut = new BZip2CompressorOutputStream(baos);
                final byte[] buffer = new byte[8192];
                int n = 0;
                while (-1 != (n = byteArrayInputStream.read(buffer))) {
                    bzOut.write(buffer, 0, n);
                }
                bzOut.close();
                return new String(baos.toByteArray(), "ISO-8859-1");
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (compressAlgo != null && compressAlgo.equals("snappy")) {
            try {
                byte[] compressed = Snappy.compress(st, "ISO-8859-1");
                String ret = new String(compressed, "ISO-8859-1");
                return ret;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return st;
}

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

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

    @Cleanup/*from  w  w  w .  java 2 s  .c o  m*/
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    @Cleanup
    BZip2CompressorOutputStream bzip2 = new BZip2CompressorOutputStream(bos);

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

    return bos.toByteArray();
}

From source file:com.linkedin.r2.filter.compression.stream.TestStreamingCompression.java

@Test
public void testBzip2Compressor()
        throws IOException, InterruptedException, CompressionException, ExecutionException {
    StreamingCompressor compressor = new Bzip2Compressor(_executor);
    final byte[] origin = new byte[BUF_SIZE];
    Arrays.fill(origin, (byte) 'c');

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    BZip2CompressorOutputStream bzip = new BZip2CompressorOutputStream(out);
    IOUtils.write(origin, bzip);//  w  ww . j av  a2 s  . c  om
    bzip.close();
    byte[] compressed = out.toByteArray();

    testCompress(compressor, origin, compressed);
    testDecompress(compressor, origin, compressed);
    testCompressThenDecompress(compressor, origin);
}