Search in sources :

Example 31 with Inflater

use of java.util.zip.Inflater in project undertow by undertow-io.

the class CompressionUtilsTest method setup.

@Before
public void setup() throws Exception {
    compress = new Deflater(Deflater.BEST_SPEED, true);
    decompress = new Inflater(true);
}
Also used : Deflater(java.util.zip.Deflater) Inflater(java.util.zip.Inflater) Before(org.junit.Before)

Example 32 with Inflater

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

the class BinaryEncoder method decode.

public static void decode(@NotNull ListIterator<String> input, long size, @NotNull ByteArrayOutputStream output) throws BinaryPatchException {
    Inflater inflater = new Inflater();
    byte[] inflated = new byte[1024];
    try {
        String line = input.next();
        while (line != null && line.length() > 0) {
            int len = getLineSizeFromChar(line.charAt(0));
            byte[] toInflate = Base85x.decode(line.substring(1));
            inflater.setInput(toInflate, 0, len);
            int resultLength;
            while (!inflater.needsInput()) {
                try {
                    resultLength = inflater.inflate(inflated);
                } catch (DataFormatException e) {
                    throw new BinaryPatchException("Can't decode binary file patch: can't decompress data");
                }
                output.write(inflated, 0, resultLength);
            }
            if (!input.hasNext())
                break;
            line = input.next();
        }
        int count = output.size();
        if (count != size) {
            throw new BinaryPatchException(String.format("Length of decoded binary patch mismatches: expected %d, received %d", size, count));
        }
    } catch (Base85x.Base85FormatException e) {
        throw new BinaryPatchException(e);
    } finally {
        inflater.end();
    }
}
Also used : DataFormatException(java.util.zip.DataFormatException) Inflater(java.util.zip.Inflater) Base85x(com.intellij.openapi.diff.impl.patch.lib.base85xjava.Base85x)

Example 33 with Inflater

use of java.util.zip.Inflater in project teaTime by ancfdy.

the class AppZLibMgr method decompressEx.

/**
     * 解压缩
     *
     * @param data
     *            待压缩的数据
     * @return byte[] 解压缩后的数据
     */
public static byte[] decompressEx(byte[] data, int offset, int len) {
    byte[] output = new byte[0];
    Inflater decompresser = new Inflater();
    decompresser.reset();
    decompresser.setInput(data, offset, len);
    ByteArrayOutputStream o = new ByteArrayOutputStream(data.length);
    try {
        byte[] buf = new byte[1024];
        while (!decompresser.finished()) {
            int i = decompresser.inflate(buf);
            o.write(buf, 0, i);
        }
        output = o.toByteArray();
    } catch (Exception e) {
        output = data;
        e.printStackTrace();
    } finally {
        try {
            o.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    decompresser.end();
    return output;
}
Also used : Inflater(java.util.zip.Inflater) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) IOException(java.io.IOException)

Example 34 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 35 with Inflater

use of java.util.zip.Inflater in project teaTime by ancfdy.

the class AppZLibMgr method decompress.

/**
     * 解压缩
     *
     * @param data
     *            待压缩的数据
     * @return byte[] 解压缩后的数据
     */
public static byte[] decompress(byte[] data) {
    byte[] output = new byte[0];
    Inflater decompresser = new Inflater();
    decompresser.reset();
    decompresser.setInput(data);
    ByteArrayOutputStream o = new ByteArrayOutputStream(data.length);
    try {
        byte[] buf = new byte[1024];
        while (!decompresser.finished()) {
            int i = decompresser.inflate(buf);
            o.write(buf, 0, i);
        }
        output = o.toByteArray();
    } catch (Exception e) {
        output = data;
        e.printStackTrace();
    } finally {
        try {
            o.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    decompresser.end();
    return output;
}
Also used : Inflater(java.util.zip.Inflater) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) IOException(java.io.IOException)

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