Example usage for java.util.zip Deflater setDictionary

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

Introduction

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

Prototype

public void setDictionary(ByteBuffer dictionary) 

Source Link

Document

Sets preset dictionary for compression.

Usage

From source file:Main.java

/**
 * DEFLATEs the specified input data./*from w  ww  . java  2s .  c  o  m*/
 * 
 * @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:com.simiacryptus.text.CompressionUtil.java

/**
 * Encode lz byte [ ]./*from w  ww  .  j  a v a2s.com*/
 *
 * @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);
}