Search in sources :

Example 6 with Deflater

use of java.util.zip.Deflater in project okio by square.

the class DeflaterSinkTest method multipleSegmentsWithoutCompression.

@Test
public void multipleSegmentsWithoutCompression() throws IOException {
    Buffer buffer = new Buffer();
    Deflater deflater = new Deflater();
    deflater.setLevel(Deflater.NO_COMPRESSION);
    DeflaterSink deflaterSink = new DeflaterSink(buffer, deflater);
    int byteCount = Segment.SIZE * 4;
    deflaterSink.write(new Buffer().writeUtf8(repeat('a', byteCount)), byteCount);
    deflaterSink.close();
    assertEquals(repeat('a', byteCount), inflate(buffer).readUtf8(byteCount));
}
Also used : Deflater(java.util.zip.Deflater) Test(org.junit.Test)

Example 7 with Deflater

use of java.util.zip.Deflater in project okio by square.

the class DeflaterSinkTest method deflateWithSyncFlush.

@Test
public void deflateWithSyncFlush() throws Exception {
    String original = "Yes, yes, yes. That's why we're taking extreme precautions.";
    Buffer data = new Buffer();
    data.writeUtf8(original);
    Buffer sink = new Buffer();
    DeflaterSink deflaterSink = new DeflaterSink(sink, new Deflater());
    deflaterSink.write(data, data.size());
    deflaterSink.flush();
    Buffer inflated = inflate(sink);
    assertEquals(original, inflated.readUtf8());
}
Also used : Deflater(java.util.zip.Deflater) Test(org.junit.Test)

Example 8 with Deflater

use of java.util.zip.Deflater in project bigbluebutton by bigbluebutton.

the class ScreenVideoEncoder method compressUsingZlib.

/**
	 * Compress the byte array using Zlib.
	 * @param pixels
	 * @return a byte array of compressed data
	 */
private static byte[] compressUsingZlib(byte[] pixels) {
    long start = System.currentTimeMillis();
    // Create the compressed stream
    byte[] output = new byte[pixels.length];
    Deflater compresser = new Deflater(Deflater.BEST_COMPRESSION);
    compresser.setInput(pixels);
    compresser.finish();
    int compressedDataLength = compresser.deflate(output);
    byte[] zData = new byte[compressedDataLength];
    System.arraycopy(output, 0, zData, 0, compressedDataLength);
    long end = System.currentTimeMillis();
    // set the byte array to the newly compressed data
    return zData;
}
Also used : Deflater(java.util.zip.Deflater)

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

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

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