Search in sources :

Example 71 with Inflater

use of java.util.zip.Inflater in project k-9 by k9mail.

the class ImapConnection method enableCompression.

private void enableCompression() throws IOException, MessagingException {
    try {
        executeSimpleCommand(Commands.COMPRESS_DEFLATE);
    } catch (NegativeImapResponseException e) {
        Log.d(LOG_TAG, "Unable to negotiate compression: " + e.getMessage());
        return;
    }
    try {
        InflaterInputStream input = new InflaterInputStream(socket.getInputStream(), new Inflater(true));
        ZOutputStream output = new ZOutputStream(socket.getOutputStream(), JZlib.Z_BEST_SPEED, true);
        output.setFlushMode(JZlib.Z_PARTIAL_FLUSH);
        setUpStreamsAndParser(input, output);
        if (K9MailLib.isDebug()) {
            Log.i(LOG_TAG, "Compression enabled for " + getLogId());
        }
    } catch (IOException e) {
        close();
        Log.e(LOG_TAG, "Error enabling compression", e);
    }
}
Also used : InflaterInputStream(java.util.zip.InflaterInputStream) Inflater(java.util.zip.Inflater) IOException(java.io.IOException) ZOutputStream(com.jcraft.jzlib.ZOutputStream)

Example 72 with Inflater

use of java.util.zip.Inflater in project AndroidAsync by koush.

the class HybiParser method finalize.

@Override
protected void finalize() throws Throwable {
    Inflater inflater = mInflater;
    if (inflater != null) {
        try {
            inflater.end();
        } catch (Exception e) {
            Log.e(TAG, "inflater.end failed", e);
        }
    }
    super.finalize();
}
Also used : Inflater(java.util.zip.Inflater) DataFormatException(java.util.zip.DataFormatException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 73 with Inflater

use of java.util.zip.Inflater in project rest.li by linkedin.

the class DeflateCompressor method inflate.

@Override
public byte[] inflate(InputStream data) throws CompressionException {
    byte[] input;
    try {
        input = IOUtils.toByteArray(data);
    } catch (IOException e) {
        throw new CompressionException(CompressionConstants.DECODING_ERROR + CompressionConstants.BAD_STREAM, e);
    }
    Inflater zlib = new Inflater();
    zlib.setInput(input);
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    byte[] temp = new byte[CompressionConstants.BUFFER_SIZE];
    int bytesRead;
    while (!zlib.finished()) {
        try {
            bytesRead = zlib.inflate(temp);
        } catch (DataFormatException e) {
            throw new CompressionException(CompressionConstants.DECODING_ERROR + getContentEncodingName(), e);
        }
        if (bytesRead == 0) {
            if (!zlib.needsInput()) {
                throw new CompressionException(CompressionConstants.DECODING_ERROR + getContentEncodingName());
            } else {
                break;
            }
        }
        if (bytesRead > 0) {
            output.write(temp, 0, bytesRead);
        }
    }
    zlib.end();
    return output.toByteArray();
}
Also used : DataFormatException(java.util.zip.DataFormatException) Inflater(java.util.zip.Inflater) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 74 with Inflater

use of java.util.zip.Inflater in project dex2jar by pxb1988.

the class ZipFile method getInputStream.

/**
     * Returns an input stream on the data of the specified {@code android.ZipEntry}.
     * 
     * @param entry
     *            the android.ZipEntry.
     * @return an input stream of the data contained in the {@code android.ZipEntry}.
     * @throws java.io.IOException
     *             if an {@code IOException} occurs.
     * @throws IllegalStateException
     *             if this zip file has been closed.
     */
public InputStream getInputStream(ZipEntry entry) throws IOException {
    long entryDataStart = getEntryDataStart(entry);
    ByteBuffer is = (ByteBuffer) raf.duplicate().position((int) entryDataStart);
    if (entry.compressionMethod == ZipEntry.STORED) {
        final ByteBuffer buf = (ByteBuffer) is.slice().order(ByteOrder.LITTLE_ENDIAN).limit((int) entry.size);
        return new ByteBufferBackedInputStream(buf);
    } else {
        final ByteBuffer buf = (ByteBuffer) is.slice().order(ByteOrder.LITTLE_ENDIAN).limit((int) entry.compressedSize);
        int bufSize = Math.max(1024, (int) Math.min(entry.getSize(), 65535L));
        return new ZipInflaterInputStream(new ByteBufferBackedInputStream(buf), new Inflater(true), bufSize, entry);
    }
}
Also used : Inflater(java.util.zip.Inflater) ByteBuffer(java.nio.ByteBuffer)

Example 75 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)

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