use of java.util.zip.Deflater in project CodeChickenLib by Chicken-Bones.
the class PacketCustom method do_compress.
/**
* Compresses the payload ByteBuf after the type byte
*/
private void do_compress() {
Deflater deflater = new Deflater();
try {
readerIndex(1);
int len = readableBytes();
deflater.setInput(array(), readerIndex(), len);
deflater.finish();
byte[] out = new byte[len];
int clen = deflater.deflate(out);
if (//not worth compressing, gets larger
clen >= len - 5 || !deflater.finished())
return;
clear();
writeByte(type | 0x80);
writeVarInt(len);
writeByteArray(out);
} catch (Exception e) {
throw new EncoderException(e);
} finally {
readerIndex(0);
deflater.end();
}
}
use of java.util.zip.Deflater in project Anki-Android by Ramblurr.
the class Utils method compress.
/**
* Compress data.
* @param bytesToCompress is the byte array to compress.
* @return a compressed byte array.
* @throws java.io.IOException
*/
public static byte[] compress(byte[] bytesToCompress, int comp) throws IOException {
// Compressor with highest level of compression.
Deflater compressor = new Deflater(comp, true);
// Give the compressor the data to compress.
compressor.setInput(bytesToCompress);
compressor.finish();
// Create an expandable byte array to hold the compressed data.
// It is not necessary that the compressed data will be smaller than
// the uncompressed data.
ByteArrayOutputStream bos = new ByteArrayOutputStream(bytesToCompress.length);
// Compress the data
byte[] buf = new byte[65536];
while (!compressor.finished()) {
bos.write(buf, 0, compressor.deflate(buf));
}
bos.close();
// Get the compressed data
return bos.toByteArray();
}
use of java.util.zip.Deflater in project hadoop by apache.
the class CompressedWritable method write.
@Override
public final void write(DataOutput out) throws IOException {
if (compressed == null) {
ByteArrayOutputStream deflated = new ByteArrayOutputStream();
Deflater deflater = new Deflater(Deflater.BEST_SPEED);
DataOutputStream dout = new DataOutputStream(new DeflaterOutputStream(deflated, deflater));
writeCompressed(dout);
dout.close();
deflater.end();
compressed = deflated.toByteArray();
}
out.writeInt(compressed.length);
out.write(compressed);
}
use of java.util.zip.Deflater in project ABPlayer by winkstu.
the class CompressionTools method compress.
public static byte[] compress(byte[] value, int offset, int length, int compressionLevel) {
ByteArrayOutputStream bos = new ByteArrayOutputStream(length);
Deflater compressor = new Deflater();
try {
//将当前压缩级别设置为指定值。
compressor.setLevel(compressionLevel);
compressor.setInput(value, offset, length);
//调用时,指示压缩应当以输入缓冲区的当前内容结尾。
compressor.finish();
// Compress the data
final byte[] buf = new byte[1024];
while (!compressor.finished()) {
//如果已到达压缩数据输出流的结尾,则返回 true。
int count = compressor.deflate(buf);
// 使用压缩数据填充指定缓冲区。
bos.write(buf, 0, count);
}
} finally {
//关闭解压缩器并放弃所有未处理的输入。
compressor.end();
}
return bos.toByteArray();
}
use of java.util.zip.Deflater in project OpenAM by OpenRock.
the class SAML2Utils method encodeForRedirect.
/**
* Returns the encoded request message.
* The SAML Request message must be
* encoded before being transmitted.
* The Request message is encoded as follows:
* 1. URL Encoded using the DEFLATE compression method.
* 2. Then the message is base-64 encoded according to
* the rules specified in RFC2045.
*
* @param str String to be encoded.
* @return String the encoded String value or null on error.
*/
public static String encodeForRedirect(final String str) {
String classMethod = "SAML2Utils.encodeForRedirect: ";
byte[] input;
try {
input = str.getBytes("UTF-8");
} catch (UnsupportedEncodingException uee) {
debug.error(classMethod + "cannot get byte array: ", uee);
return null;
}
Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
ByteArrayOutputStream out = new ByteArrayOutputStream();
DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(out, deflater);
try {
deflaterOutputStream.write(input);
} catch (IOException e) {
debug.error(classMethod + "There was a problem compressing the input", e);
return null;
} finally {
IOUtils.closeIfNotNull(deflaterOutputStream);
}
String encoded = URLEncDec.encode(Base64.encode(out.toByteArray()));
if (debug.messageEnabled()) {
debug.message(classMethod + "out string length : " + encoded.length());
debug.message(classMethod + "out string is ===>" + encoded + "<===");
}
return encoded;
}
Aggregations