Example usage for java.util.zip Deflater deflate

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

Introduction

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

Prototype

public int deflate(ByteBuffer output) 

Source Link

Document

Compresses the input data and fills specified buffer with compressed data.

Usage

From source file:Main.java

private static byte[] compressBytesInflateDeflate(byte[] inBytes) {
    Deflater deflater = new Deflater(Deflater.BEST_SPEED);
    deflater.setInput(inBytes);//  ww  w. j  av  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[] input, int compressionLevel, boolean GZIPFormat) throws IOException {
    Deflater compressor = new Deflater(compressionLevel, GZIPFormat);

    compressor.setInput(input);// w w w.j av  a2 s  .c om

    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

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

    Deflater compresser = new Deflater();
    compresser.setInput(originalData, offset, length);
    compresser.finish();//from  www  . j ava  2  s.  c  om

    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   w w  w  .  ja  va  2s .  c o 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[] data) {
    Deflater deflater = new Deflater();
    deflater.setInput(data);/*w  ww . jav  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);/* ww w .j a v a2 s.c  o  m*/
    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   ww w . j  av  a2  s. 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:org.jahia.utils.Url.java

/**
 * Encode facet filter URL parameter//w ww. ja v  a2s. co  m
 * @param inputString facet filter parameter
 * @return filter encoded for URL query parameter usage
 */

public static String encodeUrlParam(String inputString) {
    if (StringUtils.isEmpty(inputString)) {
        return inputString;
    }
    // Compress the bytes
    byte[] output = new byte[2048];
    Deflater compresser = new Deflater();
    try {
        compresser.setInput(inputString.getBytes("UTF-8"));
        compresser.finish();
        int compressedDataLength = compresser.deflate(output);
        byte[] copy = new byte[compressedDataLength];
        System.arraycopy(output, 0, copy, 0, Math.min(output.length, compressedDataLength));
        return Base64.encodeBase64URLSafeString(copy);
    } catch (UnsupportedEncodingException e) {
        logger.warn("Not able to encode facet URL: " + inputString, e);
    }

    return inputString;
}

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  . j  a va  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:org.jasig.cas.util.CompressionUtils.java

/**
 * Deflate the given string via a {@link java.util.zip.Deflater}.
 * The result will be base64 encoded with {@link #UTF8_ENCODING}.
 *
 * @param data the data//  w  w  w.  j  av  a 2 s  .  co m
 * @return base64 encoded string
 */
public static String deflate(final String data) {
    try {
        final Deflater deflater = new Deflater();
        deflater.setInput(data.getBytes(UTF8_ENCODING));
        deflater.finish();
        final byte[] buffer = new byte[data.length()];
        final int resultSize = deflater.deflate(buffer);
        final byte[] output = new byte[resultSize];
        System.arraycopy(buffer, 0, output, 0, resultSize);
        return encodeBase64(output);
    } catch (final UnsupportedEncodingException e) {
        throw new RuntimeException("Cannot find encoding:" + UTF8_ENCODING, e);
    }
}