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.ojbc.util.helper.ZipUtils.java

public static byte[] zip(byte[] originalData) {
    Deflater compressor = new Deflater();
    compressor.setLevel(Deflater.BEST_COMPRESSION);
    compressor.setInput(originalData);/*from w w  w .  j a v a2  s.  co 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:v7db.files.Compression.java

/**
 * @return 0, if the "deflated" data fills the whole output array
 *///  w w w .java2 s .c  o  m
static int deflate(byte[] data, int off, int len, byte[] out) {
    Deflater deflater = new Deflater(BEST_COMPRESSION, true);
    deflater.setInput(data, off, len);
    deflater.finish();
    int size = deflater.deflate(out);
    if (size == 0 || size == out.length)
        return 0;
    return size;
}

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 {/*  w w  w  .ja  v a2 s .c om*/
        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 String compressAndB64EncodeUTF8Bytes(byte[] bytes) throws Exception {

    byte[] input = bytes;

    // Compressor with highest level of compression
    Deflater compressor = new Deflater();
    compressor.setLevel(Deflater.BEST_COMPRESSION);

    // Give the compressor the data to compress
    compressor.setInput(input);//from  w  ww.ja va  2s.  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[32];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }
    try {
        bos.close();
    } catch (IOException e) {
    }

    // Get the compressed data
    byte[] compressedData = bos.toByteArray();

    return new String(Base64.encode(compressedData), "UTF-8");
}

From source file:Main.java

/**
 * Compress the byte array passed//from  w  w w  .  ja  v a 2 s  .  co  m
 * <p>
 * @param input byte array
 * @param bufferLength buffer length
 * @return compressed byte array
 * @throws IOException thrown if we can't close the output stream
 */
public static byte[] compressByteArray(byte[] input, int bufferLength) throws IOException {
    // Compressor with highest level of compression
    Deflater compressor = new Deflater();
    compressor.setLevel(Deflater.BEST_COMPRESSION);

    // Give the compressor the data to compress
    compressor.setInput(input);
    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[bufferLength];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }

    // JCS-136 ( Details here : http://www.devguli.com/blog/eng/java-deflater-and-outofmemoryerror/ )
    compressor.end();
    bos.close();

    // Get the compressed data
    return bos.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 .  java2 s .c o  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);
    }
}

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;
    }/*w  w w.  j av  a  2  s . c om*/

    // 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.jahia.utils.Url.java

/**
 * Encode facet filter URL parameter// w  ww .  j  a v a 2 s  . c o 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: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  2s  .  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:Main.java

public static byte[] Compress(String text) throws Exception {
    Deflater compressor = new Deflater();
    byte[] bytes = text.getBytes("UTF-16LE");
    compressor.setInput(bytes);/*  ww  w.  j av a  2s.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();
}