use of java.util.zip.Deflater in project j2objc by google.
the class OldAndroidDeflateTest method bigTest.
/*
* "step" determines how compressible the data is.
*
* Note we must set "nowrap" to false, or the Adler-32 doesn't get
* computed.
*/
private void bigTest(int step, int expectedAdler) throws UnsupportedEncodingException, DataFormatException {
byte[] input = new byte[128 * 1024];
byte[] comp = new byte[128 * 1024 + 512];
byte[] output = new byte[128 * 1024 + 512];
Inflater inflater = new Inflater(false);
Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION, false);
createSample(input, step);
compress(deflater, input, comp);
expand(inflater, comp, (int) deflater.getBytesWritten(), output);
assertEquals(inflater.getBytesWritten(), input.length);
assertEquals(deflater.getAdler(), inflater.getAdler());
assertEquals(deflater.getAdler(), expectedAdler);
}
use of java.util.zip.Deflater in project HdrHistogram by HdrHistogram.
the class SkinnyHistogram method encodeIntoCompressedByteBuffer.
public synchronized int encodeIntoCompressedByteBuffer(final ByteBuffer targetBuffer) {
ByteBuffer intermediateUncompressedByteBuffer = ByteBuffer.allocate(this.getNeededByteBufferCapacity());
int uncompressedLength = this.encodeIntoByteBuffer(intermediateUncompressedByteBuffer);
targetBuffer.putInt(SkinnyHistogram.encodingCompressedCookieBase);
targetBuffer.putInt(0);
targetBuffer.putInt(uncompressedLength);
Deflater compressor = new Deflater(defaultCompressionLevel);
compressor.setInput(intermediateUncompressedByteBuffer.array(), 0, uncompressedLength);
compressor.finish();
byte[] targetArray = targetBuffer.array();
int compressedDataLength = compressor.deflate(targetArray, 12, targetArray.length - 12);
compressor.reset();
targetBuffer.putInt(4, compressedDataLength);
return compressedDataLength + 12;
}
use of java.util.zip.Deflater in project HdrHistogram by HdrHistogram.
the class HistogramTest method testAbstractSerialization.
void testAbstractSerialization(AbstractHistogram histogram) throws Exception {
histogram.recordValue(testValueLevel);
histogram.recordValue(testValueLevel * 10);
histogram.recordValueWithExpectedInterval(histogram.getHighestTrackableValue() - 1, 255);
if (histogram.supportsAutoResize()) {
histogram.setAutoResize(true);
assertTrue(histogram.isAutoResize());
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = null;
ByteArrayInputStream bis = null;
ObjectInput in = null;
AbstractHistogram newHistogram = null;
try {
out = new ObjectOutputStream(bos);
out.writeObject(histogram);
Deflater compresser = new Deflater();
compresser.setInput(bos.toByteArray());
compresser.finish();
byte[] compressedOutput = new byte[1024 * 1024];
int compressedDataLength = compresser.deflate(compressedOutput);
System.out.println("Serialized form of " + histogram.getClass() + " with trackableValueRangeSize = " + histogram.getHighestTrackableValue() + "\n and a numberOfSignificantValueDigits = " + histogram.getNumberOfSignificantValueDigits() + " is " + bos.toByteArray().length + " bytes long. Compressed form is " + compressedDataLength + " bytes long.");
System.out.println(" (estimated footprint was " + histogram.getEstimatedFootprintInBytes() + " bytes)");
bis = new ByteArrayInputStream(bos.toByteArray());
in = new ObjectInputStream(bis);
newHistogram = (AbstractHistogram) in.readObject();
} finally {
if (out != null)
out.close();
bos.close();
if (in != null)
in.close();
if (bis != null)
bis.close();
}
Assert.assertNotNull(newHistogram);
assertEqual(histogram, newHistogram);
assertTrue(histogram.equals(newHistogram));
if (histogram.supportsAutoResize()) {
assertTrue(histogram.isAutoResize());
}
assertEquals(newHistogram.isAutoResize(), histogram.isAutoResize());
Assert.assertTrue(histogram.hashCode() == newHistogram.hashCode());
assertEquals(histogram.getNeededByteBufferCapacity(), newHistogram.copy().getNeededByteBufferCapacity());
assertEquals(histogram.getNeededByteBufferCapacity(), newHistogram.getNeededByteBufferCapacity());
}
use of java.util.zip.Deflater in project okio by square.
the class DeflaterSinkTest method deflateIntoNonemptySink.
@Test
public void deflateIntoNonemptySink() throws Exception {
String original = "They're moving in herds. They do move in herds.";
// Exercise all possible offsets for the outgoing segment.
for (int i = 0; i < Segment.SIZE; i++) {
Buffer data = new Buffer().writeUtf8(original);
Buffer sink = new Buffer().writeUtf8(repeat('a', i));
DeflaterSink deflaterSink = new DeflaterSink(sink, new Deflater());
deflaterSink.write(data, data.size());
deflaterSink.close();
sink.skip(i);
Buffer inflated = inflate(sink);
assertEquals(original, inflated.readUtf8());
}
}
use of java.util.zip.Deflater in project okio by square.
the class DeflaterSinkTest method deflatePoorlyCompressed.
@Test
public void deflatePoorlyCompressed() throws IOException {
ByteString original = randomBytes(1024 * 1024);
Buffer data = new Buffer();
data.write(original);
Buffer sink = new Buffer();
DeflaterSink deflaterSink = new DeflaterSink(sink, new Deflater());
deflaterSink.write(data, data.size());
deflaterSink.close();
Buffer inflated = inflate(sink);
assertEquals(original, inflated.readByteString());
}
Aggregations