Search in sources :

Example 81 with Inflater

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

the class InflaterSourceTest method inflate.

/** Returns a new buffer containing the inflated contents of {@code deflated}. */
private Buffer inflate(Buffer deflated) throws IOException {
    Buffer result = new Buffer();
    InflaterSource source = new InflaterSource(deflated, new Inflater());
    while (source.read(result, Integer.MAX_VALUE) != -1) {
    }
    return result;
}
Also used : Inflater(java.util.zip.Inflater)

Example 82 with Inflater

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

the class InflaterSourceTest method inflateIntoNonemptySink.

@Test
public void inflateIntoNonemptySink() throws Exception {
    for (int i = 0; i < Segment.SIZE; i++) {
        Buffer inflated = new Buffer().writeUtf8(repeat('a', i));
        Buffer deflated = decodeBase64("eJxzz09RyEjNKVAoLdZRKE9VL0pVyMxTKMlIVchIzEspVshPU0jNS8/MS00tKtYDAF6CD5s=");
        InflaterSource source = new InflaterSource(deflated, new Inflater());
        while (source.read(inflated, Integer.MAX_VALUE) != -1) {
        }
        inflated.skip(i);
        assertEquals("God help us, we're in the hands of engineers.", inflated.readUtf8());
    }
}
Also used : Inflater(java.util.zip.Inflater) Test(org.junit.Test)

Example 83 with Inflater

use of java.util.zip.Inflater in project bilibili-android-client by HotBitmapGG.

the class BiliDanmukuCompressionTools method decompressXML.

static byte[] decompressXML(byte[] data) throws DataFormatException {
    byte[] dest = new byte[data.length + 2];
    System.arraycopy(data, 0, dest, 2, data.length);
    dest[0] = 0x78;
    dest[1] = 0x01;
    data = dest;
    Inflater decompresser = new Inflater();
    decompresser.setInput(data);
    byte[] bufferArray = new byte[1024];
    ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
    try {
        int i = 1;
        while (i != 0) {
            i = decompresser.inflate(bufferArray);
            baos.write(bufferArray, 0, i);
        }
        data = baos.toByteArray();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            baos.flush();
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    decompresser.end();
    return data;
}
Also used : Inflater(java.util.zip.Inflater) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) DataFormatException(java.util.zip.DataFormatException) IOException(java.io.IOException)

Example 84 with Inflater

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

the class OldAndroidZipStressTest method testZipDeflateInflateStress.

/**
     * Native memory allocated by Deflater in system_server. The fix reduced
     * some internal ZLIB buffers in size, so this test is trying to execute a
     * lot of deflating to ensure that things are still working properly.
     * http://b/1185084
     */
public void testZipDeflateInflateStress() throws Exception {
    final int DATA_SIZE = 16384;
    // Seed makes test reproducible
    Random random = new Random(42);
    // Outer loop selects "mode" of test.
    for (int j = 1; j <= 2; j++) {
        byte[] input = new byte[DATA_SIZE];
        if (j == 1) {
            // Totally random content
            random.nextBytes(input);
        } else {
            // Random contents with longer repetitions
            int pos = 0;
            while (pos < input.length) {
                byte what = (byte) random.nextInt(256);
                int howMany = random.nextInt(32);
                if (pos + howMany >= input.length) {
                    howMany = input.length - pos;
                }
                Arrays.fill(input, pos, pos + howMany, what);
                pos += howMany;
            }
        }
        // Inner loop tries all 9 compression levels.
        for (int i = 1; i <= 9; i++) {
            System.out.println("ZipDeflateInflateStress test (" + j + "," + i + ")...");
            // Just to make sure...
            byte[] zipped = new byte[2 * DATA_SIZE];
            Deflater deflater = new Deflater(i);
            deflater.setInput(input);
            deflater.finish();
            deflater.deflate(zipped);
            deflater.end();
            byte[] output = new byte[DATA_SIZE];
            Inflater inflater = new Inflater();
            inflater.setInput(zipped);
            inflater.finished();
            inflater.inflate(output);
            inflater.end();
            assertEquals(input, output);
        }
    }
}
Also used : Random(java.util.Random) Deflater(java.util.zip.Deflater) Inflater(java.util.zip.Inflater)

Example 85 with Inflater

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

the class InflaterTest method assertRoundTrip.

private static void assertRoundTrip(byte[] dictionary) throws Exception {
    // Construct a nice long input byte sequence.
    String expected = makeString();
    byte[] expectedBytes = expected.getBytes("UTF-8");
    // Compress the bytes, using the passed-in dictionary (or no dictionary).
    byte[] deflatedBytes = deflate(expectedBytes, dictionary);
    // Get ready to decompress deflatedBytes back to the original bytes ...
    Inflater inflater = new Inflater();
    // We'll only supply the input a little bit at a time, so that zlib has to ask for more.
    final int CHUNK_SIZE = 16;
    int offset = 0;
    inflater.setInput(deflatedBytes, offset, CHUNK_SIZE);
    offset += CHUNK_SIZE;
    // If we used a dictionary to compress, check that we're asked for that same dictionary.
    if (dictionary != null) {
        // 1. there's no data available immediately...
        assertEquals(0, inflater.inflate(new byte[8]));
        // 2. ...because you need a dictionary.
        assertTrue(inflater.needsDictionary());
        // 3. ...and that dictionary has the same Adler32 as the dictionary we used.
        assertEquals(adler32(dictionary), inflater.getAdler());
        inflater.setDictionary(dictionary);
    }
    // Do the actual decompression, now the dictionary's set up appropriately...
    // We use a tiny output buffer to ensure that we call inflate multiple times, and
    // a tiny input buffer to ensure that zlib has to ask for more input.
    ByteArrayOutputStream inflatedBytes = new ByteArrayOutputStream();
    byte[] buf = new byte[8];
    while (inflatedBytes.size() != expectedBytes.length) {
        if (inflater.needsInput()) {
            int nextChunkByteCount = Math.min(CHUNK_SIZE, deflatedBytes.length - offset);
            inflater.setInput(deflatedBytes, offset, nextChunkByteCount);
            offset += nextChunkByteCount;
        } else {
            int inflatedByteCount = inflater.inflate(buf);
            if (inflatedByteCount > 0) {
                inflatedBytes.write(buf, 0, inflatedByteCount);
            }
        }
    }
    inflater.end();
    assertEquals(expected, new String(inflatedBytes.toByteArray(), "UTF-8"));
}
Also used : Inflater(java.util.zip.Inflater) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

Inflater (java.util.zip.Inflater)108 InflaterInputStream (java.util.zip.InflaterInputStream)36 IOException (java.io.IOException)33 ByteArrayOutputStream (java.io.ByteArrayOutputStream)31 DataFormatException (java.util.zip.DataFormatException)30 InputStream (java.io.InputStream)24 ByteArrayInputStream (java.io.ByteArrayInputStream)14 GZIPInputStream (java.util.zip.GZIPInputStream)14 Deflater (java.util.zip.Deflater)9 Test (org.junit.Test)7 DataInputStream (java.io.DataInputStream)6 OutputStream (java.io.OutputStream)6 BufferedInputStream (java.io.BufferedInputStream)5 BufferedReader (java.io.BufferedReader)5 InputStreamReader (java.io.InputStreamReader)5 HttpURLConnection (java.net.HttpURLConnection)5 ByteBuffer (java.nio.ByteBuffer)5 JSONObject (org.json.JSONObject)5 DataOutputStream (java.io.DataOutputStream)4 OutputStreamWriter (java.io.OutputStreamWriter)4