Search in sources :

Example 11 with Deflater

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));
}
Also used : Deflater(java.util.zip.Deflater) ByteBuffer(java.nio.ByteBuffer) AbstractExtensionTest(org.eclipse.jetty.websocket.common.extensions.AbstractExtensionTest) Test(org.junit.Test)

Example 12 with Deflater

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);
}
Also used : Deflater(java.util.zip.Deflater)

Example 13 with Deflater

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;
}
Also used : Deflater(java.util.zip.Deflater) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 14 with Deflater

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);
}
Also used : Deflater(java.util.zip.Deflater) DeflaterOutputStream(java.util.zip.DeflaterOutputStream)

Example 15 with Deflater

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");
}
Also used : Deflater(java.util.zip.Deflater) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HttpTester(org.eclipse.jetty.http.HttpTester) Test(org.junit.Test)

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