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));
}
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());
}
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;
}
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();
}
}
}
};
}
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;
}
Aggregations