Search in sources :

Example 81 with Deflater

use of java.util.zip.Deflater in project JGroups by belaban.

the class COMPRESS method down.

/**
 * We compress the payload if it is larger than {@code min_size}. In this case we add a header containing
 * the original size before compression. Otherwise we add no header.<p>
 * Note that we compress either the entire buffer (if offset/length are not used), or a subset (if offset/length
 * are used)
 */
public Object down(Message msg) {
    // takes offset/length (if set) into account
    int length = msg.getLength();
    if (length >= min_size) {
        // here we get the ref so we can avoid copying
        byte[] payload = msg.getRawBuffer();
        byte[] compressed_payload = new byte[length];
        Deflater deflater = null;
        try {
            deflater = deflater_pool.take();
            deflater.reset();
            deflater.setInput(payload, msg.getOffset(), length);
            deflater.finish();
            deflater.deflate(compressed_payload);
            int compressed_size = deflater.getTotalOut();
            if (compressed_size < length) {
                // JGRP-1000
                Message copy = msg.copy(false).putHeader(this.id, new CompressHeader(length)).setBuffer(compressed_payload, 0, compressed_size);
                if (log.isTraceEnabled())
                    log.trace("compressed payload from %d bytes to %d bytes", length, compressed_size);
                return down_prot.down(copy);
            } else {
                if (log.isTraceEnabled())
                    log.trace("skipping compression since the compressed message (%d) is not " + "smaller than the original (%d)", compressed_size, length);
            }
        } catch (InterruptedException e) {
            // set interrupt flag again
            Thread.currentThread().interrupt();
            throw new RuntimeException(e);
        } finally {
            if (deflater != null)
                deflater_pool.offer(deflater);
        }
    }
    return down_prot.down(msg);
}
Also used : Deflater(java.util.zip.Deflater) Message(org.jgroups.Message)

Example 82 with Deflater

use of java.util.zip.Deflater in project jPOS by jpos.

the class DailyLogListenerTest method testCloseCompressedOutputStream.

@Test
public void testCloseCompressedOutputStream() throws Throwable {
    OutputStream os = new DeflaterOutputStream(new PrintStream(new ByteArrayOutputStream()), new Deflater());
    DailyLogListener dailyLogListener = new DailyLogListener();
    dailyLogListener.closeCompressedOutputStream(os);
    assertTrue("Test completed without Exception", true);
}
Also used : Deflater(java.util.zip.Deflater) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) Test(org.junit.Test)

Example 83 with Deflater

use of java.util.zip.Deflater in project BKCommonLib by bergerhealer.

the class CommonUtil method setCompressionLevel.

/**
 * Sets the compression level of a deflater stream
 *
 * @param stream to set the compression level of
 * @param level to set to
 * @return input stream
 */
public static <T extends DeflaterOutputStream> T setCompressionLevel(T stream, int level) {
    try {
        Field defField = DeflaterOutputStream.class.getDeclaredField("def");
        defField.setAccessible(true);
        Deflater deflater = (Deflater) defField.get(stream);
        deflater.setLevel(level);
    } catch (Throwable t) {
        t.printStackTrace();
    }
    return stream;
}
Also used : Field(java.lang.reflect.Field) Deflater(java.util.zip.Deflater)

Example 84 with Deflater

use of java.util.zip.Deflater in project scheduling by ow2-proactive.

the class ObjectByteConverter method objectToByteArray.

/**
 * Convert the given Serializable Object into a byte array.
 * <p>
 * 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();
        }
    }
}
Also used : Deflater(java.util.zip.Deflater) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream)

Example 85 with Deflater

use of java.util.zip.Deflater in project scheduling by ow2-proactive.

the class ByteCompressionUtils method compress.

public static byte[] compress(byte[] data) throws IOException {
    Deflater deflater = new Deflater();
    deflater.setInput(data);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    deflater.finish();
    byte[] buffer = new byte[1024];
    while (!deflater.finished()) {
        // returns the generated code... index
        int count = deflater.deflate(buffer);
        outputStream.write(buffer, 0, count);
    }
    outputStream.close();
    byte[] output = outputStream.toByteArray();
    deflater.end();
    LOG.debug("Original: " + data.length / 1024 + " Kb");
    LOG.debug("Compressed: " + output.length / 1024 + " Kb");
    return output;
}
Also used : Deflater(java.util.zip.Deflater) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

Deflater (java.util.zip.Deflater)85 ByteArrayOutputStream (java.io.ByteArrayOutputStream)33 DeflaterOutputStream (java.util.zip.DeflaterOutputStream)25 IOException (java.io.IOException)18 Test (org.junit.Test)13 Inflater (java.util.zip.Inflater)9 OutputStream (java.io.OutputStream)7 InflaterInputStream (java.util.zip.InflaterInputStream)7 InputStream (java.io.InputStream)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 EOFException (java.io.EOFException)3 FileOutputStream (java.io.FileOutputStream)3 Field (java.lang.reflect.Field)3 CRC32 (java.util.zip.CRC32)3 BufferedOutputStream (java.io.BufferedOutputStream)2 CharArrayReader (java.io.CharArrayReader)2 DataOutputStream (java.io.DataOutputStream)2 FileInputStream (java.io.FileInputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 Reader (java.io.Reader)2