Example usage for java.util.zip Inflater setDictionary

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

Introduction

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

Prototype

public void setDictionary(ByteBuffer dictionary) 

Source Link

Document

Sets the preset dictionary to the bytes in the given buffer.

Usage

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

/**
 * Decode lz byte [ ]./* w w  w . jav a2s  . com*/
 *
 * @param data       the data
 * @param dictionary the dictionary
 * @return the byte [ ]
 */
public static byte[] decodeLZ(byte[] data, String dictionary) {
    try {
        Inflater decompresser = new Inflater();
        decompresser.setInput(data, 0, data.length);
        byte[] result = new byte[data.length * 32];
        int resultLength = 0;
        if (!dictionary.isEmpty()) {
            resultLength = decompresser.inflate(result);
            assert (0 == resultLength);
            if (decompresser.needsDictionary()) {
                byte[] bytes = dictionary.getBytes("UTF-8");
                decompresser.setDictionary(bytes);
            }
        }
        resultLength = decompresser.inflate(result);
        decompresser.end();
        return Arrays.copyOfRange(result, 0, resultLength);
    } catch (DataFormatException | UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}