Search in sources :

Example 66 with Inflater

use of java.util.zip.Inflater in project jetty.project by eclipse.

the class CompressExtension method decompress.

protected void decompress(ByteAccumulator accumulator, ByteBuffer buf) throws DataFormatException {
    if ((buf == null) || (!buf.hasRemaining())) {
        return;
    }
    byte[] output = new byte[DECOMPRESS_BUF_SIZE];
    Inflater inflater = getInflater();
    while (buf.hasRemaining() && inflater.needsInput()) {
        if (!supplyInput(inflater, buf)) {
            LOG.debug("Needed input, but no buffer could supply input");
            return;
        }
        int read;
        while ((read = inflater.inflate(output)) >= 0) {
            if (read == 0) {
                LOG.debug("Decompress: read 0 {}", toDetail(inflater));
                break;
            } else {
                // do something with output
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Decompressed {} bytes: {}", read, toDetail(inflater));
                }
                accumulator.copyChunk(output, 0, read);
            }
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Decompress: exiting {}", toDetail(inflater));
    }
}
Also used : Inflater(java.util.zip.Inflater)

Example 67 with Inflater

use of java.util.zip.Inflater in project MusicDNA by harjot-oberai.

the class ID3Compression method uncompress.

/**
     * Decompress realFrameSize bytes to decompressedFrameSize bytes and return as ByteBuffer
     *
     * @param byteBuffer
     * @param decompressedFrameSize
     * @param realFrameSize
     * @return
     * @throws org.jaudiotagger.tag.InvalidFrameException
     *
     */
protected static ByteBuffer uncompress(String identifier, String filename, ByteBuffer byteBuffer, int decompressedFrameSize, int realFrameSize) throws InvalidFrameException {
    logger.config(filename + ":About to decompress " + realFrameSize + " bytes, expect result to be:" + decompressedFrameSize + " bytes");
    // Decompress the bytes into this buffer, size initialized from header field
    byte[] result = new byte[decompressedFrameSize];
    byte[] input = new byte[realFrameSize];
    //Store position ( just after frame header and any extra bits)
    //Read frame data into array, and then put buffer back to where it was
    int position = byteBuffer.position();
    byteBuffer.get(input, 0, realFrameSize);
    byteBuffer.position(position);
    Inflater decompresser = new Inflater();
    decompresser.setInput(input);
    try {
        int inflatedTo = decompresser.inflate(result);
        logger.config(filename + ":Decompressed to " + inflatedTo + " bytes");
    } catch (DataFormatException dfe) {
        logger.log(Level.CONFIG, "Unable to decompress this frame:" + identifier, dfe);
        //Update position of main buffer, so no attempt is made to reread these bytes
        byteBuffer.position(byteBuffer.position() + realFrameSize);
        throw new InvalidFrameException(ErrorMessage.ID3_UNABLE_TO_DECOMPRESS_FRAME.getMsg(identifier, filename, dfe.getMessage()));
    }
    decompresser.end();
    return ByteBuffer.wrap(result);
}
Also used : DataFormatException(java.util.zip.DataFormatException) Inflater(java.util.zip.Inflater) InvalidFrameException(org.jaudiotagger.tag.InvalidFrameException)

Example 68 with Inflater

use of java.util.zip.Inflater in project cassandra by apache.

the class DeflateCompressor method uncompressBuffer.

public void uncompressBuffer(ByteBuffer input, ByteBuffer output) throws IOException {
    try {
        Inflater inf = inflater.get();
        inf.reset();
        byte[] buffer = getThreadLocalScratchBuffer();
        // Use half the buffer for input, half for output.
        int chunkLen = buffer.length / 2;
        while (input.remaining() > chunkLen) {
            input.get(buffer, 0, chunkLen);
            inf.setInput(buffer, 0, chunkLen);
            while (!inf.needsInput()) {
                int len = inf.inflate(buffer, chunkLen, chunkLen);
                output.put(buffer, chunkLen, len);
            }
        }
        int inputLength = input.remaining();
        input.get(buffer, 0, inputLength);
        inf.setInput(buffer, 0, inputLength);
        while (!inf.needsInput()) {
            int len = inf.inflate(buffer, chunkLen, chunkLen);
            output.put(buffer, chunkLen, len);
        }
    } catch (DataFormatException e) {
        throw new IOException(e);
    }
}
Also used : DataFormatException(java.util.zip.DataFormatException) Inflater(java.util.zip.Inflater) IOException(java.io.IOException)

Example 69 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 70 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)

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