use of java.util.zip.Deflater in project jetty.project by eclipse.
the class DeflateFrameExtensionTest method testDeflateBasics.
@Test
public void testDeflateBasics() throws Exception {
// Setup deflater basics
Deflater compressor = new Deflater(Deflater.BEST_COMPRESSION, true);
compressor.setStrategy(Deflater.DEFAULT_STRATEGY);
// Text to compress
String text = "info:";
byte[] uncompressed = StringUtil.getUtf8Bytes(text);
// Prime the compressor
compressor.reset();
compressor.setInput(uncompressed, 0, uncompressed.length);
compressor.finish();
// Perform compression
ByteBuffer outbuf = ByteBuffer.allocate(64);
BufferUtil.clearToFill(outbuf);
while (!compressor.finished()) {
byte[] out = new byte[64];
int len = compressor.deflate(out, 0, out.length, Deflater.SYNC_FLUSH);
if (len > 0) {
outbuf.put(out, 0, len);
}
}
compressor.end();
BufferUtil.flipToFlush(outbuf, 0);
byte[] compressed = BufferUtil.toArray(outbuf);
// Clear the BFINAL bit that has been set by the compressor.end() call.
// In the real implementation we never end() the compressor.
compressed[0] &= 0xFE;
String actual = TypeUtil.toHexString(compressed);
// what pywebsocket produces
String expected = "CaCc4bCbB70200";
Assert.assertThat("Compressed data", actual, is(expected));
}
use of java.util.zip.Deflater in project mapdb by jankotek.
the class SerializerCompressionDeflateWrapper method serialize.
// /** used for deserialization */
// @SuppressWarnings("unchecked")
// protected SerializerCompressionDeflateWrapper(SerializerBase serializerBase, DataInput2 is, SerializerBase.FastArrayList<Object> objectStack) throws IOException {
// objectStack.add(this);
// this.serializer = (Serializer<E>) serializerBase.deserialize(is,objectStack);
// this.compressLevel = is.readByte();
// int dictlen = is.unpackInt();
// if(dictlen==0) {
// dictionary = null;
// } else {
// byte[] d = new byte[dictlen];
// is.readFully(d);
// dictionary = d;
// }
// }
@Override
public void serialize(DataOutput2 out, E value) throws IOException {
DataOutput2 out2 = new DataOutput2();
serializer.serialize(out2, value);
byte[] tmp = new byte[out2.pos + 41];
int newLen;
try {
Deflater deflater = new Deflater(compressLevel);
if (dictionary != null) {
deflater.setDictionary(dictionary);
}
deflater.setInput(out2.buf, 0, out2.pos);
deflater.finish();
newLen = deflater.deflate(tmp);
//LZF.get().compress(out2.buf,out2.pos,tmp,0);
} catch (IndexOutOfBoundsException e) {
//larger after compression
newLen = 0;
}
if (newLen >= out2.pos || newLen == 0) {
//compression adds size, so do not compress
out.packInt(0);
out.write(out2.buf, 0, out2.pos);
return;
}
//unpacked size, zero indicates no compression
out.packInt(out2.pos + 1);
out.write(tmp, 0, newLen);
}
use of java.util.zip.Deflater in project Mycat-Server by MyCATApache.
the class CompressUtil method compress.
/**
* 适用于mysql与客户端交互时zlib 压缩
*
* @param data
* @return
*/
public static byte[] compress(byte[] data) {
byte[] output = null;
Deflater compresser = new Deflater();
compresser.setInput(data);
compresser.finish();
ByteArrayOutputStream out = new ByteArrayOutputStream(data.length);
byte[] result = new byte[1024];
try {
while (!compresser.finished()) {
int length = compresser.deflate(result);
out.write(result, 0, length);
}
output = out.toByteArray();
} finally {
try {
out.close();
} catch (Exception e) {
}
compresser.end();
}
return output;
}
use of java.util.zip.Deflater in project jersey by jersey.
the class DeflateEncoder method encode.
@Override
public OutputStream encode(String contentEncoding, OutputStream entityStream) throws IOException {
// some implementations don't support the correct deflate
// so we have a property to configure the incorrect deflate (no zlib wrapper) should be used
// let's check that
Object value = config.getProperty(MessageProperties.DEFLATE_WITHOUT_ZLIB);
boolean deflateWithoutZLib;
if (value instanceof String) {
deflateWithoutZLib = Boolean.valueOf((String) value);
} else if (value instanceof Boolean) {
deflateWithoutZLib = (Boolean) value;
} else {
deflateWithoutZLib = false;
}
return deflateWithoutZLib ? new DeflaterOutputStream(entityStream, new Deflater(Deflater.DEFAULT_COMPRESSION, true)) : new DeflaterOutputStream(entityStream);
}
use of java.util.zip.Deflater in project dropwizard by dropwizard.
the class BiDiGzipHandlerTest method testDecompressDeflateRequestGzipCompatible.
@Test
public void testDecompressDeflateRequestGzipCompatible() throws Exception {
request.setMethod("POST");
request.setURI("/banner");
request.setHeader(HttpHeaders.CONTENT_ENCODING, "deflate");
request.setHeader(HttpHeaders.CONTENT_TYPE, PLAIN_TEXT_UTF_8);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (DeflaterOutputStream deflate = new DeflaterOutputStream(baos, new Deflater(-1, true))) {
Resources.copy(Resources.getResource("assets/new-banner.txt"), deflate);
}
request.setContent(baos.toByteArray());
HttpTester.Response response = HttpTester.parseResponse(servletTester.getResponses(request.generate()));
assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getContent()).isEqualTo("Banner has been updated");
}
Aggregations