Search in sources :

Example 16 with Inflater

use of java.util.zip.Inflater in project robovm by robovm.

the class SpdyReader method newNameValueBlockStream.

private DataInputStream newNameValueBlockStream() {
    // Limit the inflater input stream to only those bytes in the Name/Value block.
    final InputStream throttleStream = new InputStream() {

        @Override
        public int read() throws IOException {
            return Util.readSingleByte(this);
        }

        @Override
        public int read(byte[] buffer, int offset, int byteCount) throws IOException {
            byteCount = Math.min(byteCount, compressedLimit);
            int consumed = in.read(buffer, offset, byteCount);
            compressedLimit -= consumed;
            return consumed;
        }

        @Override
        public void close() throws IOException {
            in.close();
        }
    };
    // Subclass inflater to install a dictionary when it's needed.
    Inflater inflater = new Inflater() {

        @Override
        public int inflate(byte[] buffer, int offset, int count) throws DataFormatException {
            int result = super.inflate(buffer, offset, count);
            if (result == 0 && needsDictionary()) {
                setDictionary(DICTIONARY);
                result = super.inflate(buffer, offset, count);
            }
            return result;
        }
    };
    return new DataInputStream(new InflaterInputStream(throttleStream, inflater));
}
Also used : DataInputStream(java.io.DataInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) Inflater(java.util.zip.Inflater) DataInputStream(java.io.DataInputStream)

Example 17 with Inflater

use of java.util.zip.Inflater in project robovm by robovm.

the class FilterInputStreamNullSourceTest method testInflaterInputStream.

public void testInflaterInputStream() throws IOException {
    try {
        new InflaterInputStream(null);
        fail();
    } catch (NullPointerException expected) {
    }
    try {
        new InflaterInputStream(null, new Inflater());
        fail();
    } catch (NullPointerException expected) {
    }
    try {
        new InflaterInputStream(null, new Inflater(), 1024);
        fail();
    } catch (NullPointerException expected) {
    }
}
Also used : InflaterInputStream(java.util.zip.InflaterInputStream) Inflater(java.util.zip.Inflater)

Example 18 with Inflater

use of java.util.zip.Inflater in project robovm by robovm.

the class DeflaterTest method testDeflate.

public void testDeflate() throws DataFormatException {
    deflater.setInput(new byte[] { 1, 2, 3 });
    deflateInflate(Deflater.NO_FLUSH);
    assertTrue(totalInflated < 3);
    // the 3rd byte shouldn't have been flushed yet
    assertEquals(0, decompressed[2]);
    deflater.setInput(new byte[] { 4, 5, 6 });
    deflateInflate(Deflater.SYNC_FLUSH);
    assertEquals(6, totalInflated);
    assertDecompressed(1, 2, 3, 4, 5, 6);
    assertEquals(0, inflater.inflate(decompressed));
    deflater.setInput(new byte[] { 7, 8, 9 });
    deflateInflate(Deflater.FULL_FLUSH);
    assertEquals(9, totalInflated);
    assertDecompressed(1, 2, 3, 4, 5, 6, 7, 8, 9);
    assertEquals(0, inflater.inflate(decompressed));
    // safe because we did a FULL_FLUSH
    inflater = new Inflater(true);
    deflater.setInput(new byte[] { 10, 11, 12 });
    deflateInflate(Deflater.SYNC_FLUSH);
    assertEquals(12, totalInflated);
    assertDecompressed(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
    assertEquals(0, inflater.inflate(decompressed));
}
Also used : Inflater(java.util.zip.Inflater)

Example 19 with Inflater

use of java.util.zip.Inflater in project robovm by robovm.

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 20 with Inflater

use of java.util.zip.Inflater in project robovm by robovm.

the class OldAndroidDeflateTest method simpleTest.

/*
     * Simple inflate/deflate test, taken from the reference docs for the
     * Inflater/Deflater classes.
     */
private void simpleTest() throws UnsupportedEncodingException, DataFormatException {
    // Encode a String into bytes
    String inputString = "blahblahblah??";
    byte[] input = inputString.getBytes("UTF-8");
    // Compress the bytes
    byte[] output = new byte[100];
    Deflater compresser = new Deflater();
    compresser.setInput(input);
    compresser.finish();
    int compressedDataLength = compresser.deflate(output);
    // Decompress the bytes
    Inflater decompresser = new Inflater();
    decompresser.setInput(output, 0, compressedDataLength);
    byte[] result = new byte[100];
    int resultLength = decompresser.inflate(result);
    // Decode the bytes into a String
    String outputString = new String(result, 0, resultLength, "UTF-8");
    assertEquals(inputString, outputString);
    assertEquals(compresser.getAdler(), decompresser.getAdler());
    decompresser.end();
}
Also used : Deflater(java.util.zip.Deflater) Inflater(java.util.zip.Inflater)

Aggregations

Inflater (java.util.zip.Inflater)99 InflaterInputStream (java.util.zip.InflaterInputStream)34 IOException (java.io.IOException)28 ByteArrayOutputStream (java.io.ByteArrayOutputStream)26 InputStream (java.io.InputStream)24 DataFormatException (java.util.zip.DataFormatException)24 GZIPInputStream (java.util.zip.GZIPInputStream)14 ByteArrayInputStream (java.io.ByteArrayInputStream)10 Deflater (java.util.zip.Deflater)9 BufferedInputStream (java.io.BufferedInputStream)6 DataInputStream (java.io.DataInputStream)6 OutputStream (java.io.OutputStream)6 Test (org.junit.Test)6 BufferedReader (java.io.BufferedReader)5 InputStreamReader (java.io.InputStreamReader)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 HttpURLConnection (java.net.HttpURLConnection)5 ByteBuffer (java.nio.ByteBuffer)5 DataOutputStream (java.io.DataOutputStream)4 OutputStreamWriter (java.io.OutputStreamWriter)4