Example usage for java.util.zip InflaterInputStream InflaterInputStream

List of usage examples for java.util.zip InflaterInputStream InflaterInputStream

Introduction

In this page you can find the example usage for java.util.zip InflaterInputStream InflaterInputStream.

Prototype

public InflaterInputStream(InputStream in, Inflater inf) 

Source Link

Document

Creates a new input stream with the specified decompressor and a default buffer size.

Usage

From source file:Main.java

public static InputStream getInputEncoding(URLConnection connection) throws IOException {
    InputStream in;/*w  w  w  . jav a 2 s. c  o  m*/
    String encoding = connection.getContentEncoding();
    if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
        in = new GZIPInputStream(connection.getInputStream());
    } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
        in = new InflaterInputStream(connection.getInputStream(), new Inflater(true));
    } else {
        in = connection.getInputStream();
    }
    return in;
}

From source file:Main.java

static String downloadHtml(String urlString) {
    StringBuffer buffer = new StringBuffer();

    try {/*from  ww  w.  j a v  a 2 s.  co  m*/
        URL url = new URL(urlString);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        HttpURLConnection.setFollowRedirects(true);
        conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
        String encoding = conn.getContentEncoding();
        InputStream inStr = null;

        if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
            inStr = new GZIPInputStream(conn.getInputStream());
        } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
            inStr = new InflaterInputStream(conn.getInputStream(), new Inflater(true));
        } else {
            inStr = conn.getInputStream();
        }
        int ptr = 0;
        InputStreamReader inStrReader = new InputStreamReader(inStr, Charset.forName("GB2312"));

        while ((ptr = inStrReader.read()) != -1) {
            buffer.append((char) ptr);
        }
        inStrReader.close();
        conn.disconnect();
        inStr.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return buffer.toString();
}

From source file:org.openmrs.module.shr.contenthandler.DataUtil.java

public static byte[] uncompressDeflate(byte[] content) throws IOException {
    InflaterInputStream deflateIn = new InflaterInputStream(new ByteArrayInputStream(content),
            new Inflater(true));
    return copyToByteArray(deflateIn);
}

From source file:com.barrybecker4.common.util.Base64Codec.java

/**
 * Take a String and decompress it.//from  w w w  .  j a  v a  2s.c om
 * @param data the compressed string to decompress.
 * @return the decompressed string.
 */
public static synchronized String decompress(final String data) {

    // convert from string to bytes for decompressing
    byte[] compressedDat = Base64.decodeBase64(data.getBytes());

    final ByteArrayInputStream in = new ByteArrayInputStream(compressedDat);
    final Inflater inflater = new Inflater();
    final InflaterInputStream iStream = new InflaterInputStream(in, inflater);
    final char cBuffer[] = new char[4096];
    StringBuilder sBuf = new StringBuilder();
    try {
        InputStreamReader iReader = new InputStreamReader(iStream, CONVERTER_UTF8);
        while (true) {
            final int numRead = iReader.read(cBuffer);
            if (numRead == -1) {
                break;
            }
            sBuf.append(cBuffer, 0, numRead);
        }
    } catch (UnsupportedEncodingException e) {
        throw new IllegalArgumentException("Unsupported encoding exception :" + e.getMessage(), e);
    } catch (IOException e) {
        throw new IllegalStateException("io error :" + e.getMessage(), e);
    }

    return sBuf.toString();
}

From source file:com.iflytek.spider.util.DeflateUtils.java

/**
 * Returns an inflated copy of the input array, truncated to
 * <code>sizeLimit</code> bytes, if necessary.  If the deflated input
 * has been truncated or corrupted, a best-effort attempt is made to
 * inflate as much as possible.  If no data can be extracted
 * <code>null</code> is returned.
 *//*  w  w w.ja  va  2  s.c om*/
public static final byte[] inflateBestEffort(byte[] in, int sizeLimit) {
    // decompress using InflaterInputStream 
    ByteArrayOutputStream outStream = new ByteArrayOutputStream(EXPECTED_COMPRESSION_RATIO * in.length);

    // "true" because HTTP does not provide zlib headers
    Inflater inflater = new Inflater(true);
    InflaterInputStream inStream = new InflaterInputStream(new ByteArrayInputStream(in), inflater);

    byte[] buf = new byte[BUF_SIZE];
    int written = 0;
    while (true) {
        try {
            int size = inStream.read(buf);
            if (size <= 0)
                break;
            if ((written + size) > sizeLimit) {
                outStream.write(buf, 0, sizeLimit - written);
                break;
            }
            outStream.write(buf, 0, size);
            written += size;
        } catch (Exception e) {
            LOG.info("Caught Exception in inflateBestEffort");
            e.printStackTrace(LogUtil.getWarnStream(LOG));
            break;
        }
    }
    try {
        outStream.close();
    } catch (IOException e) {
    }

    return outStream.toByteArray();
}

From source file:com.faceye.feature.util.http.DeflateUtils.java

/**
 * Returns an inflated copy of the input array, truncated to
 * <code>sizeLimit</code> bytes, if necessary.  If the deflated input
 * has been truncated or corrupted, a best-effort attempt is made to
 * inflate as much as possible.  If no data can be extracted
 * <code>null</code> is returned.
 *///from  www  . ja v a 2 s .  com
public static final byte[] inflateBestEffort(byte[] in, int sizeLimit) {
    // decompress using InflaterInputStream 
    ByteArrayOutputStream outStream = new ByteArrayOutputStream(EXPECTED_COMPRESSION_RATIO * in.length);

    // "true" because HTTP does not provide zlib headers
    Inflater inflater = new Inflater(true);
    InflaterInputStream inStream = new InflaterInputStream(new ByteArrayInputStream(in), inflater);

    byte[] buf = new byte[BUF_SIZE];
    int written = 0;
    while (true) {
        try {
            int size = inStream.read(buf);
            if (size <= 0)
                break;
            if ((written + size) > sizeLimit) {
                outStream.write(buf, 0, sizeLimit - written);
                break;
            }
            outStream.write(buf, 0, size);
            written += size;
        } catch (Exception e) {
            LOG.info("Caught Exception in inflateBestEffort", e);
            break;
        }
    }
    try {
        outStream.close();
    } catch (IOException e) {
    }

    return outStream.toByteArray();
}

From source file:net.fenyo.mail4hotspot.service.Browser.java

public static String getHtml(final String target_url, final Cookie[] cookies) throws IOException {
    // log.debug("RETRIEVING_URL=" + target_url);
    final URL url = new URL(target_url);

    final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    //Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.69.60.6", 3128)); 
    // final HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy);

    //      HttpURLConnection.setFollowRedirects(true);
    // conn.setRequestProperty("User-agent", "my agent name");

    conn.setRequestProperty("Accept-Language", "en-US");

    //      conn.setRequestProperty(key, value);

    // allow both GZip and Deflate (ZLib) encodings
    conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
    final String encoding = conn.getContentEncoding();
    InputStream is = null;//w  w w .  j a va2 s.com
    // create the appropriate stream wrapper based on the encoding type
    if (encoding != null && encoding.equalsIgnoreCase("gzip"))
        is = new GZIPInputStream(conn.getInputStream());
    else if (encoding != null && encoding.equalsIgnoreCase("deflate"))
        is = new InflaterInputStream(conn.getInputStream(), new Inflater(true));
    else
        is = conn.getInputStream();

    final InputStreamReader reader = new InputStreamReader(new BufferedInputStream(is));

    final CharBuffer cb = CharBuffer.allocate(1024 * 1024);
    int ret;
    do {
        ret = reader.read(cb);
    } while (ret > 0);
    cb.flip();
    return cb.toString();
}

From source file:com.hundsun.jresplus.web.nosession.cookie.HessianZipSerializer.java

public static Object decode(byte[] encodedValue) throws SerializationException {
    ByteArrayInputStream bais = new ByteArrayInputStream(encodedValue);
    Inflater inf = new Inflater(false);
    InflaterInputStream iis = new InflaterInputStream(bais, inf);
    Hessian2Input hi = null;//  w  ww . j a  v a2s  . c om
    try {
        hi = new Hessian2Input(iis);
        return hi.readObject();
    } catch (Exception e) {
        throw new SerializationException("Failed to parse data", e);
    } finally {
        if (hi != null) {
            try {
                hi.close();
            } catch (IOException e) {
            }
        }
        try {
            iis.close();
        } catch (IOException e) {
        }
        inf.end();
    }
}

From source file:com.google.android.feeds.ContentHandlerUtils.java

/**
 * Returns the uncompressed {@link InputStream} for the given
 * {@link URLConnection}.//from w  ww  .  j  a  va  2 s.  c  om
 */
public static InputStream getUncompressedInputStream(URLConnection connection) throws IOException {
    InputStream source = connection.getInputStream();
    String encoding = connection.getContentEncoding();
    if ("gzip".equalsIgnoreCase(encoding)) {
        return new GZIPInputStream(source);
    } else if ("deflate".equalsIgnoreCase(encoding)) {
        boolean noHeader = true;
        Inflater inflater = new Inflater(noHeader);
        return new InflaterInputStream(source, inflater);
    } else {
        return source;
    }
}

From source file:com.tremolosecurity.unison.u2f.util.U2fUtil.java

private static String inflate(String saml) throws Exception {
    byte[] compressedData = Base64.decodeBase64(saml);
    ByteArrayInputStream bin = new ByteArrayInputStream(compressedData);

    InflaterInputStream decompressor = new InflaterInputStream(bin, new Inflater(true));
    //decompressor.setInput(compressedData);

    // Create an expandable byte array to hold the decompressed data
    ByteArrayOutputStream bos = new ByteArrayOutputStream(compressedData.length);

    // Decompress the data
    byte[] buf = new byte[1024];
    int len;/*from   w w  w.  ja v a  2s  .  c  o  m*/
    while ((len = decompressor.read(buf)) > 0) {

        bos.write(buf, 0, len);

    }
    try {
        bos.close();
    } catch (IOException e) {
    }

    // Get the decompressed data
    byte[] decompressedData = bos.toByteArray();

    String decoded = new String(decompressedData);

    return decoded;
}