Example usage for java.util.zip Deflater finish

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

Introduction

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

Prototype

boolean finish

To view the source code for java.util.zip Deflater finish.

Click Source Link

Usage

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);/*  w  w  w.j  a  v  a2 s . c o m*/
    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

public static byte[] compress(byte[] data) throws IOException {
    Deflater deflater = new Deflater();
    deflater.setInput(data);/*from w  w  w  .jav a2 s .co  m*/

    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();

    System.out.println("Original: " + data.length / 1024 + " Kb");
    System.out.println("Compressed: " + output.length / 1024 + " Kb");
    return output;
}

From source file:Main.java

private static byte[] compress(byte[] data) throws IOException {
    Deflater deflater = new Deflater();
    deflater.setInput(data);//from  ww w.  j  a  v a2s  .c  o m

    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();
    deflater.end();

    // System.out.println("Original: " + data.length + " bytes.");
    // System.out.println("Compressed: " + output.length + " bytes.");
    return output;
}

From source file:org.samlsnort.util.EncodingTool.java

public static String deflateToBase64(String inflated) {

    Deflater deflater = new Deflater(Deflater.DEFLATED, true);
    deflater.setInput(inflated.getBytes(CHARSET));
    deflater.finish();
    byte[] deflatedBytes = new byte[2048];
    int len = deflater.deflate(deflatedBytes);
    return new String(Base64.encodeBase64(Arrays.copyOf(deflatedBytes, len)), CHARSET);
}

From source file:Main.java

/**
 * DEFLATEs the specified input data.//w w w  . j a v  a 2s . c  om
 * 
 * @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:Main.java

public static byte[] compressZLIB(byte[] input) throws IOException {
    // Create the compressor with highest level of compression
    Deflater compressor = new Deflater();
    compressor.setLevel(Deflater.BEST_SPEED);
    // Give the compressor the data to compress
    compressor.setInput(input);//from  w  w  w  .jav  a2s .c om
    compressor.finish();
    // Create an expandable byte array to hold the compressed data.
    // You cannot use an array that's the same size as the orginal because
    // there is no guarantee that the compressed data will be smaller than
    // the uncompressed data.
    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
    // Compress the data
    byte[] buf = new byte[1024];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }
    bos.close();
    // Get the compressed data
    byte[] compressedData = bos.toByteArray();
    return compressedData;
}

From source file:Main.java

private static byte[] compressBytesInflateDeflate(byte[] inBytes) {
    Deflater deflater = new Deflater(Deflater.BEST_SPEED);
    deflater.setInput(inBytes);/* ww  w. j  a  v a2 s  .co  m*/
    ByteArrayOutputStream bos = new ByteArrayOutputStream(inBytes.length);
    deflater.finish();
    byte[] buffer = new byte[1024 * 8];
    while (!deflater.finished()) {
        int count = deflater.deflate(buffer);
        bos.write(buffer, 0, count);
    }
    byte[] output = bos.toByteArray();
    return output;
}

From source file:Main.java

public static byte[] compress(byte[] data) throws IOException {
    Deflater compresser = new Deflater(Deflater.BEST_COMPRESSION);
    compresser.setInput(data);/*from  w w w  .  ja va2 s.com*/
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    compresser.finish();
    byte[] buffer = new byte[1024];
    while (!compresser.finished()) {
        int count = compresser.deflate(buffer);
        outputStream.write(buffer, 0, count);
    }
    compresser.end();
    outputStream.close();
    return outputStream.toByteArray();
}

From source file:Main.java

public static byte[] compress(byte[] input) {
    if (input == null || input.length == 0)
        return input;
    Deflater compressor = new Deflater();
    compressor.setLevel(Deflater.BEST_COMPRESSION);

    // Give the compressor the data to compress
    compressor.setInput(input);/*  w  w w.j ava2 s .  c  o  m*/
    compressor.finish();

    // Create an expandable byte array to hold the compressed data.
    // It is not necessary that the compressed data will be smaller than
    // the uncompressed data.
    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
    // Compress the data
    byte[] buf = new byte[1024];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }

    // Get the compressed data
    return bos.toByteArray();
}

From source file:Main.java

public static byte[] compress(byte[] data) {
    Deflater deflater = new Deflater();
    deflater.setInput(data);/*from  w w  w. ja  va 2s .  com*/
    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();
    }

}