Example usage for java.util.zip Deflater end

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

Introduction

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

Prototype

public void end() 

Source Link

Document

Closes the compressor and discards any unprocessed input.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception { // main method
    // Encode a String into bytes
    String inputString = "this is a test";
    byte[] input = inputString.getBytes("UTF-8");

    // Compress the bytes
    byte[] output1 = new byte[input.length];
    Deflater compresser = new Deflater();
    compresser.setInput(input);/*w ww .  ja  va2s  . c o  m*/
    compresser.finish();
    int compressedDataLength = compresser.deflate(output1);
    compresser.end();

    String str = new String(Base64.getEncoder().encode(output1));
    System.out.println("Deflated String:" + str);

    byte[] output2 = Base64.getDecoder().decode(str);

    // Decompress the bytes
    Inflater decompresser = new Inflater();
    decompresser.setInput(output2);
    byte[] result = str.getBytes();
    int resultLength = decompresser.inflate(result);
    decompresser.end();

    // Decode the bytes into a String
    String outputString = new String(result, 0, resultLength, "UTF-8");
    System.out.println("Deflated String:" + outputString);

}

From source file:Main.java

public static byte[] compress(byte[] data, int level) throws IOException {
    if (data == null || data.length == 0) {
        return data;
    }//www  .j av a 2 s  . c o  m
    ByteArrayOutputStream bout = new ByteArrayOutputStream(data.length);
    Deflater deflater = new Deflater();
    deflater.setLevel(level);
    deflater.setInput(data);
    deflater.finish();
    byte[] buf = new byte[BUFFER_SIZE];
    while (!deflater.finished()) {
        int count = deflater.deflate(buf);
        bout.write(buf, 0, count);
    }
    deflater.end();
    bout.close();
    return bout.toByteArray();
}

From source file:Main.java

public static byte[] compressInZlib(byte[] originalData, int offset, int length) throws IOException {

    Deflater compresser = new Deflater();
    compresser.setInput(originalData, offset, length);
    compresser.finish();// w w  w . j a  v a2  s .  co  m

    ByteArrayOutputStream bos = new ByteArrayOutputStream(length);

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

    byte[] compressData = bos.toByteArray();
    bos.close();

    return compressData;
}

From source file:Main.java

public static byte[] compress(byte[] data) throws IOException {
    Deflater compresser = new Deflater(Deflater.BEST_COMPRESSION);
    compresser.setInput(data);/*from ww  w  .  jav a  2s.  co  m*/
    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, int compressionLevel, boolean GZIPFormat) throws IOException {
    Deflater compressor = new Deflater(compressionLevel, GZIPFormat);

    compressor.setInput(input);/*w  w  w .j  av  a  2 s .  co  m*/

    compressor.finish();

    ByteArrayOutputStream bao = new ByteArrayOutputStream();
    byte[] readBuffer = new byte[1024];
    int readCount = 0;

    while (!compressor.finished()) {
        readCount = compressor.deflate(readBuffer);
        if (readCount > 0) {
            bao.write(readBuffer, 0, readCount);
        }
    }

    compressor.end();
    return bao.toByteArray();
}

From source file:Main.java

private static byte[] compress(byte[] data) throws IOException {
    Deflater deflater = new Deflater();
    deflater.setInput(data);/*  w  w w .  j  a  va2 s  .  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.apache.flex.compiler.internal.embedding.transcoders.JPEGTranscoder.java

public static byte[] deflate(byte[] buf) throws IOException {
    Deflater deflater = new Deflater(Deflater.BEST_SPEED);
    DAByteArrayOutputStream out = new DAByteArrayOutputStream();
    DeflaterOutputStream deflaterStream = new DeflaterOutputStream(out, deflater);
    try {/*from  w  ww .j av a2 s  .  co  m*/
        deflaterStream.write(buf, 0, buf.length);
        deflaterStream.finish();
        deflater.end();
    } finally {
        IOUtils.closeQuietly(deflaterStream);
    }
    return out.getDirectByteArray();
}

From source file:org.atricore.idbus.capabilities.sso.main.binding.SamlR2HttpRedirectBinding.java

public static String deflateForRedirect(String redirStr, boolean encode) {

    int n = redirStr.length();
    byte[] redirIs = null;
    try {//from w  w  w  .java2  s. c o  m
        redirIs = redirStr.getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }

    byte[] deflated = new byte[n];

    Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
    deflater.setInput(redirIs);
    deflater.finish();
    int len = deflater.deflate(deflated);
    deflater.end();

    byte[] exact = new byte[len];

    System.arraycopy(deflated, 0, exact, 0, len);

    if (encode) {
        byte[] base64Str = new Base64().encode(exact);
        return new String(base64Str);
    }

    return new String(exact);
}

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  .ja v a2s  . c  o m*/
        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:Main.java

public static byte[] compress(byte[] data) {
    Deflater deflater = new Deflater();
    deflater.setInput(data);//from ww w  .j ava 2  s.  co  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();
    }

}