Search in sources :

Example 6 with Inflater

use of java.util.zip.Inflater in project Notes by MiCode.

the class GTaskClient method getResponseContent.

private String getResponseContent(HttpEntity entity) throws IOException {
    String contentEncoding = null;
    if (entity.getContentEncoding() != null) {
        contentEncoding = entity.getContentEncoding().getValue();
        Log.d(TAG, "encoding: " + contentEncoding);
    }
    InputStream input = entity.getContent();
    if (contentEncoding != null && contentEncoding.equalsIgnoreCase("gzip")) {
        input = new GZIPInputStream(entity.getContent());
    } else if (contentEncoding != null && contentEncoding.equalsIgnoreCase("deflate")) {
        Inflater inflater = new Inflater(true);
        input = new InflaterInputStream(entity.getContent(), inflater);
    }
    try {
        InputStreamReader isr = new InputStreamReader(input);
        BufferedReader br = new BufferedReader(isr);
        StringBuilder sb = new StringBuilder();
        while (true) {
            String buff = br.readLine();
            if (buff == null) {
                return sb.toString();
            }
            sb = sb.append(buff);
        }
    } finally {
        input.close();
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) InputStreamReader(java.io.InputStreamReader) GZIPInputStream(java.util.zip.GZIPInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) BufferedReader(java.io.BufferedReader) Inflater(java.util.zip.Inflater)

Example 7 with Inflater

use of java.util.zip.Inflater in project Mycat-Server by MyCATApache.

the class CompressUtil method decompress.

/**
	 * 适用于mysql与客户端交互时zlib解压
	 *
	 * @param data  数据
	 * @param off   偏移量
	 * @param len   长度
	 * @return
	 */
public static byte[] decompress(byte[] data, int off, int len) {
    byte[] output = null;
    Inflater decompresser = new Inflater();
    decompresser.reset();
    decompresser.setInput(data, off, len);
    ByteArrayOutputStream out = new ByteArrayOutputStream(data.length);
    try {
        byte[] result = new byte[1024];
        while (!decompresser.finished()) {
            int i = decompresser.inflate(result);
            out.write(result, 0, i);
        }
        output = out.toByteArray();
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        try {
            out.close();
        } catch (Exception e) {
        }
        decompresser.end();
    }
    return output;
}
Also used : Inflater(java.util.zip.Inflater) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 8 with Inflater

use of java.util.zip.Inflater in project phonegap-facebook-plugin by Wizcorp.

the class SpdyReader method newNameValueBlockStream.

private DataInputStream newNameValueBlockStream() {
    // Limit the inflater input stream to only those bytes in the Name/Value block.
    final InputStream throttleStream = new InputStream() {

        @Override
        public int read() throws IOException {
            return Util.readSingleByte(this);
        }

        @Override
        public int read(byte[] buffer, int offset, int byteCount) throws IOException {
            byteCount = Math.min(byteCount, compressedLimit);
            int consumed = in.read(buffer, offset, byteCount);
            compressedLimit -= consumed;
            return consumed;
        }

        @Override
        public void close() throws IOException {
            in.close();
        }
    };
    // Subclass inflater to install a dictionary when it's needed.
    Inflater inflater = new Inflater() {

        @Override
        public int inflate(byte[] buffer, int offset, int count) throws DataFormatException {
            int result = super.inflate(buffer, offset, count);
            if (result == 0 && needsDictionary()) {
                setDictionary(DICTIONARY);
                result = super.inflate(buffer, offset, count);
            }
            return result;
        }
    };
    return new DataInputStream(new InflaterInputStream(throttleStream, inflater));
}
Also used : DataInputStream(java.io.DataInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) Inflater(java.util.zip.Inflater) DataInputStream(java.io.DataInputStream)

Example 9 with Inflater

use of java.util.zip.Inflater in project mapdb by jankotek.

the class SerializerCompressionDeflateWrapper method deserialize.

@Override
public E deserialize(DataInput2 in, int available) throws IOException {
    final int unpackedSize = in.unpackInt() - 1;
    if (unpackedSize == -1) {
        //was not compressed
        return serializer.deserialize(in, available > 0 ? available - 1 : available);
    }
    Inflater inflater = new Inflater();
    if (dictionary != null) {
        inflater.setDictionary(dictionary);
    }
    InflaterInputStream in4 = new InflaterInputStream(new DataInput2.DataInputToStream(in), inflater);
    byte[] unpacked = new byte[unpackedSize];
    in4.read(unpacked, 0, unpackedSize);
    DataInput2.ByteArray in2 = new DataInput2.ByteArray(unpacked);
    E ret = serializer.deserialize(in2, unpackedSize);
    if (CC.ASSERT && !(in2.pos == unpackedSize))
        throw new DBException.DataCorruption("data were not fully read");
    return ret;
}
Also used : InflaterInputStream(java.util.zip.InflaterInputStream) Inflater(java.util.zip.Inflater)

Example 10 with Inflater

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

the class DeflateEncoder method decode.

@Override
public InputStream decode(String contentEncoding, InputStream encodedStream) throws IOException {
    // correct impl. should wrap deflate in zlib, but some don't do it - have to identify, which one we got
    InputStream markSupportingStream = encodedStream.markSupported() ? encodedStream : new BufferedInputStream(encodedStream);
    markSupportingStream.mark(1);
    // read the first byte
    int firstByte = markSupportingStream.read();
    markSupportingStream.reset();
    // that should never be the case if no zlib wrapper
    if ((firstByte & 15) == 8) {
        // ok, zlib wrapped stream
        return new InflaterInputStream(markSupportingStream);
    } else {
        // no zlib wrapper
        return new InflaterInputStream(markSupportingStream, new Inflater(true));
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) 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