Example usage for java.util.zip DeflaterOutputStream DeflaterOutputStream

List of usage examples for java.util.zip DeflaterOutputStream DeflaterOutputStream

Introduction

In this page you can find the example usage for java.util.zip DeflaterOutputStream DeflaterOutputStream.

Prototype

public DeflaterOutputStream(OutputStream out, Deflater def, boolean syncFlush) 

Source Link

Document

Creates a new output stream with the specified compressor, flush mode and a default buffer size.

Usage

From source file:com.uber.hoodie.common.HoodieJsonPayload.java

private byte[] compressData(String jsonData) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
    DeflaterOutputStream dos = new DeflaterOutputStream(baos, deflater, true);
    try {//w  w  w  . j av  a  2 s  .  c  o m
        dos.write(jsonData.getBytes());
    } finally {
        dos.flush();
        dos.close();
        // Its important to call this.
        // Deflater takes off-heap native memory and does not release until GC kicks in
        deflater.end();
    }
    return baos.toByteArray();
}

From source file:com.uber.hoodie.common.TestRawTripPayload.java

private byte[] compressData(String jsonData) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DeflaterOutputStream dos = new DeflaterOutputStream(baos, new Deflater(Deflater.BEST_COMPRESSION), true);
    try {//from  w w  w.j ava 2  s . c o  m
        dos.write(jsonData.getBytes());
    } finally {
        dos.flush();
        dos.close();
    }
    return baos.toByteArray();
}

From source file:jfs.sync.encryption.JFSEncryptedStream.java

private void internalClose() throws IOException {
    delegate.close();//  w w w.jav  a2  s  . c om
    byte[] bytes = delegate.toByteArray();
    final byte[] originalBytes = bytes;
    long l = bytes.length;

    byte marker = COMPRESSION_NONE;

    if (log.isDebugEnabled()) {
        log.debug("close() checking for compressions for");
    } // if

    CompressionThread dt = new CompressionThread(originalBytes) {

        @Override
        public void run() {
            try {
                ByteArrayOutputStream deflaterStream = new ByteArrayOutputStream();
                Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION, true);
                OutputStream dos = new DeflaterOutputStream(deflaterStream, deflater, COMPRESSION_BUFFER_SIZE);
                dos.write(originalBytes);
                dos.close();
                compressedValue = deflaterStream.toByteArray();
            } catch (Exception e) {
                log.error("run()", e);
            } // try/catch
        } // run()

    };

    CompressionThread bt = new CompressionThread(originalBytes) {

        @Override
        public void run() {
            try {
                if (originalBytes.length > BZIP_MAX_LENGTH) {
                    compressedValue = originalBytes;
                } else {
                    ByteArrayOutputStream bzipStream = new ByteArrayOutputStream();
                    OutputStream bos = new BZip2CompressorOutputStream(bzipStream);
                    bos.write(originalBytes);
                    bos.close();
                    compressedValue = bzipStream.toByteArray();
                } // if
            } catch (Exception e) {
                log.error("run()", e);
            } // try/catch
        } // run()

    };

    CompressionThread lt = new CompressionThread(originalBytes) {

        /*
         * // "  -a{N}:  set compression mode - [0, 1], default: 1 (max)\n" +
         * "  -d{N}:  set dictionary - [0,28], default: 23 (8MB)\n"
         * +"  -fb{N}: set number of fast bytes - [5, 273], default: 128\n"
         * +"  -lc{N}: set number of literal context bits - [0, 8], default: 3\n"
         * +"  -lp{N}: set number of literal pos bits - [0, 4], default: 0\n"
         * +"  -pb{N}: set number of pos bits - [0, 4], default: 2\n"
         * +"  -mf{MF_ID}: set Match Finder: [bt2, bt4], default: bt4\n"+"  -eos:   write End Of Stream marker\n");
         */
        private int dictionarySize = 1 << 23;

        private int lc = 3;

        private int lp = 0;

        private int pb = 2;

        private int fb = 128;

        public int algorithm = 2;

        public int matchFinderIndex = 1; // 0, 1, 2

        @Override
        public void run() {
            try {
                Encoder encoder = new Encoder();
                encoder.SetEndMarkerMode(false);
                encoder.SetAlgorithm(algorithm); // Whatever that means
                encoder.SetDictionarySize(dictionarySize);
                encoder.SetNumFastBytes(fb);
                encoder.SetMatchFinder(matchFinderIndex);
                encoder.SetLcLpPb(lc, lp, pb);

                ByteArrayOutputStream lzmaStream = new ByteArrayOutputStream();
                ByteArrayInputStream inStream = new ByteArrayInputStream(originalBytes);

                encoder.WriteCoderProperties(lzmaStream);
                encoder.Code(inStream, lzmaStream, -1, -1, null);
                compressedValue = lzmaStream.toByteArray();
            } catch (Exception e) {
                log.error("run()", e);
            } // try/catch
        } // run()

    };

    dt.start();
    bt.start();
    lt.start();

    try {
        dt.join();
        bt.join();
        lt.join();
    } catch (InterruptedException e) {
        log.error("run()", e);
    } // try/catch

    if (dt.compressedValue.length < l) {
        marker = COMPRESSION_DEFLATE;
        bytes = dt.compressedValue;
        l = bytes.length;
    } // if

    if (lt.compressedValue.length < l) {
        marker = COMPRESSION_LZMA;
        bytes = lt.compressedValue;
        l = bytes.length;
    } // if

    if (bt.compressedValue.length < l) {
        marker = COMPRESSION_BZIP2;
        bytes = bt.compressedValue;
        if (log.isWarnEnabled()) {
            log.warn("close() using bzip2 and saving " + (l - bytes.length) + " bytes.");
        } // if
        l = bytes.length;
    } // if

    if (log.isInfoEnabled()) {
        if (marker == COMPRESSION_NONE) {
            if (log.isInfoEnabled()) {
                log.info("close() using no compression");
            } // if
        } // if
        if (marker == COMPRESSION_LZMA) {
            if (log.isInfoEnabled()) {
                log.info("close() using lzma");
            } // if
        } // if
    } // if

    ObjectOutputStream oos = new ObjectOutputStream(baseOutputStream);
    oos.writeByte(marker);
    oos.writeLong(originalBytes.length);
    oos.flush();
    OutputStream out = baseOutputStream;
    if (cipher != null) {
        out = new CipherOutputStream(out, cipher);
    } // if
    out.write(bytes);
    out.close();
    delegate = null;
    baseOutputStream = null;
}