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);
}
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);
}
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;
}
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();
}
}
}
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;
}
Aggregations