Search in sources :

Example 1 with InflaterInputStream

use of java.util.zip.InflaterInputStream in project elasticsearch by elastic.

the class DeflateCompressor method streamInput.

@Override
public StreamInput streamInput(StreamInput in) throws IOException {
    final byte[] headerBytes = new byte[HEADER.length];
    int len = 0;
    while (len < headerBytes.length) {
        final int read = in.read(headerBytes, len, headerBytes.length - len);
        if (read == -1) {
            break;
        }
        len += read;
    }
    if (len != HEADER.length || Arrays.equals(headerBytes, HEADER) == false) {
        throw new IllegalArgumentException("Input stream is not compressed with DEFLATE!");
    }
    final boolean nowrap = true;
    final Inflater inflater = new Inflater(nowrap);
    InputStream decompressedIn = new InflaterInputStream(in, inflater, BUFFER_SIZE);
    decompressedIn = new BufferedInputStream(decompressedIn, BUFFER_SIZE);
    return new InputStreamStreamInput(decompressedIn) {

        final AtomicBoolean closed = new AtomicBoolean(false);

        public void close() throws IOException {
            try {
                super.close();
            } finally {
                if (closed.compareAndSet(false, true)) {
                    // important to release native memory
                    inflater.end();
                }
            }
        }
    };
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) 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) InputStreamStreamInput(org.elasticsearch.common.io.stream.InputStreamStreamInput)

Example 2 with InflaterInputStream

use of java.util.zip.InflaterInputStream 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)

Example 3 with InflaterInputStream

use of java.util.zip.InflaterInputStream in project nutz by nutzam.

the class Sender method createResponse.

protected Response createResponse(Map<String, String> reHeaders) throws IOException {
    Response rep = null;
    if (reHeaders != null) {
        rep = new Response(conn, reHeaders);
        if (rep.isOK()) {
            InputStream is1 = conn.getInputStream();
            InputStream is2 = null;
            String encoding = conn.getContentEncoding();
            // 如果采用了压缩,则需要处理否则都是乱码
            if (encoding != null && encoding.contains("gzip")) {
                is2 = new GZIPInputStream(is1);
            } else if (encoding != null && encoding.contains("deflate")) {
                is2 = new InflaterInputStream(is1, new Inflater(true));
            } else {
                is2 = is1;
            }
            BufferedInputStream is = new BufferedInputStream(is2);
            rep.setStream(is);
        } else {
            try {
                rep.setStream(conn.getInputStream());
            } catch (IOException e) {
                try {
                    rep.setStream(conn.getErrorStream());
                } catch (Exception e1) {
                    rep.setStream(new VoidInputStream());
                }
            }
        }
    }
    if (this.interceptor != null)
        this.interceptor.afterResponse(request, conn, rep);
    return rep;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) VoidInputStream(org.nutz.lang.stream.VoidInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) VoidInputStream(org.nutz.lang.stream.VoidInputStream) Inflater(java.util.zip.Inflater) IOException(java.io.IOException) IOException(java.io.IOException)

Example 4 with InflaterInputStream

use of java.util.zip.InflaterInputStream in project OpenAM by OpenRock.

the class IOUtils method deserialise.

/**
     * Deserialises an object from a byte array to an object of a specified type.
     *
     * @param bytes The bytes that represent the Object to be deserialized. The classes to be loaded must be from the
     *              set specified in the whitelist maintained in the <code>WhitelistObjectInputStream</code>
     * @param compressed If true, expect that the bytes are compressed.
     * @param classLoader Used in place of the default ClassLoader, default will be used if null.
     * @param <T> The returned object type.
     * @return The Object T representing the deserialized bytes
     * @throws IOException If there was a problem with the ObjectInputStream process.
     * @throws ClassNotFoundException If there was problem loading a class that makes up the bytes to be deserialized.
     */
public static <T> T deserialise(byte[] bytes, boolean compressed, ClassLoader classLoader) throws IOException, ClassNotFoundException {
    final ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    final ObjectInputStream ois = compressed ? new WhitelistObjectInputStream(new InflaterInputStream(bais), classLoader) : new WhitelistObjectInputStream(bais, classLoader);
    final T result;
    try {
        result = (T) ois.readObject();
    } finally {
        closeIfNotNull(ois);
    }
    return result;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 5 with InflaterInputStream

use of java.util.zip.InflaterInputStream in project voltdb by VoltDB.

the class Encoder method decodeBase64AndDecompressToBytes.

public static byte[] decodeBase64AndDecompressToBytes(String string) {
    byte[] bytes = Base64.decodeFast(string);
    if (string.length() == 0) {
        return new byte[0];
    }
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    InflaterInputStream dis = new InflaterInputStream(bais);
    byte[] buffer = new byte[1024 * 8];
    int length = 0;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        while ((length = dis.read(buffer)) >= 0) {
            baos.write(buffer, 0, length);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return baos.toByteArray();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

InflaterInputStream (java.util.zip.InflaterInputStream)200 ByteArrayInputStream (java.io.ByteArrayInputStream)113 InputStream (java.io.InputStream)100 IOException (java.io.IOException)74 Inflater (java.util.zip.Inflater)66 ByteArrayOutputStream (java.io.ByteArrayOutputStream)50 GZIPInputStream (java.util.zip.GZIPInputStream)39 DataInputStream (java.io.DataInputStream)33 FileInputStream (java.io.FileInputStream)27 BufferedInputStream (java.io.BufferedInputStream)24 InputStreamReader (java.io.InputStreamReader)13 DeflaterOutputStream (java.util.zip.DeflaterOutputStream)13 HttpURLConnection (java.net.HttpURLConnection)12 URL (java.net.URL)11 File (java.io.File)10 BufferedReader (java.io.BufferedReader)9 OutputStream (java.io.OutputStream)9 Point (java.awt.Point)7 EOFException (java.io.EOFException)7 URLConnection (java.net.URLConnection)7