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:com.bigdata.dastor.utils.FBUtilities.java

public static void compressToStream(byte[] input, ByteArrayOutputStream bos) throws IOException {
    // Create the 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. ja v  a  2s  .c  om
    compressor.finish();

    // Write the compressed data to the stream
    byte[] buf = new byte[1024];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }
}

From source file:org.ow2.proactive.utils.ObjectByteConverter.java

/**
 * Convert the given Serializable Object into a byte array.
 * <p>//from   w w  w .j av  a 2  s .com
 * The returned byteArray can be compressed by setting compress boolean argument value to <code>true</code>.
 * 
 * @param obj the Serializable object to be compressed
 * @param compress true if the returned byteArray must be also compressed, false if no compression is required.
 * @return a compressed (or not) byteArray representing the Serialization of the given object.
 * @throws IOException if an I/O exception occurs when writing the output byte array
 */
public static final byte[] objectToByteArray(Object obj, boolean compress) throws IOException {
    ByteArrayOutputStream baos = null;
    ObjectOutputStream oos = null;
    if (obj == null) {
        return null;
    }
    try {
        baos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(baos);
        oos.writeObject(obj);
        oos.flush();
        if (!compress) {
            // Return the UNCOMPRESSED data
            return baos.toByteArray();
        } else {
            // Compressor with highest level of compression
            Deflater compressor = new Deflater();
            compressor.setLevel(Deflater.BEST_COMPRESSION);
            // Give the compressor the data to compress
            compressor.setInput(baos.toByteArray());
            compressor.finish();

            ByteArrayOutputStream bos = null;
            try {
                // Create an expandable byte array to hold the compressed data.
                bos = new ByteArrayOutputStream();
                // Compress the data
                byte[] buf = new byte[512];
                while (!compressor.finished()) {
                    int count = compressor.deflate(buf);
                    bos.write(buf, 0, count);
                }
                // Return the COMPRESSED data
                return bos.toByteArray();
            } finally {
                if (bos != null) {
                    bos.close();
                }
            }
        }
    } finally {
        if (oos != null) {
            oos.close();
        }
        if (baos != null) {
            baos.close();
        }
    }
}

From source file:edu.stanford.junction.addon.JSONObjWrapper.java

private static String compressString(String str) {
    byte[] input;
    try {/*from   w  ww  .  j a v a 2s . c  om*/
        input = str.getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
        input = str.getBytes();
    }
    // Create the 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. 
    // 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);
    }
    try {
        bos.close();
    } catch (IOException e) {
    }
    // Get the compressed data 
    byte[] compressedData = bos.toByteArray();
    return Base64.encodeBytes(compressedData);
}

From source file:org.apache.marmotta.kiwi.io.KiWiIO.java

/**
 * Write a string to the data output. In case the string length exceeds LITERAL_COMPRESS_LENGTH, uses a LZW
 * compressed format, otherwise writes the plain bytes.
 *
 * @param out      output destination to write to
 * @param content  string to write/*from   www  .  j  a v  a 2s.co m*/
 * @throws IOException
 */
private static void writeContent(DataOutput out, String content) throws IOException {
    if (content.length() > LITERAL_COMPRESS_LENGTH) {
        // temporary buffer of the size of bytes in the content string (assuming that the compressed data will fit into it)
        byte[] data = content.getBytes("UTF-8");
        byte[] buffer = new byte[data.length];

        Deflater compressor = new Deflater(Deflater.BEST_COMPRESSION, true);
        compressor.setInput(data);
        compressor.finish();

        int length = compressor.deflate(buffer);

        // only use compressed version if it is smaller than the number of bytes used by the string
        if (length < buffer.length) {
            log.debug("compressed string with {} bytes; compression ratio {}", data.length,
                    (double) length / data.length);

            out.writeByte(MODE_COMPRESSED);
            out.writeInt(data.length);
            out.writeInt(length);
            out.write(buffer, 0, length);
        } else {
            log.warn("compressed length exceeds string buffer: {} > {}", length, buffer.length);

            out.writeByte(MODE_DEFAULT);
            DataIO.writeString(out, content);
        }

        compressor.end();
    } else {
        out.writeByte(MODE_DEFAULT);
        DataIO.writeString(out, content);
    }
}

From source file:NCDSearch.DistributedNCDSearch.java

public static float NCD(byte[] file, byte[] target, int compression) {

    Deflater compressor = new Deflater(compression);

    //This is where we dump our compressed bytes.  All we need is the size of this thing.  
    //TODO: In theory the compressed bytes could exceed the length of the target files...
    byte[] outputtrash = new byte[file.length + target.length];

    int bothcompressedsize;
    int filecompressedsize;
    int targetcompressedsize;

    //puts the target file and the searched file together.
    byte[] both = new byte[file.length + target.length];
    for (int i = 0; i < file.length; i++) {
        both[i] = file[i];//from ww w  . j a v  a 2 s  .c  o m
    }
    for (int i = 0; i < target.length; i++) {
        both[i + file.length] = target[i];
    }

    compressor.setInput(file);
    compressor.finish();
    filecompressedsize = compressor.deflate(outputtrash);
    compressor.reset();
    compressor.setInput(target);
    compressor.finish();
    targetcompressedsize = compressor.deflate(outputtrash);
    compressor.reset();
    compressor.setInput(both);
    compressor.finish();
    bothcompressedsize = compressor.deflate(outputtrash);
    compressor.reset();

    return (float) (bothcompressedsize - filecompressedsize) / (float) targetcompressedsize;
}

From source file:com.kactech.otj.Utils.java

public static byte[] zlibCompress(byte[] data) {
    Deflater deflater = new Deflater();
    deflater.setInput(data);//ww w. j  a v a2 s .co m

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);

    deflater.finish();
    byte[] buffer = new byte[1024];
    while (!deflater.finished()) {
        int count = deflater.deflate(buffer);
        outputStream.write(buffer, 0, count);
    }
    try {
        outputStream.close();
    } catch (IOException e) {
        // don't be silly
        throw new RuntimeException(e);
    }
    return outputStream.toByteArray();
}

From source file:org.apache.geode.management.internal.cli.CliUtil.java

public static DeflaterInflaterData compressBytes(byte[] input) {
    Deflater compresser = new Deflater();
    compresser.setInput(input);/*from  w w  w.j  ava 2s  .  c  o m*/
    compresser.finish();
    byte[] buffer = new byte[100];
    byte[] result = new byte[0];
    int compressedDataLength = 0;
    int totalCompressedDataLength = 0;
    do {
        byte[] newResult = new byte[result.length + buffer.length];
        System.arraycopy(result, 0, newResult, 0, result.length);

        compressedDataLength = compresser.deflate(buffer);
        totalCompressedDataLength += compressedDataLength;
        System.arraycopy(buffer, 0, newResult, result.length, buffer.length);
        result = newResult;
    } while (compressedDataLength != 0);
    return new DeflaterInflaterData(totalCompressedDataLength, result);
}

From source file:fr.eoit.util.dumper.DumperTest.java

@Test
public void testCompression() {

    byte[] strBytes = COPYRIGHTS.getBytes();

    byte[] output = new byte[8096];
    Deflater compresser = new Deflater(Deflater.BEST_COMPRESSION, true);
    compresser.setInput(strBytes);/*from  ww w  .  j a v  a  2  s . c o m*/
    compresser.finish();
    int compressedDataLength = compresser.deflate(output);
    compresser.end();

    String inputString = new String(Hex.encodeHex(strBytes));
    String hexString = new String(Arrays.copyOf(output, compressedDataLength));

    int i = 0;
    i++;
}

From source file:acp.sdk.SecureUtil.java

/**
 * .//w ww.j  a  va 2  s.  c  o m
 * 
 * @param inputByte
 *            ?byte[]
 * @return ??
 * @throws IOException
 */
public static byte[] deflater(final byte[] inputByte) throws IOException {
    int compressedDataLength = 0;
    Deflater compresser = new Deflater();
    compresser.setInput(inputByte);
    compresser.finish();
    ByteArrayOutputStream o = new ByteArrayOutputStream(inputByte.length);
    byte[] result = new byte[1024];
    try {
        while (!compresser.finished()) {
            compressedDataLength = compresser.deflate(result);
            o.write(result, 0, compressedDataLength);
        }
    } finally {
        o.close();
    }
    compresser.end();
    return o.toByteArray();
}

From source file:com.moesol.keys.EncodeUidsTest.java

public void testCompress() {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 10000; i++) {
        UUID id = UUID.randomUUID();
        sb.append(id);/*from   ww w  .  j av a 2 s . co m*/
        sb.append(',');
    }
    String result = sb.toString();
    //      System.out.println("val=" + result);

    Deflater deflate = new Deflater();
    try {
        byte[] compressed = new byte[512000];
        deflate.setInput(result.getBytes());
        deflate.finish();
        System.out.printf("in=%d out=%d%n", deflate.getBytesRead(), deflate.getBytesWritten());
        deflate.deflate(compressed);
        System.out.printf("in=%d out=%d%n", deflate.getBytesRead(), deflate.getBytesWritten());
    } finally {
        deflate.end();
    }
}