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:org.codice.ddf.security.common.jaxrs.RestSecurity.java

public static String inflateBase64(String base64EncodedValue) throws IOException {
    byte[] deflatedValue = Base64.getMimeDecoder().decode(base64EncodedValue);
    InputStream is = new InflaterInputStream(new ByteArrayInputStream(deflatedValue),
            new Inflater(GZIP_COMPATIBLE));
    return IOUtils.toString(is, StandardCharsets.UTF_8.name());
}

From source file:com.alibaba.citrus.service.requestcontext.session.valueencoder.AbstractSessionValueEncoder.java

private byte[] decompress(byte[] data) throws SessionValueEncoderException {
    if (!doCompress()) {
        return data;
    }/*from w ww  .  j a  va2s. c o  m*/

    ByteArrayInputStream bais = new ByteArrayInputStream(data);
    Inflater inf = new Inflater(false);
    InflaterInputStream iis = new InflaterInputStream(bais, inf);

    try {
        return StreamUtil.readBytes(iis, true).toByteArray();
    } catch (Exception e) {
        throw new SessionValueEncoderException(e);
    } finally {
        inf.end();
    }
}

From source file:gridool.db.partitioning.phihash.csv.distmm.InMemoryIndexHelper.java

public static void loadIndex(final InMemoryMappingIndex index, final String[] fkIdxNames, final int bucket,
        final boolean loadAll) throws GridException {
    for (final String fkIdxName : fkIdxNames) {
        if (!loadAll) {
            if (index.containsIndex(fkIdxName)) {
                continue;
            }//  w ww  .  ja v a2  s.  c o m
        }
        final Map<String, IntArrayList> keymap = index.getKeyMap(fkIdxName);
        File file = getFkIndexFile(fkIdxName, bucket);
        if (!file.exists()) {
            LOG.warn("Loading index failed because a chunk FK index file does not exist: "
                    + file.getAbsolutePath());
            continue;
        }
        final FileInputStream fis;
        try {
            fis = new FileInputStream(file);
        } catch (FileNotFoundException fne) {
            throw new GridException("FK chunk index file not found: " + file.getAbsolutePath(), fne);
        }
        InflaterInputStream zin = new ContinousInflaterInputStream(fis, new Inflater(false),
                COMPRESSOR_BUFSIZE);
        final FastBufferedInputStream in = new FastBufferedInputStream(zin, FKCHUNK_IO_BUFSIZE);
        try {
            int b;
            while ((b = in.read()) != -1) {
                int distkeylen = VariableByteCodec.decodeInt(in, b);
                int valuelen = VariableByteCodec.decodeInt(in);
                byte[] k = new byte[distkeylen];
                in.read(k, 0, distkeylen);
                byte[] v = new byte[valuelen];
                in.read(v, 0, valuelen);
                String distkey = StringUtils.toString(k, 0, distkeylen);
                NodeWithPartitionNo nodeinfo = NodeWithPartitionNo.deserialize(v);
                index.addEntry(distkey, nodeinfo, keymap);
            }
        } catch (IOException ioe) {
            String errmsg = "Failed to load a chunk FK index: " + file.getAbsolutePath();
            LOG.error(errmsg, ioe);
            throw new GridException(errmsg, ioe);
        } finally {
            IOUtils.closeQuietly(in);
        }
    }
}

From source file:cn.edu.bit.whitesail.crawl.Crawler.java

private byte[] download(String URL) {
    byte[] result = null;
    HttpEntity httpEntity = null;/*ww w  .j  ava  2 s.co  m*/
    try {

        HttpGet httpGet = new HttpGet(URL);

        httpGet.addHeader("Accept-Language", "zh-cn,zh,en");
        httpGet.addHeader("Accept-Encoding", "gzip,deflate");

        HttpResponse response = httpClient.execute(httpGet);

        if (response.getStatusLine().getStatusCode() != 200) {
            return null;
        }

        Header header = response.getFirstHeader("content-type");

        if (header == null || header.getValue().indexOf("text/html") < 0) {
            return null;
        }

        int pos = header.getValue().indexOf("charset=");
        if (pos >= 0) {
            detectedEncoding = header.getValue().substring(pos + 8);
        }

        httpEntity = response.getEntity();
        InputStream in = null;
        in = httpEntity.getContent();

        header = response.getFirstHeader("Content-Encoding");
        if (null != header) {
            if (header.getValue().indexOf("gzip") >= 0) {
                in = new GZIPInputStream(in);
            } else if (header.getValue().indexOf("deflate") >= 0) {
                in = new InflaterInputStream(in, new Inflater(true));
            }
        }

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = in.read(buffer)) != -1) {
            out.write(buffer, 0, len);
        }
        result = out.toByteArray();

    } catch (IOException ex) {
        LOG.warn("downloading error,abandon");
        result = null;
    }
    return result;
}

From source file:org.oscim.utils.overpass.OverpassAPIReader.java

/**
 * Open a connection to the given url and return a reader on the input
 * stream from that connection.//from   w  ww.  ja  va 2 s.c  o m
 * 
 * @param pUrlStr
 *            The exact url to connect to.
 * @return An reader reading the input stream (servers answer) or
 *         <code>null</code>.
 * @throws IOException
 *             on io-errors
 */
private InputStream getInputStream(final String pUrlStr) throws IOException {
    URL url;
    int responseCode;
    String encoding;

    url = new URL(pUrlStr);
    myActiveConnection = (HttpURLConnection) url.openConnection();

    myActiveConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");

    responseCode = myActiveConnection.getResponseCode();

    if (responseCode != RESPONSECODE_OK) {
        String message;
        String apiErrorMessage;

        apiErrorMessage = myActiveConnection.getHeaderField("Error");

        if (apiErrorMessage != null) {
            message = "Received API HTTP response code " + responseCode + " with message \"" + apiErrorMessage
                    + "\" for URL \"" + pUrlStr + "\".";
        } else {
            message = "Received API HTTP response code " + responseCode + " for URL \"" + pUrlStr + "\".";
        }

        throw new IOException(message);
    }

    myActiveConnection.setConnectTimeout(TIMEOUT);

    encoding = myActiveConnection.getContentEncoding();

    responseStream = myActiveConnection.getInputStream();
    if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
        responseStream = new GZIPInputStream(responseStream);
    } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
        responseStream = new InflaterInputStream(responseStream, new Inflater(true));
    }

    return responseStream;
}

From source file:org.jahia.utils.zip.legacy.ZipInputStream.java

/**
 * Creates a new ZIP input stream.//from w  ww .  j  a  v  a 2s  .c o  m
 * @param in the actual input stream
 */
public ZipInputStream(InputStream in) {
    super(new PushbackInputStream(in, 512), new Inflater(true), 512);
    usesDefaultInflater = true;
    if (in == null) {
        throw new NullPointerException("in is null");
    }
}

From source file:com.nesscomputing.httpclient.response.ContentResponseHandler.java

/**
 * Processes the client response./*from   w w  w .j ava  2  s  .  c  om*/
 */
@Override
public T handle(final HttpClientResponse response) throws IOException {
    if (allowRedirect && response.isRedirected()) {
        LOG.debug("Redirecting based on '%d' response code", response.getStatusCode());
        throw new RedirectedException(response);
    } else {
        // Find the response stream - the error stream may be valid in cases
        // where the input stream is not.
        InputStream is = null;
        try {
            is = response.getResponseBodyAsStream();
        } catch (IOException e) {
            LOG.warnDebug(e, "Could not locate response body stream");
            // normal for 401, 403 and 404 responses, for example...
        }

        if (is == null) {
            // Fall back to zero length response.
            is = new NullInputStream(0);
        }

        try {
            final Long contentLength = response.getContentLength();

            if (maxBodyLength > 0) {
                if (contentLength != null && contentLength > maxBodyLength) {
                    throw new SizeExceededException("Content-Length: " + contentLength);
                }

                LOG.debug("Limiting stream length to '%d'", maxBodyLength);
                is = new SizeLimitingInputStream(is, maxBodyLength);
            }

            final String encoding = StringUtils.trimToEmpty(response.getHeader("Content-Encoding"));

            if (StringUtils.equalsIgnoreCase(encoding, "lz4")) {
                LOG.debug("Found LZ4 stream");
                is = new LZ4BlockInputStream(is);
            } else if (StringUtils.equalsIgnoreCase(encoding, "gzip")
                    || StringUtils.equalsIgnoreCase(encoding, "x-gzip")) {
                LOG.debug("Found GZIP stream");
                is = new GZIPInputStream(is);
            } else if (StringUtils.equalsIgnoreCase(encoding, "deflate")) {
                LOG.debug("Found deflate stream");
                final Inflater inflater = new Inflater(true);
                is = new InflaterInputStream(is, inflater);
            }

            return contentConverter.convert(response, is);
        } catch (IOException ioe) {
            return contentConverter.handleError(response, ioe);
        }
    }
}

From source file:com.adamrosenfield.wordswithcrosses.versions.DefaultUtil.java

private void downloadHelper(URL url, String scrubbedUrl, Map<String, String> headers, HttpContext httpContext,
        OutputStream output) throws IOException {
    HttpGet httpget;/*  w  w  w.  j a  va2s .c  o m*/
    try {
        httpget = new HttpGet(url.toURI());
    } catch (URISyntaxException e) {
        e.printStackTrace();
        throw new IOException("Invalid URL: " + url);
    }

    httpget.setHeader("Accept-Encoding", "gzip, deflate");
    for (Entry<String, String> e : headers.entrySet()) {
        httpget.setHeader(e.getKey(), e.getValue());
    }

    HttpResponse response = mHttpClient.execute(httpget, httpContext);

    int status = response.getStatusLine().getStatusCode();
    HttpEntity entity = response.getEntity();

    if (status != 200) {
        LOG.warning("Download failed: " + scrubbedUrl + " status=" + status);
        if (entity != null) {
            entity.consumeContent();
        }

        throw new HTTPException(status);
    }

    if (entity != null) {
        // If we got a compressed entity, create the proper decompression
        // stream wrapper
        InputStream content = entity.getContent();
        Header contentEncoding = entity.getContentEncoding();
        if (contentEncoding != null) {
            if ("gzip".equals(contentEncoding.getValue())) {
                content = new GZIPInputStream(content);
            } else if ("deflate".equals(contentEncoding.getValue())) {
                content = new InflaterInputStream(content, new Inflater(true));
            }
        }

        try {
            IO.copyStream(content, output);
        } finally {
            entity.consumeContent();
        }
    }
}

From source file:acp.sdk.SecureUtil.java

/**
 * ./*ww w  .j  a v  a  2s . c  o  m*/
 * 
 * @param inputByte
 *            byte[]?
 * @return ??
 * @throws IOException
 */
public static byte[] inflater(final byte[] inputByte) throws IOException {
    int compressedDataLength = 0;
    Inflater compresser = new Inflater(false);
    compresser.setInput(inputByte, 0, inputByte.length);
    ByteArrayOutputStream o = new ByteArrayOutputStream(inputByte.length);
    byte[] result = new byte[1024];
    try {
        while (!compresser.finished()) {
            compressedDataLength = compresser.inflate(result);
            if (compressedDataLength == 0) {
                break;
            }
            o.write(result, 0, compressedDataLength);
        }
    } catch (Exception ex) {
        System.err.println("Data format error!\n");
        ex.printStackTrace();
    } finally {
        o.close();
    }
    compresser.end();
    return o.toByteArray();
}

From source file:org.wso2.carbon.identity.authenticator.saml2.sso.ui.Util.java

/**
 * Decoding and deflating the encoded AuthReq
 *
 * @param encodedStr encoded AuthReq//from   w  ww.  j  a  v a  2 s  .c om
 * @return decoded AuthReq
 */
public static String decode(String encodedStr) throws SAML2SSOUIAuthenticatorException {
    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();
            String decodedStr = new String(baos.toByteArray());
            return decodedStr;
        }
    } catch (IOException e) {
        throw new SAML2SSOUIAuthenticatorException("Error when decoding the SAML Request.", e);
    }

}