Search in sources :

Example 41 with Inflater

use of java.util.zip.Inflater in project WordPress-Android by wordpress-mobile.

the class Note method buildFromBase64EncodedData.

public static synchronized Note buildFromBase64EncodedData(String noteId, String base64FullNoteData) {
    Note note = null;
    if (base64FullNoteData == null)
        return null;
    byte[] b64DecodedPayload = Base64.decode(base64FullNoteData, Base64.DEFAULT);
    // Decompress the payload
    Inflater decompresser = new Inflater();
    decompresser.setInput(b64DecodedPayload, 0, b64DecodedPayload.length);
    //max length an Android PN payload can have
    byte[] result = new byte[4096];
    int resultLength = 0;
    try {
        resultLength = decompresser.inflate(result);
        decompresser.end();
    } catch (DataFormatException e) {
        AppLog.e(AppLog.T.NOTIFS, "Can't decompress the PN Payload. It could be > 4K", e);
    }
    String out = null;
    try {
        out = new String(result, 0, resultLength, "UTF8");
    } catch (UnsupportedEncodingException e) {
        AppLog.e(AppLog.T.NOTIFS, "Notification data contains non UTF8 characters.", e);
    }
    if (out != null) {
        try {
            JSONObject jsonObject = new JSONObject(out);
            if (jsonObject.has("notes")) {
                JSONArray jsonArray = jsonObject.getJSONArray("notes");
                if (jsonArray != null && jsonArray.length() == 1) {
                    jsonObject = jsonArray.getJSONObject(0);
                }
            }
            note = new Note(noteId, jsonObject);
        } catch (JSONException e) {
            AppLog.e(AppLog.T.NOTIFS, "Can't parse the Note JSON received in the PN", e);
        }
    }
    return note;
}
Also used : DataFormatException(java.util.zip.DataFormatException) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JSONException(org.json.JSONException) Inflater(java.util.zip.Inflater)

Example 42 with Inflater

use of java.util.zip.Inflater in project oxCore by GluuFederation.

the class CompressionHelper method inflate.

public static byte[] inflate(byte[] data, boolean nowrap) throws IOException, DataFormatException {
    Inflater inflater = new Inflater(nowrap);
    inflater.setInput(data);
    ByteArrayOutputStream os = new ByteArrayOutputStream(data.length);
    try {
        byte[] buffer = new byte[1024];
        while (!inflater.finished()) {
            int count = inflater.inflate(buffer);
            os.write(buffer, 0, count);
        }
    } finally {
        IOUtils.closeQuietly(os);
    }
    return os.toByteArray();
}
Also used : Inflater(java.util.zip.Inflater) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 43 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 44 with Inflater

use of java.util.zip.Inflater in project jdk8u_jdk by JetBrains.

the class PNGImageReader method readImage.

private void readImage(ImageReadParam param) throws IIOException {
    readMetadata();
    int width = metadata.IHDR_width;
    int height = metadata.IHDR_height;
    // Init default values
    sourceXSubsampling = 1;
    sourceYSubsampling = 1;
    sourceMinProgressivePass = 0;
    sourceMaxProgressivePass = 6;
    sourceBands = null;
    destinationBands = null;
    destinationOffset = new Point(0, 0);
    // If an ImageReadParam is available, get values from it
    if (param != null) {
        sourceXSubsampling = param.getSourceXSubsampling();
        sourceYSubsampling = param.getSourceYSubsampling();
        sourceMinProgressivePass = Math.max(param.getSourceMinProgressivePass(), 0);
        sourceMaxProgressivePass = Math.min(param.getSourceMaxProgressivePass(), 6);
        sourceBands = param.getSourceBands();
        destinationBands = param.getDestinationBands();
        destinationOffset = param.getDestinationOffset();
    }
    Inflater inf = null;
    try {
        stream.seek(imageStartPosition);
        Enumeration<InputStream> e = new PNGImageDataEnumeration(stream);
        InputStream is = new SequenceInputStream(e);
        /* InflaterInputStream uses an Inflater instance which consumes
            * native (non-GC visible) resources. This is normally implicitly
            * freed when the stream is closed. However since the
            * InflaterInputStream wraps a client-supplied input stream,
            * we cannot close it.
            * But the app may depend on GC finalization to close the stream.
            * Therefore to ensure timely freeing of native resources we
            * explicitly create the Inflater instance and free its resources
            * when we are done with the InflaterInputStream by calling
            * inf.end();
            */
        inf = new Inflater();
        is = new InflaterInputStream(is, inf);
        is = new BufferedInputStream(is);
        this.pixelStream = new DataInputStream(is);
        /*
             * NB: the PNG spec declares that valid range for width
             * and height is [1, 2^31-1], so here we may fail to allocate
             * a buffer for destination image due to memory limitation.
             *
             * However, the recovery strategy for this case should be
             * defined on the level of application, so we will not
             * try to estimate the required amount of the memory and/or
             * handle OOM in any way.
             */
        theImage = getDestination(param, getImageTypes(0), width, height);
        Rectangle destRegion = new Rectangle(0, 0, 0, 0);
        sourceRegion = new Rectangle(0, 0, 0, 0);
        computeRegions(param, width, height, theImage, sourceRegion, destRegion);
        destinationOffset.setLocation(destRegion.getLocation());
        // At this point the header has been read and we know
        // how many bands are in the image, so perform checking
        // of the read param.
        int colorType = metadata.IHDR_colorType;
        checkReadParamBandSettings(param, inputBandsForColorType[colorType], theImage.getSampleModel().getNumBands());
        processImageStarted(0);
        decodeImage();
        if (abortRequested()) {
            processReadAborted();
        } else {
            processImageComplete();
        }
    } catch (IOException e) {
        throw new IIOException("Error reading PNG image data", e);
    } finally {
        if (inf != null) {
            inf.end();
        }
    }
}
Also used : DataInputStream(java.io.DataInputStream) BufferedInputStream(java.io.BufferedInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) SubImageInputStream(com.sun.imageio.plugins.common.SubImageInputStream) SequenceInputStream(java.io.SequenceInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) Rectangle(java.awt.Rectangle) IIOException(javax.imageio.IIOException) Point(java.awt.Point) IIOException(javax.imageio.IIOException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) Point(java.awt.Point) SequenceInputStream(java.io.SequenceInputStream) BufferedInputStream(java.io.BufferedInputStream) Inflater(java.util.zip.Inflater)

Example 45 with Inflater

use of java.util.zip.Inflater in project ceylon-compiler by ceylon.

the class ZipFileIndex method inflate.

private int inflate(byte[] src, byte[] dest) {
    Inflater inflater = (inflaterRef == null ? null : inflaterRef.get());
    // construct the inflater object or reuse an existing one
    if (inflater == null)
        inflaterRef = new SoftReference<Inflater>(inflater = new Inflater(true));
    inflater.reset();
    inflater.setInput(src);
    try {
        return inflater.inflate(dest);
    } catch (DataFormatException ex) {
        return -1;
    }
}
Also used : DataFormatException(java.util.zip.DataFormatException) SoftReference(java.lang.ref.SoftReference) 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