Search in sources :

Example 21 with Inflater

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

the class JdkZlibDecoder method decode.

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    if (finished) {
        // Skip data received after finished.
        in.skipBytes(in.readableBytes());
        return;
    }
    int readableBytes = in.readableBytes();
    if (readableBytes == 0) {
        return;
    }
    if (decideZlibOrNone) {
        // First two bytes are needed to decide if it's a ZLIB stream.
        if (readableBytes < 2) {
            return;
        }
        boolean nowrap = !looksLikeZlib(in.getShort(in.readerIndex()));
        inflater = new Inflater(nowrap);
        decideZlibOrNone = false;
    }
    if (crc != null) {
        switch(gzipState) {
            case FOOTER_START:
                if (readGZIPFooter(in)) {
                    finished = true;
                }
                return;
            default:
                if (gzipState != GzipState.HEADER_END) {
                    if (!readGZIPHeader(in)) {
                        return;
                    }
                }
        }
        // Some bytes may have been consumed, and so we must re-set the number of readable bytes.
        readableBytes = in.readableBytes();
    }
    if (in.hasArray()) {
        inflater.setInput(in.array(), in.arrayOffset() + in.readerIndex(), readableBytes);
    } else {
        byte[] array = new byte[readableBytes];
        in.getBytes(in.readerIndex(), array);
        inflater.setInput(array);
    }
    int maxOutputLength = inflater.getRemaining() << 1;
    ByteBuf decompressed = ctx.alloc().heapBuffer(maxOutputLength);
    try {
        boolean readFooter = false;
        byte[] outArray = decompressed.array();
        while (!inflater.needsInput()) {
            int writerIndex = decompressed.writerIndex();
            int outIndex = decompressed.arrayOffset() + writerIndex;
            int length = decompressed.writableBytes();
            if (length == 0) {
                // completely filled the buffer allocate a new one and start to fill it
                out.add(decompressed);
                decompressed = ctx.alloc().heapBuffer(maxOutputLength);
                outArray = decompressed.array();
                continue;
            }
            int outputLength = inflater.inflate(outArray, outIndex, length);
            if (outputLength > 0) {
                decompressed.writerIndex(writerIndex + outputLength);
                if (crc != null) {
                    crc.update(outArray, outIndex, outputLength);
                }
            } else {
                if (inflater.needsDictionary()) {
                    if (dictionary == null) {
                        throw new DecompressionException("decompression failure, unable to set dictionary as non was specified");
                    }
                    inflater.setDictionary(dictionary);
                }
            }
            if (inflater.finished()) {
                if (crc == null) {
                    // Do not decode anymore.
                    finished = true;
                } else {
                    readFooter = true;
                }
                break;
            }
        }
        in.skipBytes(readableBytes - inflater.getRemaining());
        if (readFooter) {
            gzipState = GzipState.FOOTER_START;
            if (readGZIPFooter(in)) {
                finished = true;
            }
        }
    } catch (DataFormatException e) {
        throw new DecompressionException("decompression failure", e);
    } finally {
        if (decompressed.isReadable()) {
            out.add(decompressed);
        } else {
            decompressed.release();
        }
    }
}
Also used : DataFormatException(java.util.zip.DataFormatException) Inflater(java.util.zip.Inflater) ByteBuf(io.netty.buffer.ByteBuf)

Example 22 with Inflater

use of java.util.zip.Inflater in project android_frameworks_base by ResurrectionRemix.

the class StrictJarFile method getZipInputStream.

private InputStream getZipInputStream(ZipEntry ze) {
    if (ze.getMethod() == ZipEntry.STORED) {
        return new FDStream(fd, ze.getDataOffset(), ze.getDataOffset() + ze.getSize());
    } else {
        final FDStream wrapped = new FDStream(fd, ze.getDataOffset(), ze.getDataOffset() + ze.getCompressedSize());
        int bufSize = Math.max(1024, (int) Math.min(ze.getSize(), 65535L));
        return new ZipInflaterInputStream(wrapped, new Inflater(true), bufSize, ze);
    }
}
Also used : Inflater(java.util.zip.Inflater)

Example 23 with Inflater

use of java.util.zip.Inflater in project voltdb by VoltDB.

the class AbstractHistogram method decodeFromCompressedByteBuffer.

static <T extends AbstractHistogram> T decodeFromCompressedByteBuffer(final ByteBuffer buffer, final Class<T> histogramClass, final long minBarForHighestTrackableValue) throws DataFormatException {
    int initialTargetPosition = buffer.position();
    final int cookie = buffer.getInt();
    final int headerSize;
    if ((getCookieBase(cookie) == compressedEncodingCookieBase) || (getCookieBase(cookie) == V1CompressedEncodingCookieBase)) {
        headerSize = ENCODING_HEADER_SIZE;
    } else if (getCookieBase(cookie) == V0CompressedEncodingCookieBase) {
        headerSize = V0_ENCODING_HEADER_SIZE;
    } else {
        throw new IllegalArgumentException("The buffer does not contain a compressed Histogram");
    }
    final int lengthOfCompressedContents = buffer.getInt();
    final Inflater decompressor = new Inflater();
    if (buffer.hasArray()) {
        decompressor.setInput(buffer.array(), initialTargetPosition + 8, lengthOfCompressedContents);
    } else {
        byte[] compressedContents = new byte[lengthOfCompressedContents];
        buffer.get(compressedContents);
        decompressor.setInput(compressedContents);
    }
    final ByteBuffer headerBuffer = ByteBuffer.allocate(headerSize).order(BIG_ENDIAN);
    decompressor.inflate(headerBuffer.array());
    T histogram = decodeFromByteBuffer(headerBuffer, histogramClass, minBarForHighestTrackableValue, decompressor);
    return histogram;
}
Also used : Inflater(java.util.zip.Inflater) ByteBuffer(java.nio.ByteBuffer)

Example 24 with Inflater

use of java.util.zip.Inflater in project voltdb by VoltDB.

the class FileArchiver method unarchive.

public static void unarchive(String infilename, String outfilename, FileAccess storage, int compressionType) throws IOException {
    InputStream f = null;
    OutputStream outstream = null;
    boolean completed = false;
    try {
        if (!storage.isStreamElement(infilename)) {
            return;
        }
        storage.removeElement(outfilename);
        byte[] b = new byte[COPY_BLOCK_SIZE];
        f = storage.openInputStreamElement(infilename);
        switch(compressionType) {
            case COMPRESSION_ZIP:
                f = new InflaterInputStream(f, new Inflater());
                break;
            case COMPRESSION_GZIP:
                f = new GZIPInputStream(f, b.length);
                break;
            case COMPRESSION_NONE:
                break;
            default:
                throw new RuntimeException("FileArchiver: " + compressionType);
        }
        outstream = storage.openOutputStreamElement(outfilename);
        while (true) {
            int l = f.read(b, 0, b.length);
            if (l == -1) {
                break;
            }
            outstream.write(b, 0, l);
        }
        completed = true;
    } catch (Throwable e) {
        throw FileUtil.toIOException(e);
    } finally {
        try {
            if (f != null) {
                f.close();
            }
            if (outstream != null) {
                outstream.flush();
                outstream.close();
            }
            if (!completed && storage.isStreamElement(outfilename)) {
                storage.removeElement(outfilename);
            }
        } catch (Throwable e) {
            throw FileUtil.toIOException(e);
        }
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) OutputStream(java.io.OutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) Inflater(java.util.zip.Inflater)

Example 25 with Inflater

use of java.util.zip.Inflater in project voltdb by VoltDB.

the class ScriptReaderZipped method openFile.

protected void openFile() throws IOException {
    InputStream d = db.isFilesInJar() ? getClass().getResourceAsStream(fileName) : db.getFileAccess().openInputStreamElement(fileName);
    dataStreamIn = new DataInputStream(new BufferedInputStream(new InflaterInputStream(d, new Inflater()), 1 << 13));
}
Also used : BufferedInputStream(java.io.BufferedInputStream) DataInputStream(java.io.DataInputStream) BufferedInputStream(java.io.BufferedInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) Inflater(java.util.zip.Inflater) DataInputStream(java.io.DataInputStream)

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