Example usage for java.util.zip Deflater setInput

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

Introduction

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

Prototype

public void setInput(ByteBuffer input) 

Source Link

Document

Sets input data for compression.

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);
    compressor.finish();/*from w  ww  . ja v  a2  s .c  o m*/

    // 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.apache.geode.management.internal.cli.CliUtil.java

public static DeflaterInflaterData compressBytes(byte[] input) {
    Deflater compresser = new Deflater();
    compresser.setInput(input);
    compresser.finish();/*from   w  w  w . j a  va 2s  . c  o  m*/
    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:com.kactech.otj.Utils.java

public static byte[] zlibCompress(byte[] data) {
    Deflater deflater = new Deflater();
    deflater.setInput(data);

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);

    deflater.finish();//from   ww w .  j a va 2s .com
    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.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/*  www . j  av a 2s .  c  o  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  va  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:acp.sdk.SecureUtil.java

/**
 * .//from ww  w  . j av a2  s.  co  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:nl.nn.adapterframework.util.Misc.java

public static byte[] compress(byte[] input) 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);
    compressor.finish();//from  www .j a va  2s .c o m

    // 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
    return bos.toByteArray();
}

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);
    compresser.finish();/*  w  w w.  j ava 2s.c o m*/
    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:com.ctriposs.r2.filter.compression.DeflateCompressor.java

@Override
public byte[] deflate(InputStream data) throws CompressionException {
    byte[] input;
    try {/*from   ww w .j av  a 2s . c  om*/
        input = IOUtils.toByteArray(data);
    } catch (IOException e) {
        throw new CompressionException(CompressionConstants.DECODING_ERROR + CompressionConstants.BAD_STREAM,
                e);
    }

    Deflater zlib = new Deflater();
    zlib.setInput(input);
    zlib.finish();

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    byte[] temp = new byte[CompressionConstants.BUFFER_SIZE];

    int bytesRead;
    while (!zlib.finished()) {
        bytesRead = zlib.deflate(temp);

        if (bytesRead == 0) {
            if (!zlib.needsInput()) {
                throw new CompressionException(CompressionConstants.DECODING_ERROR + getContentEncodingName());
            } else {
                break;
            }
        }
        output.write(temp, 0, bytesRead);
    }
    zlib.end();

    return output.toByteArray();
}

From source file:org.hyperic.hq.livedata.agent.commands.LiveData_result.java

private String compress(String s) throws IOException {
    Deflater compressor = new Deflater();
    compressor.setInput(s.getBytes("UTF-8"));
    compressor.finish();//from  ww w  . j  a va  2s . c  o  m

    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    byte[] buf = new byte[1024];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }

    bos.close();

    byte[] compressedData = bos.toByteArray();
    return Base64.encode(compressedData);
}