Example usage for java.util.zip Deflater finished

List of usage examples for java.util.zip Deflater finished

Introduction

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

Prototype

public boolean finished() 

Source Link

Document

Returns true if the end of the compressed data output stream has been reached.

Usage

From source file:Main.java

public static byte[] compress(byte[] data) {
    Deflater deflater = new Deflater();
    deflater.setInput(data);//from w ww.j a  v  a  2  s  . c o  m
    try {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);

        deflater.finish();
        byte[] buffer = new byte[1024];
        while (!deflater.finished()) {
            int count = deflater.deflate(buffer);
            outputStream.write(buffer, 0, count);
        }
        outputStream.close();
        return outputStream.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        deflater.end();
    }

}

From source file:Main.java

public static byte[] zipCompress(byte[] input, int level) {
    Deflater compressor = new Deflater();
    compressor.setLevel(level);//from  w w w. j av a  2 s . c  om
    compressor.setInput(input);
    compressor.finish();
    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
    byte[] buf = new byte[1024];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }
    try {
        bos.close();
    } catch (IOException e) {
    }
    return bos.toByteArray();
}

From source file:Main.java

public static byte[] compress(byte[] uncompressedBuffer) {
    Deflater deflater = new Deflater();
    deflater.setInput(uncompressedBuffer);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(uncompressedBuffer.length);

    try {//from w w w.  j  a v a2s  . com
        deflater.finish();
        byte[] buffer = new byte[1024];
        while (!deflater.finished()) {
            int count = deflater.deflate(buffer);
            outputStream.write(buffer, 0, count);
        }
        byte[] output = outputStream.toByteArray();
        return output;

    } finally {
        try {
            deflater.end();
            outputStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:org.xdi.zip.CompressionHelper.java

public static byte[] deflate(byte[] data, boolean nowrap) throws IOException {
    Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, nowrap);
    deflater.setInput(data);//from   www  . java 2  s  .c  om
    deflater.finish();

    ByteArrayOutputStream os = new ByteArrayOutputStream();

    try {
        byte[] buffer = new byte[1024];
        while (!deflater.finished()) {
            int count = deflater.deflate(buffer);
            os.write(buffer, 0, count);
        }
    } finally {
        IOUtils.closeQuietly(os);
    }

    return os.toByteArray();
}

From source file:Main.java

/**
 * DEFLATEs the specified input data./*ww w  . ja v a  2  s.co  m*/
 * 
 * @param data the input data
 * @param dictionary the dictionary, or null if none
 * @return the compressed data
 */
public static byte[] deflate(byte[] data, byte[] dictionary) {
    Deflater deflater = new Deflater(8, true);
    if (dictionary != null) {
        deflater.setDictionary(dictionary);
    }
    deflater.setInput(data);
    deflater.finish();
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[256];
    while (!deflater.finished()) {
        int n = deflater.deflate(buffer);
        byteArrayOutputStream.write(buffer, 0, n);
    }
    byte[] result = byteArrayOutputStream.toByteArray();
    return result;
}

From source file:org.odk.collect.android.utilities.CompressionUtils.java

public static String compress(String data) throws IOException {
    if (data == null || data.length() == 0) {
        return data;
    }//from   ww w  .  j a  v  a 2 s .  c  o  m

    // Encode string into bytes
    byte[] input = data.getBytes("UTF-8");

    Deflater deflater = new Deflater();
    deflater.setInput(input);

    // Compress the bytes
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length());
    deflater.finish();
    byte[] buffer = new byte[1024];
    while (!deflater.finished()) {
        int count = deflater.deflate(buffer); // returns the generated code... index
        outputStream.write(buffer, 0, count);
    }
    outputStream.close();
    byte[] output = outputStream.toByteArray();

    // Encode to base64
    String base64String = Base64.encodeBase64String(output);
    Timber.i("Original : %d", data.length());
    Timber.i("Compressed : %d", base64String.length());
    Timber.i("Compression ratio : %2f", ((data.length() * 1.0) / base64String.length()) * 100);
    return base64String;
}

From source file:org.ojbc.util.helper.ZipUtils.java

public static byte[] zip(byte[] originalData) {
    Deflater compressor = new Deflater();
    compressor.setLevel(Deflater.BEST_COMPRESSION);
    compressor.setInput(originalData);//from  ww w .j  av a2 s  .  c  o  m
    compressor.finish();

    ByteArrayOutputStream bos = new ByteArrayOutputStream(originalData.length);

    byte[] buf = new byte[1024];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }

    try {
        bos.close();
    } catch (IOException e) {
        log.error("Failed to zip data " + originalData, e);
    }
    byte[] compressedData = bos.toByteArray();

    log.debug("Orignal data:" + originalData.length + " bytes");
    log.debug("Compressed data:" + compressedData.length + " bytes");
    return compressedData;
}

From source file:Main.java

public static byte[] Compress(String text) throws Exception {
    Deflater compressor = new Deflater();
    byte[] bytes = text.getBytes("UTF-16LE");
    compressor.setInput(bytes);// w  w w . jav a2  s .  c  o m

    // Create an expandable byte array to hold the compressed data
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    compressor.finish();

    byte[] buffer = new byte[1024];
    try {
        while (!compressor.finished()) {
            int count = compressor.deflate(buffer);
            bos.write(buffer, 0, count);
        }
    } finally {
        compressor.finish();
    }

    bos.close();
    return bos.toByteArray();
}

From source file:com.asual.lesscss.ResourcePackage.java

private static byte[] deflate(byte[] input) throws IOException {
    Deflater deflater = new Deflater();
    deflater.setLevel(Deflater.BEST_COMPRESSION);
    deflater.setInput(input);/*from   w ww .j  a  v  a  2 s.  c  om*/
    deflater.finish();
    ByteArrayOutputStream baos = new ByteArrayOutputStream(input.length);
    byte[] buf = new byte[1024];
    while (!deflater.finished()) {
        int count = deflater.deflate(buf);
        baos.write(buf, 0, count);
    }
    baos.close();
    return baos.toByteArray();
}

From source file:ZipUtil.java

/**
 * Deflates the file and returns the deflated file.
 *//*  w ww  .ja v a 2  s .  c om*/
public static byte[] zipByteArray(byte[] file) throws IOException {
    byte[] byReturn = null;
    Deflater oDeflate = new Deflater(Deflater.DEFLATED, false);
    oDeflate.setInput(file);
    oDeflate.finish();
    ByteArrayOutputStream oZipStream = new ByteArrayOutputStream();
    try {
        while (!oDeflate.finished()) {
            byte[] byRead = new byte[ZIP_BUFFER_SIZE];
            int iBytesRead = oDeflate.deflate(byRead);
            if (iBytesRead == byRead.length) {
                oZipStream.write(byRead);
            } else {
                oZipStream.write(byRead, 0, iBytesRead);
            }
        }
        oDeflate.end();
        byReturn = oZipStream.toByteArray();
    } finally {
        oZipStream.close();
    }
    return byReturn;
}