Search in sources :

Example 51 with Deflater

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

Example 52 with Deflater

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

Example 53 with Deflater

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

Example 54 with Deflater

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

Example 55 with Deflater

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