Example usage for java.util.zip Deflater Deflater

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

Introduction

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

Prototype

public Deflater(int level) 

Source Link

Document

Creates a new compressor using the specified compression level.

Usage

From source file:Main.java

private static byte[] compressBytesInflateDeflate(byte[] inBytes) {
    Deflater deflater = new Deflater(Deflater.BEST_SPEED);
    deflater.setInput(inBytes);/* w  ww. jav a  2  s.  c  o  m*/
    ByteArrayOutputStream bos = new ByteArrayOutputStream(inBytes.length);
    deflater.finish();
    byte[] buffer = new byte[1024 * 8];
    while (!deflater.finished()) {
        int count = deflater.deflate(buffer);
        bos.write(buffer, 0, count);
    }
    byte[] output = bos.toByteArray();
    return output;
}

From source file:Main.java

public static byte[] compress(byte[] data) throws IOException {
    Deflater compresser = new Deflater(Deflater.BEST_COMPRESSION);
    compresser.setInput(data);//from   w  w w.  j  av a2  s.com
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    compresser.finish();
    byte[] buffer = new byte[1024];
    while (!compresser.finished()) {
        int count = compresser.deflate(buffer);
        outputStream.write(buffer, 0, count);
    }
    compresser.end();
    outputStream.close();
    return outputStream.toByteArray();
}

From source file:zipB64.java

protected static String encodeMessage(String messageStr) {
    try {/* w w w.j a v  a2  s.  c  o  m*/
        ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
        Deflater deflater = new Deflater(Deflater.DEFLATED);
        DeflaterOutputStream deflaterStream = new DeflaterOutputStream(bytesOut, deflater);
        deflaterStream.write(messageStr.getBytes("UTF-8"));
        deflaterStream.finish();

        Base64 b = new Base64(-1);
        return new String(b.encode(bytesOut.toByteArray()));
    } catch (Exception e) {
        return "crotte";
    }
}

From source file:org.apache.pig.impl.util.ObjectSerializer.java

public static String serialize(Serializable obj) throws IOException {
    if (obj == null)
        return "";
    try {/*from www .  j  ava 2  s.  com*/
        ByteArrayOutputStream serialObj = new ByteArrayOutputStream();
        Deflater def = new Deflater(Deflater.BEST_COMPRESSION);
        ObjectOutputStream objStream = new ObjectOutputStream(new DeflaterOutputStream(serialObj, def));
        objStream.writeObject(obj);
        objStream.close();
        return encodeBytes(serialObj.toByteArray());
    } catch (Exception e) {
        throw new IOException("Serialization error: " + e.getMessage(), e);
    }
}

From source file:rascal.storage.loose.LooseStorageNodeWritableChannel.java

private void initTempObjectFileChannel() throws IOException {
    Deflater deflater = new Deflater(storageConfiguration.getLooseCompressionLevel());
    DeflaterOutputStream output = new DeflaterOutputStream(new FileOutputStream(tempObjectFile), deflater);
    tempObjectFileChannel = Channels.newChannel(output);
}

From source file:rascal.storage.loose.AbstractLooseStorageNodeChannelIntegrationTest.java

@Before
public void setUpTestData() throws IOException {
    testData = RandomTestDataUtils.createRandomData();
    ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
    DeflaterOutputStream out = new DeflaterOutputStream(outBuffer, new Deflater(DEFLATED_DATA_COMPRESS_LEVER));
    out.write(testData);/*from   w  ww  . ja v  a2 s .com*/
    out.close();
    deflatedTestData = outBuffer.toByteArray();
}

From source file:org.apache.tez.common.TezUtils.java

/**
 * Convert a Configuration to compressed ByteString using Protocol buffer
 *
 * @param conf/*from  w w  w. j  a  v  a  2  s .  co m*/
 *          : Configuration to be converted
 * @return PB ByteString (compressed)
 * @throws java.io.IOException
 */
public static ByteString createByteStringFromConf(Configuration conf) throws IOException {
    Preconditions.checkNotNull(conf, "Configuration must be specified");
    ByteString.Output os = ByteString.newOutput();
    DeflaterOutputStream compressOs = new DeflaterOutputStream(os, new Deflater(Deflater.BEST_SPEED));
    try {
        writeConfInPB(compressOs, conf);
    } finally {
        if (compressOs != null) {
            compressOs.close();
        }
    }
    return os.toByteString();
}

From source file:com.uber.hoodie.common.HoodieJsonPayload.java

private byte[] compressData(String jsonData) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
    DeflaterOutputStream dos = new DeflaterOutputStream(baos, deflater, true);
    try {/*  w w  w.  ja  v a2s . c  o  m*/
        dos.write(jsonData.getBytes());
    } finally {
        dos.flush();
        dos.close();
        // Its important to call this.
        // Deflater takes off-heap native memory and does not release until GC kicks in
        deflater.end();
    }
    return baos.toByteArray();
}

From source file:NCDSearch.NCDSearch.java

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

    Deflater compressor = new Deflater(compression);

    byte[] outputtrash = new byte[file.length + target.length];

    int bothcompressedsize;
    int filecompressedsize;
    int targetcompressedsize;

    byte[] both = new byte[file.length + target.length];
    for (int i = 0; i < file.length; i++) {
        both[i] = file[i];/*www  .j  ava2s.com*/
    }
    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 (double) (bothcompressedsize - filecompressedsize) / (double) targetcompressedsize;
}

From source file:org.fastcatsearch.ir.document.DocumentWriter.java

public DocumentWriter(SchemaSetting schemaSetting, File dir, IndexConfig indexConfig)
        throws IOException, IRException {

    compressor = new Deflater(Deflater.BEST_SPEED);
    fields = schemaSetting.getFieldSettingList();

    docOutput = new BufferedFileOutput(dir, IndexFileNames.docStored);
    positionOutput = new BufferedFileOutput(dir, IndexFileNames.docPosition);

    fbaos = new BytesDataOutput(3 * 1024 * 1024); // 3Mb .
    workingBuffer = new byte[1024];
    docOutput.writeInt(0); // document count

    inflaterOutput = new ByteRefArrayOutputStream(INFLATE_BUFFER_INIT_SIZE);

}