Example usage for java.util.zip Inflater Inflater

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

Introduction

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

Prototype

public Inflater(boolean nowrap) 

Source Link

Document

Creates a new decompressor.

Usage

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  v a 2s. 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  w w w.jav  a2s.c o m*/
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:com.predic8.membrane.core.util.ByteUtil.java

public static byte[] getDecompressedData(byte[] compressedData) throws IOException {
    Inflater decompressor = new Inflater(true);
    decompressor.setInput(compressedData);

    byte[] buf = new byte[1024];

    List<Chunk> chunks = new ArrayList<Chunk>();

    while (!decompressor.finished()) {
        int count;
        try {/*w w  w. ja v a  2 s .com*/
            count = decompressor.inflate(buf);
        } catch (DataFormatException e) {
            throw new IOException(e);
        }
        if (buf.length == count) {
            Chunk chunk = new Chunk(buf);
            chunks.add(chunk);
        } else if (count < buf.length) {
            byte[] shortContent = new byte[count];
            for (int j = 0; j < count; j++) {
                shortContent[j] = buf[j];
            }
            Chunk chunk = new Chunk(shortContent);
            chunks.add(chunk);
        }
    }

    log.debug("Number of decompressed chunks: " + chunks.size());
    if (chunks.size() > 0) {

        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        for (Chunk chunk : chunks) {
            chunk.write(bos);
        }

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

    return null;
}

From source file:org.wso2.carbon.identity.sso.saml.tomcat.agent.Util.java

/**
 * Decoding and deflating the encoded AuthReq
 *
 * @param encodedStr//from w  w w. ja v a  2 s. co  m
 *            encoded AuthReq
 * @return decoded AuthReq
 */
public static String decode(String encodedStr) throws SSOAgentException {
    try {
        org.apache.commons.codec.binary.Base64 base64Decoder = new org.apache.commons.codec.binary.Base64();
        byte[] xmlBytes = encodedStr.getBytes("UTF-8");
        byte[] base64DecodedByteArray = base64Decoder.decode(xmlBytes);

        try {
            Inflater inflater = new Inflater(true);
            inflater.setInput(base64DecodedByteArray);
            byte[] xmlMessageBytes = new byte[5000];
            int resultLength = inflater.inflate(xmlMessageBytes);

            if (inflater.getRemaining() > 0) {
                throw new RuntimeException("didn't allocate enough space to hold " + "decompressed data");
            }

            inflater.end();
            return new String(xmlMessageBytes, 0, resultLength, "UTF-8");

        } catch (DataFormatException e) {
            ByteArrayInputStream bais = new ByteArrayInputStream(base64DecodedByteArray);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            InflaterInputStream iis = new InflaterInputStream(bais);
            byte[] buf = new byte[1024];
            int count = iis.read(buf);
            while (count != -1) {
                baos.write(buf, 0, count);
                count = iis.read(buf);
            }
            iis.close();
            return new String(baos.toByteArray());
        }
    } catch (IOException e) {
        throw new SSOAgentException("Error when decoding the SAML Request.", e);
    }
}

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;/*from   w  w  w. j a  v a2 s  .c o  m*/
    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: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;/*from   w  w w .j  a va 2  s  .  co  m*/
    // 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:ch.cern.security.saml2.utils.xml.XMLUtils.java

public static String xmlDecodeAndInflate(String encodedXmlString, boolean isDebugEnabled)
        throws UnsupportedEncodingException, DataFormatException {

    // URL decode
    // No need to URL decode: auto decoded by request.getParameter() method (GET)

    if (isDebugEnabled)
        nc.notice("Encoded XML String: " + encodedXmlString);

    // Base64 decode
    Base64 base64Decoder = new Base64();
    byte[] xmlBytes = encodedXmlString.getBytes("UTF-8");
    byte[] base64DecodedByteArray = base64Decoder.decode(xmlBytes);

    if (isDebugEnabled)
        nc.notice("Base64 decoded bytes: " + new String(base64DecodedByteArray, "UTF-8"));

    // Inflate (uncompress) the AuthnRequest data
    // First attempt to unzip the byte array according to DEFLATE (rfc 1951)
    Inflater inflater = new Inflater(true);
    inflater.setInput(base64DecodedByteArray);
    // since we are decompressing, it's impossible to know how much space we
    // might need; hopefully this number is suitably big
    byte[] xmlMessageBytes = new byte[5000];
    int resultLength = inflater.inflate(xmlMessageBytes);

    if (!inflater.finished()) {
        throw new RuntimeException("didn't allocate enough space to hold " + "decompressed data");
    }/* w  ww. ja  v  a  2 s. co m*/

    inflater.end();

    String decodedString = new String(xmlMessageBytes, 0, resultLength, "UTF-8");

    if (isDebugEnabled)
        nc.notice("Decoded and inflated string (UTF-8): " + new String(base64DecodedByteArray));

    return decodedString;
}

From source file:ZipUtil.java

/**
 * Inflates a previously deflated file.//from   ww w  .java  2s. c o  m
 */
public static byte[] unzipByteArray(byte[] file) throws IOException {
    byte[] byReturn = null;

    Inflater oInflate = new Inflater(false);
    oInflate.setInput(file);

    ByteArrayOutputStream oZipStream = new ByteArrayOutputStream();
    try {
        while (!oInflate.finished()) {
            byte[] byRead = new byte[ZIP_BUFFER_SIZE];
            int iBytesRead = oInflate.inflate(byRead);
            if (iBytesRead == byRead.length) {
                oZipStream.write(byRead);
            } else {
                oZipStream.write(byRead, 0, iBytesRead);
            }
        }
        byReturn = oZipStream.toByteArray();
    } catch (DataFormatException ex) {
        throw new IOException("Attempting to unzip file that is not zipped.");
    } finally {
        oZipStream.close();
    }
    return byReturn;
}

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

/**
 * Returns the uncompressed {@link InputStream} for the given
 * {@link URLConnection}./*from ww w  . j av a 2s. c o m*/
 */
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:v7db.files.Compression.java

/**
 * assumes that the result buffer has exactly the needed size
 * /*  ww w  . ja v  a  2s .  co  m*/
 * @throws DataFormatException
 */
static void inflate(byte[] data, int off, int len, byte[] out) throws DataFormatException {
    Inflater inflater = new Inflater(true);
    inflater.setInput(data, off, len);
    int size = inflater.inflate(out);
    if (size != out.length)
        throw new DataFormatException(
                "unexpected size of deflated data: " + size + " instead of " + out.length);
}