Java Deflate Byte Array deflate(byte[] data, byte[] dictionary)

Here you can find the source of deflate(byte[] data, byte[] dictionary)

Description

DEFLATEs the specified input data.

License

Open Source License

Parameter

Parameter Description
data the input data
dictionary the dictionary, or null if none

Return

the compressed data

Declaration

public static byte[] deflate(byte[] data, byte[] dictionary) 

Method Source Code

//package com.java2s;
/**/*from w w w.j  a  va  2 s  . co  m*/
 * Copyright (c) 2010 Martin Geisse
 *
 * This file is distributed under the terms of the MIT license.
 */

import java.io.ByteArrayOutputStream;

import java.util.zip.Deflater;

public class Main {
    /**
     * DEFLATEs the specified input data.
     * 
     * @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;
    }
}

Related

  1. deflate(byte[] buf)
  2. deflate(byte[] bytes)
  3. deflate(byte[] data)
  4. deflate(byte[] data)
  5. deflate(byte[] data)
  6. deflate(byte[] in)
  7. deflate(byte[] input)
  8. deflate(final byte[] pInput)
  9. deflate(String inString)