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:gov.niem.ws.util.SecurityUtil.java

/**
 * Encode data with DEFLATE and then Base64 without chunking, so it can
 * safely be placed in an HTTP header.//from   w  ww  . ja  va 2s  .  c  o m
 * 
 * @param data
 * @return the compressed, b64 encoded data
 * @throws DataFormatException
 */
public static String encodeHeader(byte[] data) throws DataFormatException {
    // TODO: length limit on encoded?      
    ByteArrayOutputStream out = new ByteArrayOutputStream(data.length);
    Deflater deflater = new Deflater();
    deflater.setInput(data);
    deflater.finish();
    byte[] buffer = new byte[1024];
    while (!deflater.finished()) {
        int count = deflater.deflate(buffer);
        if (count == 0)
            break;
        out.write(buffer, 0, count);
    }

    try {
        deflater.end();
        out.close();
    } catch (IOException e) {
    }

    return new String(Base64.encodeBase64(out.toByteArray(), false));
}

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);/*ww  w.  j a  va2s .  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 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  w w. j a  v a  2  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[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  2s.c om*/
 * <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:com.nary.Debug.java

public static void saveClass(OutputStream _out, Object _class, boolean _compress) throws IOException {
    if (_compress) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream OOS = new ObjectOutputStream(bos);
        OOS.writeObject(_class);

        byte[] dataArray = bos.toByteArray();
        byte[] test = new byte[dataArray.length]; // this is where the byte array gets compressed to
        Deflater def = new Deflater(Deflater.BEST_COMPRESSION);
        def.setInput(dataArray);//from w  ww .ja va 2 s  .  c o  m
        def.finish();
        def.deflate(test);
        _out.write(test, 0, def.getTotalOut());
    } else {
        ObjectOutputStream OS = new ObjectOutputStream(_out);
        OS.writeObject(_class);
    }
}

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 ww  .  j ava  2s .  c o  m
    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:de.unidue.inf.is.ezdl.dlcore.utils.StringUtils.java

/**
 * Compresses a string.//from   ww  w. j a va2  s  .  co  m
 * 
 * @param s
 *            The string to compress
 * @return The compressed string
 */
public static String compress(String s) {
    ByteArrayOutputStream baos = null;
    try {
        byte[] input = s.getBytes("UTF-8");
        Deflater compresser = new Deflater();
        compresser.setLevel(Deflater.BEST_COMPRESSION);
        compresser.setInput(input);
        compresser.finish();
        baos = new ByteArrayOutputStream();
        while (!compresser.finished()) {
            byte[] output = new byte[1024];
            int compressedDataLength = compresser.deflate(output);
            baos.write(output, 0, compressedDataLength);
        }
        baos.flush();
        return Base64.encodeBase64String(baos.toByteArray());

    } catch (UnsupportedEncodingException e) {
        logger.error(e.getMessage(), e);
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    } finally {
        ClosingUtils.close(baos);
    }
    return "";
}

From source file:com.simiacryptus.text.CompressionUtil.java

/**
 * Encode lz byte [ ].//from   www. j av  a2  s  .  c o  m
 *
 * @param bytes      the bytes
 * @param dictionary the dictionary
 * @return the byte [ ]
 */
public static byte[] encodeLZ(byte[] bytes, String dictionary) {
    byte[] output = new byte[(int) (bytes.length * 1.05 + 32)];
    Deflater compresser = new Deflater();
    try {
        compresser.setInput(bytes);
        if (null != dictionary && !dictionary.isEmpty()) {
            byte[] bytes2 = dictionary.getBytes("UTF-8");
            compresser.setDictionary(bytes2);
        }
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
    compresser.finish();
    int compressedDataLength = compresser.deflate(output);
    compresser.end();
    return Arrays.copyOf(output, compressedDataLength);
}

From source file:com.android.server.wifi.WifiLogger.java

private static String compressToBase64(byte[] input) {
    String result;/*www  .j  ava 2  s. co  m*/
    //compress
    Deflater compressor = new Deflater();
    compressor.setLevel(Deflater.BEST_COMPRESSION);
    compressor.setInput(input);
    compressor.finish();
    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
    final byte[] buf = new byte[1024];

    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }

    try {
        compressor.end();
        bos.close();
    } catch (IOException e) {
        Log.e(TAG, "ByteArrayOutputStream close error");
        result = android.util.Base64.encodeToString(input, Base64.DEFAULT);
        return result;
    }

    byte[] compressed = bos.toByteArray();
    if (DBG) {
        Log.d(TAG, " length is:" + (compressed == null ? "0" : compressed.length));
    }

    //encode
    result = android.util.Base64.encodeToString(compressed.length < input.length ? compressed : input,
            Base64.DEFAULT);

    if (DBG) {
        Log.d(TAG, "FwMemoryDump length is :" + result.length());
    }

    return result;
}

From source file:com.qatickets.common.ZIPHelper.java

public static byte[] compress(byte[] data) {

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

    // Give the compressor the data to compress
    compressor.setInput(data);//from   ww  w  . j  a va2  s.  co  m
    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.
    final ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);

    // Compress the data
    byte[] buf = new byte[1024];
    while (!compressor.finished()) {
        final int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }
    try {
        bos.close();
    } catch (IOException e) {
    }

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

    compressor.end();

    return compressedData;
}