Search in sources :

Example 1 with Deflater

use of java.util.zip.Deflater in project hive by apache.

the class InputJobInfo method writeObject.

/**
   * Serialize this object, compressing the partitions which can exceed the
   * allowed jobConf size.
   * @see <a href="https://issues.apache.org/jira/browse/HCATALOG-453">HCATALOG-453</a>
   */
private void writeObject(ObjectOutputStream oos) throws IOException {
    oos.defaultWriteObject();
    Deflater def = new Deflater(Deflater.BEST_COMPRESSION);
    ObjectOutputStream partInfoWriter = new ObjectOutputStream(new DeflaterOutputStream(oos, def));
    partInfoWriter.writeObject(partitions);
    partInfoWriter.close();
}
Also used : Deflater(java.util.zip.Deflater) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) ObjectOutputStream(java.io.ObjectOutputStream)

Example 2 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)

Example 3 with Deflater

use of java.util.zip.Deflater in project jetty.project by eclipse.

the class GzipHandler method getDeflater.

/* ------------------------------------------------------------ */
@Override
public Deflater getDeflater(Request request, long content_length) {
    String ua = request.getHttpFields().get(HttpHeader.USER_AGENT);
    if (ua != null && !isAgentGzipable(ua)) {
        LOG.debug("{} excluded user agent {}", this, request);
        return null;
    }
    if (content_length >= 0 && content_length < _minGzipSize) {
        LOG.debug("{} excluded minGzipSize {}", this, request);
        return null;
    }
    // check the accept encoding header
    HttpField accept = request.getHttpFields().getField(HttpHeader.ACCEPT_ENCODING);
    if (accept == null) {
        LOG.debug("{} excluded !accept {}", this, request);
        return null;
    }
    boolean gzip = accept.contains("gzip");
    if (!gzip) {
        LOG.debug("{} excluded not gzip accept {}", this, request);
        return null;
    }
    Deflater df = _deflater.get();
    if (df == null)
        df = new Deflater(_compressionLevel, true);
    else
        _deflater.set(null);
    return df;
}
Also used : Deflater(java.util.zip.Deflater) HttpField(org.eclipse.jetty.http.HttpField)

Example 4 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 5 with Deflater

use of java.util.zip.Deflater in project elasticsearch by elastic.

the class DeflateCompressor method streamOutput.

@Override
public StreamOutput streamOutput(StreamOutput out) throws IOException {
    out.writeBytes(HEADER);
    final boolean nowrap = true;
    final Deflater deflater = new Deflater(LEVEL, nowrap);
    final boolean syncFlush = true;
    OutputStream compressedOut = new DeflaterOutputStream(out, deflater, BUFFER_SIZE, syncFlush);
    compressedOut = new BufferedOutputStream(compressedOut, BUFFER_SIZE);
    return new OutputStreamStreamOutput(compressedOut) {

        final AtomicBoolean closed = new AtomicBoolean(false);

        public void close() throws IOException {
            try {
                super.close();
            } finally {
                if (closed.compareAndSet(false, true)) {
                    // important to release native memory
                    deflater.end();
                }
            }
        }
    };
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) OutputStreamStreamOutput(org.elasticsearch.common.io.stream.OutputStreamStreamOutput) Deflater(java.util.zip.Deflater) OutputStream(java.io.OutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

Deflater (java.util.zip.Deflater)75 ByteArrayOutputStream (java.io.ByteArrayOutputStream)30 DeflaterOutputStream (java.util.zip.DeflaterOutputStream)24 IOException (java.io.IOException)15 Test (org.junit.Test)12 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 CRC32 (java.util.zip.CRC32)3 BufferedOutputStream (java.io.BufferedOutputStream)2 DataOutputStream (java.io.DataOutputStream)2 FileInputStream (java.io.FileInputStream)2 Field (java.lang.reflect.Field)2 ByteBuffer (java.nio.ByteBuffer)2 Random (java.util.Random)2 SHA1 (aQute.libg.cryptography.SHA1)1