Search in sources :

Example 46 with Inflater

use of java.util.zip.Inflater in project intellij-plugins by JetBrains.

the class SwfTranscoder method readSource.

// in will be closed
protected void readSource(InputStream in, long inputLength) throws IOException {
    final int uncompressedBodyLength;
    final boolean compressed;
    byte[] data;
    try {
        int n = in.read(partialHeader);
        assert n == PARTIAL_HEADER_LENGTH;
        uncompressedBodyLength = (partialHeader[4] & 0xFF | (partialHeader[5] & 0xFF) << 8 | (partialHeader[6] & 0xFF) << 16 | partialHeader[7] << 24) - PARTIAL_HEADER_LENGTH;
        compressed = partialHeader[0] == 0x43;
        data = FileUtil.loadBytes(in, compressed ? (int) inputLength - PARTIAL_HEADER_LENGTH : uncompressedBodyLength);
    } finally {
        in.close();
    }
    if (compressed) {
        final Inflater inflater = INFLATER.get();
        try {
            inflater.setInput(data);
            byte[] uncompressedData = new byte[uncompressedBodyLength];
            try {
                inflater.inflate(uncompressedData);
            } catch (DataFormatException e) {
                throw new ZipException(e.getMessage() != null ? e.getMessage() : "Invalid ZLIB data format");
            }
            data = uncompressedData;
        } finally {
            inflater.reset();
        }
    }
    buffer = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN);
    readFrameSizeFrameRateAndFrameCount(data[0]);
}
Also used : DataFormatException(java.util.zip.DataFormatException) Inflater(java.util.zip.Inflater) ZipException(java.util.zip.ZipException)

Example 47 with Inflater

use of java.util.zip.Inflater in project ddf by codice.

the class RestSecurity method inflateBase64.

public static String inflateBase64(String base64EncodedValue) throws IOException {
    byte[] deflatedValue = Base64.getMimeDecoder().decode(base64EncodedValue);
    InputStream is = new InflaterInputStream(new ByteArrayInputStream(deflatedValue), new Inflater(GZIP_COMPATIBLE));
    return IOUtils.toString(is, StandardCharsets.UTF_8.name());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) Inflater(java.util.zip.Inflater)

Example 48 with Inflater

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

the class CompressedArray method decompress.

/**
 * Decompress the byte array previously returned by compress
 */
public static byte[] decompress(byte[] value) throws DataFormatException {
    // Create an expandable byte array to hold the decompressed data
    ByteArrayOutputStream bos = new ByteArrayOutputStream(value.length);
    Inflater decompressor = new Inflater();
    try {
        decompressor.setInput(value);
        final byte[] buf = new byte[1024];
        while (!decompressor.finished()) {
            int count = decompressor.inflate(buf);
            bos.write(buf, 0, count);
        }
    } finally {
        decompressor.end();
    }
    return bos.toByteArray();
}
Also used : Inflater(java.util.zip.Inflater) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GHPoint(com.graphhopper.util.shapes.GHPoint)

Example 49 with Inflater

use of java.util.zip.Inflater in project inbot-utils by Inbot.

the class CompressionUtils method decompress.

public static byte[] decompress(byte[] compressed) {
    Inflater inflater = new Inflater();
    inflater.setInput(compressed);
    ByteArrayOutputStream bos = new ByteArrayOutputStream(compressed.length);
    // Compress the data
    byte[] buf = new byte[1024];
    try {
        while (!inflater.finished()) {
            int count = inflater.inflate(buf);
            bos.write(buf, 0, count);
        }
        bos.close();
    } catch (IOException e) {
        throw new IllegalArgumentException("could not decompress");
    } catch (DataFormatException e) {
        throw new IllegalArgumentException("could not decompress");
    }
    return bos.toByteArray();
}
Also used : DataFormatException(java.util.zip.DataFormatException) Inflater(java.util.zip.Inflater) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 50 with Inflater

use of java.util.zip.Inflater in project JGroups by belaban.

the class COMPRESS method uncompress.

/**
 * Returns a new message as a result of uncompressing msg, or null if msg couldn't be uncompressed
 */
protected Message uncompress(Message msg, int original_size) {
    byte[] compressed_payload = msg.getRawBuffer();
    if (compressed_payload != null && compressed_payload.length > 0) {
        byte[] uncompressed_payload = new byte[original_size];
        Inflater inflater = null;
        try {
            inflater = inflater_pool.take();
            inflater.reset();
            inflater.setInput(compressed_payload, msg.getOffset(), msg.getLength());
            try {
                inflater.inflate(uncompressed_payload);
                // we need to copy: https://jira.jboss.org/jira/browse/JGRP-867
                return msg.copy(false).setBuffer(uncompressed_payload);
            } catch (DataFormatException e) {
                log.error(Util.getMessage("CompressionFailure"), e);
            }
        } catch (InterruptedException e) {
            // set the interrupt bit again, so caller can handle it
            Thread.currentThread().interrupt();
        } finally {
            if (inflater != null)
                inflater_pool.offer(inflater);
        }
    }
    return null;
}
Also used : DataFormatException(java.util.zip.DataFormatException) Inflater(java.util.zip.Inflater)

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