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) 

Source Link

Document

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

Usage

From source file:org.lockss.util.StreamUtil.java

/** Return an InputStream that uncompresses the data on the input
 * stream (normally an HTTP response stream)
 * @param instr raw InputStream/*from  w w  w  .  jav a 2 s.  c  om*/
 * @param contentEncoding value of HTTP Content-Encoding: header
 * @return The wrapped stream, or the original stream if contentEncoding
 * is null or "identity"
 * @throws UnsupportedEncodingException
 */
public static InputStream getUncompressedInputStream(InputStream instr, String contentEncoding)
        throws IOException, UnsupportedEncodingException {
    InputStream res;
    if (StringUtil.isNullString(contentEncoding) || contentEncoding.equalsIgnoreCase("identity")) {
        res = instr;
    } else if (contentEncoding.equalsIgnoreCase("gzip") || contentEncoding.equalsIgnoreCase("x-gzip")) {
        log.debug3("Wrapping in GZIPInputStream");
        res = new GZIPInputStream(instr);
    } else if (contentEncoding.equalsIgnoreCase("deflate")) {
        log.debug3("Wrapping in InflaterInputStream");
        res = new InflaterInputStream(instr);
    } else {
        throw new UnsupportedEncodingException(contentEncoding);
    }
    return res;
}

From source file:org.wso2.carbon.identity.auth.saml2.common.SAML2AuthUtils.java

public static String decodeForRedirect(String encodedStr) throws IdentityRuntimeException {
    try {//from  w  ww .  j  ava  2 s.  c o  m
        if (logger.isDebugEnabled()) {
            logger.debug(" >> encoded string in the SSOUtils/decode : " + encodedStr);
        }
        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 {
            //TODO if the request came in POST, inflating is wrong
            Inflater inflater = new Inflater(true);
            inflater.setInput(base64DecodedByteArray);
            byte[] xmlMessageBytes = new byte[5000];
            int resultLength = inflater.inflate(xmlMessageBytes);

            if (!inflater.finished()) {
                throw new RuntimeException("End of the compressed data stream has NOT been reached");
            }

            inflater.end();
            String decodedString = new String(xmlMessageBytes, 0, resultLength, "UTF-8");
            if (logger.isDebugEnabled()) {
                logger.debug("Request message " + decodedString);
            }
            return decodedString;

        } 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(), Charset.forName("UTF-8"));
            if (logger.isDebugEnabled()) {
                logger.debug("Request message " + decodedStr);
            }
            return decodedStr;
        }
    } catch (IOException e) {
        throw new IdentityRuntimeException("Error when decoding the SAML Request.", e);
    }
}

From source file:org.candlepin.pinsetter.tasks.HypervisorUpdateJob.java

public static String decompress(byte[] bytes) {
    InputStream in = new InflaterInputStream(new ByteArrayInputStream(bytes));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {/* www.  jav a2  s  .co  m*/
        byte[] buffer = new byte[8192];
        int len;
        while ((len = in.read(buffer)) > 0) {
            baos.write(buffer, 0, len);
        }
        return new String(baos.toByteArray(), "UTF-8");
    } catch (IOException e) {
        throw new AssertionError(e);
    }
}

From source file:org.oclc.oai.harvester.verb.HarvesterVerb.java

/**
 * @param args/*from w w  w .  ja  v a2s  . c o m*/
 * @throws IOException
 * @throws ParserConfigurationException
 * @throws SAXException
 */
public static void main(String[] args) throws IOException, ParserConfigurationException, SAXException {
    String requestURL = "https://databank.ora.ox.ac.uk/oaipmh?verb=ListRecords&resumptionToken=20121206_TKMGW4A_SWT3NHV";

    //String requestURL = "http://bd2.inesc-id.pt:8080/repox2Eudml/OAIHandler?verb=ListRecords&resumptionToken=1354116062009:ELibM_external:eudml-article2:33753:37054::";
    //String requestURL = "http://bd2.inesc-id.pt:8080/repox2Eudml/OAIHandler?verb=GetRecord&identifier=urn:eudml.eu:ELibM_external:05152756&metadataPrefix=eudml-article2";
    //String requestURL = "C:/Users/Gilberto Pedrosa/Desktop/OAIHandler.xml";
    //FileInputStream fis = new FileInputStream(requestURL);
    //InputStream in = fis;
    logger.debug("requestURL=" + requestURL);
    DocumentBuilderFactory factory;
    factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    Thread t = Thread.currentThread();
    DocumentBuilder builder = factory.newDocumentBuilder();
    HashMap<Thread, DocumentBuilder> builderMap = new HashMap<Thread, DocumentBuilder>();
    builderMap.put(t, builder);

    InputStream in;

    URL url = new URL(requestURL);
    HttpURLConnection con;
    int responseCode;
    do {
        con = (HttpURLConnection) url.openConnection();
        con.setConnectTimeout(30000);
        con.setReadTimeout(600000);

        if (con.getAllowUserInteraction()) {
            con.setRequestProperty("User-Agent", "OAIHarvester/2.0");
            con.setRequestProperty("Accept-Encoding", "compress, gzip, identify");
        }
        try {
            responseCode = con.getResponseCode();
            logger.debug("responseCode=" + responseCode);
        } catch (FileNotFoundException e) {
            // assume it's a 503 response
            logger.error(requestURL, e);
            responseCode = HttpURLConnection.HTTP_UNAVAILABLE;
        }

        if (responseCode == HttpURLConnection.HTTP_UNAVAILABLE) {
            long retrySeconds = con.getHeaderFieldInt("Retry-After", -1);
            if (retrySeconds == -1) {
                long now = (new Date()).getTime();
                long retryDate = con.getHeaderFieldDate("Retry-After", now);
                retrySeconds = retryDate - now;
            }
            if (retrySeconds == 0) { // Apparently, it's a bad URL
                throw new FileNotFoundException("Bad URL?");
            }
            logger.warn("Server response: Retry-After=" + retrySeconds);
            if (retrySeconds > 0) {
                try {
                    Thread.sleep(retrySeconds * 1000);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }

    while (responseCode == HttpURLConnection.HTTP_UNAVAILABLE);
    String contentEncoding = con.getHeaderField("Content-Encoding");
    logger.debug("contentEncoding=" + contentEncoding);
    if ("compress".equals(contentEncoding)) {
        ZipInputStream zis = new ZipInputStream(con.getInputStream());
        zis.getNextEntry();
        in = zis;
    } else if ("gzip".equals(contentEncoding)) {
        in = new GZIPInputStream(con.getInputStream());
    } else if ("deflate".equals(contentEncoding)) {
        in = new InflaterInputStream(con.getInputStream());
    } else {
        in = con.getInputStream();
    }

    byte[] inputBytes = IOUtils.toByteArray(in);
    InputSource data = new InputSource(new ByteArrayInputStream(inputBytes));

    String xmlString = new String(inputBytes, "UTF-8");
    xmlString = XmlUtil.removeInvalidXMLCharacters(xmlString);

    builder.parse(data);

    System.out.println("data = " + data);
}

From source file:org.wso2.identity.integration.test.requestPathAuthenticator.RequestPathAuthenticatorTestCase.java

/**
 * Decoding and deflating the encoded AuthReq
 *
 * @param encodedStr encoded AuthReq//w  ww  . j a  v  a2  s  .c  om
 * @return decoded AuthReq
 */
private static String decode(String encodedStr) {
    try {
        Base64 base64Decoder = new Base64();
        byte[] xmlBytes = encodedStr.getBytes(DEFAULT_CHARSET);
        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.finished()) {
                throw new RuntimeException("End of the compressed data stream has NOT been reached");
            }

            inflater.end();
            String decodedString = new String(xmlMessageBytes, 0, resultLength, (DEFAULT_CHARSET));
            return decodedString;

        } catch (DataFormatException e) {
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(base64DecodedByteArray);
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            InflaterInputStream iis = new InflaterInputStream(byteArrayInputStream);
            byte[] buf = new byte[1024];
            int count = iis.read(buf);
            while (count != -1) {
                byteArrayOutputStream.write(buf, 0, count);
                count = iis.read(buf);
            }
            iis.close();
            String decodedStr = new String(byteArrayOutputStream.toByteArray(), StandardCharsets.UTF_8);

            return decodedStr;
        }
    } catch (IOException e) {
        Assert.fail("Error while decoding SAML response");
        return "";
    }
}

From source file:microsoft.exchange.webservices.data.core.request.ServiceRequestBase.java

/**
 * Gets the response error stream.//from ww  w. j  av a2 s .com
 *
 * @param request the request
 * @return the response error stream
 * @throws EWSHttpException    the EWS http exception
 * @throws java.io.IOException Signals that an I/O exception has occurred.
 */
private static InputStream getResponseErrorStream(HttpWebRequest request) throws EWSHttpException, IOException {
    String contentEncoding = "";

    if (null != request.getContentEncoding()) {
        contentEncoding = request.getContentEncoding().toLowerCase();
    }

    InputStream responseStream;

    if (contentEncoding.contains("gzip")) {
        responseStream = new GZIPInputStream(request.getErrorStream());
    } else if (contentEncoding.contains("deflate")) {
        responseStream = new InflaterInputStream(request.getErrorStream());
    } else {
        responseStream = request.getErrorStream();
    }
    return responseStream;
}

From source file:org.opensaml.saml2.metadata.provider.HTTPMetadataProvider.java

/**
 * Extracts the raw metadata bytes from the response taking in to account possible deflate and GZip compression.
 * // ww w  .j  av a 2  s.  c om
 * @param getMethod GetMethod containing a valid HTTP response
 * 
 * @return the raw metadata bytes
 * 
 * @throws MetadataProviderException thrown if there is a problem getting the raw metadata bytes from the response
 */
protected byte[] getMetadataBytesFromResponse(GetMethod getMethod) throws MetadataProviderException {
    log.debug("Attempting to extract metadata from response to request for metadata from '{}'",
            getMetadataURI());
    try {
        InputStream ins = getMethod.getResponseBodyAsStream();

        Header httpHeader = getMethod.getResponseHeader("Content-Encoding");
        if (httpHeader != null) {
            String contentEncoding = httpHeader.getValue();
            if ("deflate".equalsIgnoreCase(contentEncoding)) {
                log.debug("Metadata document from '{}' was deflate compressed, decompressing it", metadataURI);
                ins = new InflaterInputStream(ins);
            }

            if ("gzip".equalsIgnoreCase(contentEncoding)) {
                log.debug("Metadata document from '{}' was GZip compressed, decompressing it", metadataURI);
                ins = new GZIPInputStream(ins);
            }
        }

        return inputstreamToByteArray(ins);
    } catch (IOException e) {
        log.error("Unable to read response", e);
        throw new MetadataProviderException("Unable to read response", e);
    }
}

From source file:com.ichi2.anki.AnkiDroidProxy.java

/**
 * Anki Desktop -> libanki/anki/sync.py, HttpSyncServerProxy - applyPayload
 *
 * @param payload/*w  ww.j a v a  2s  .co  m*/
 */
public JSONObject applyPayload(JSONObject payload) {
    Log.i(AnkiDroidApp.TAG, "applyPayload");
    // Log.i(AnkiDroidApp.TAG, "user = " + username + ", password = " + password + ", payload = " +
    // payload.toString());
    JSONObject payloadReply = new JSONObject();

    try {
        // FIXME: Try to do the connection without encoding the payload in Base 64
        String data = "p=" + URLEncoder.encode(mPassword, "UTF-8") + "&u="
                + URLEncoder.encode(mUsername, "UTF-8") + "&d=" + URLEncoder.encode(mDeckName, "UTF-8")
                + "&payload="
                + URLEncoder.encode(Base64.encodeBytes(Utils.compress(payload.toString().getBytes())), "UTF-8")
                + "&base64=" + URLEncoder.encode("true", "UTF-8");

        // Log.i(AnkiDroidApp.TAG, "Data json = " + data);
        HttpPost httpPost = new HttpPost(SYNC_URL + "applyPayload");
        StringEntity entity = new StringEntity(data);
        httpPost.setEntity(entity);
        httpPost.setHeader("Accept-Encoding", "identity");
        httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpResponse response = httpClient.execute(httpPost);
        Log.i(AnkiDroidApp.TAG, "Response = " + response.toString());
        HttpEntity entityResponse = response.getEntity();
        Log.i(AnkiDroidApp.TAG, "Entity's response = " + entityResponse.toString());
        InputStream content = entityResponse.getContent();
        Log.i(AnkiDroidApp.TAG, "Content = " + content.toString());
        String contentString = Utils.convertStreamToString(new InflaterInputStream(content));
        Log.i(AnkiDroidApp.TAG, "Payload response = ");
        payloadReply = new JSONObject(contentString);
        Utils.printJSONObject(payloadReply, false);
        Utils.saveJSONObject(payloadReply);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        Log.i(AnkiDroidApp.TAG, "ClientProtocolException = " + e.getMessage());
    } catch (IOException e) {
        Log.i(AnkiDroidApp.TAG, "IOException = " + e.getMessage());
    } catch (JSONException e) {
        Log.i(AnkiDroidApp.TAG, "JSONException = " + e.getMessage());
    }

    return payloadReply;
}

From source file:org.oclc.oai.harvester2.verb.HarvesterVerb.java

/**
 * Preforms the OAI request, recovering from typical XML error
 *
 * @author nfreire Nuno Freire / Gilberto Pedrosa
 * @param requestURL/*  w  w w .  j a v  a 2s.c om*/
 * @throws IOException
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws TransformerException
 */
private void harvest(String requestURL)
        throws IOException, ParserConfigurationException, SAXException, TransformerException {
    this.requestURL = requestURL;
    logger.debug("requestURL=" + requestURL);
    InputStream in;
    URL url = new URL(requestURL);
    HttpURLConnection con;
    int responseCode;
    do {
        con = (HttpURLConnection) url.openConnection();
        con.setConnectTimeout(30000);
        con.setReadTimeout(600000);

        if (con.getAllowUserInteraction()) {
            con.setRequestProperty("User-Agent", "OAIHarvester/2.0");
            con.setRequestProperty("Accept-Encoding", "compress, gzip, identify");
        }
        try {
            responseCode = con.getResponseCode();
            logger.debug("responseCode=" + responseCode);
        } catch (FileNotFoundException e) {
            // assume it's a 503 response
            logger.error(requestURL, e);
            responseCode = HttpURLConnection.HTTP_UNAVAILABLE;
        }

        if (responseCode == HttpURLConnection.HTTP_UNAVAILABLE) {
            long retrySeconds = con.getHeaderFieldInt("Retry-After", -1);
            if (retrySeconds == -1) {
                long now = (new Date()).getTime();
                long retryDate = con.getHeaderFieldDate("Retry-After", now);
                retrySeconds = retryDate - now;
            }
            if (retrySeconds == 0) { // Apparently, it's a bad URL
                throw new FileNotFoundException("Bad URL?");
            }
            logger.warn("Server response: Retry-After=" + retrySeconds);
            if (retrySeconds > 0) {
                try {
                    Thread.sleep(retrySeconds * 1000);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        }
    } while (responseCode == HttpURLConnection.HTTP_UNAVAILABLE);
    String contentEncoding = con.getHeaderField("Content-Encoding");
    logger.debug("contentEncoding=" + contentEncoding);
    if ("compress".equals(contentEncoding)) {
        ZipInputStream zis = new ZipInputStream(con.getInputStream());
        zis.getNextEntry();
        in = zis;
    } else if ("gzip".equals(contentEncoding)) {
        in = new GZIPInputStream(con.getInputStream());
    } else if ("deflate".equals(contentEncoding)) {
        in = new InflaterInputStream(con.getInputStream());
    } else {
        in = con.getInputStream();
    }

    byte[] inputBytes = IOUtils.toByteArray(in);
    InputSource data = new InputSource(new ByteArrayInputStream(inputBytes));

    Thread t = Thread.currentThread();
    DocumentBuilder builder = (DocumentBuilder) builderMap.get(t);
    if (builder == null) {
        builder = factory.newDocumentBuilder();
        builderMap.put(t, builder);
    }
    try {
        doc = builder.parse(data);
    } catch (SAXException e) {
        try {
            //Here we can try to recover the xml from known typical problems

            //Recover from invalid characters
            //we assume this is UTF-8...
            String xmlString = new String(inputBytes, "UTF-8");
            xmlString = XmlUtil.removeInvalidXMLCharacters(xmlString);

            data = new InputSource(new ByteArrayInputStream(xmlString.getBytes("UTF-8")));
            doc = builder.parse(data);
        } catch (Exception e2) {
            //the recovered version did not work either. Throw the original exception
            throw e;
        }
    } catch (IOException e3) {
        System.out.println("e = " + e3.getMessage());
    } catch (Exception e4) {
        System.out.println("e = " + e4.getMessage());
    }

    StringTokenizer tokenizer = new StringTokenizer(getSingleString("/*/@xsi:schemaLocation"), " ");
    StringBuffer sb = new StringBuffer();
    while (tokenizer.hasMoreTokens()) {
        if (sb.length() > 0)
            sb.append(" ");
        sb.append(tokenizer.nextToken());
    }
    this.schemaLocation = sb.toString();
    this.defaultNamespace = getDocument().getDocumentElement().getNamespaceURI();
}

From source file:org.oclc.oai.harvester.verb.HarvesterVerb.java

/**
 * Performs the OAI request, recovering from typical XML error
 * /*  ww w  .j a va  2  s. co m*/
 * @author nfreire Nuno Freire / Gilberto Pedrosa
 * @param requestURL
 * @throws IOException
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws TransformerException
 */
private void harvest(String requestURL)
        throws IOException, ParserConfigurationException, SAXException, TransformerException {
    this.requestURL = requestURL;
    logger.debug("requestURL=" + requestURL);
    InputStream in;
    URL url = new URL(requestURL);
    HttpURLConnection con;
    int responseCode;
    do {
        con = (HttpURLConnection) url.openConnection();
        con.setConnectTimeout(30000);
        con.setReadTimeout(600000);

        if (con.getAllowUserInteraction()) {
            con.setRequestProperty("User-Agent", "OAIHarvester/2.0");
            con.setRequestProperty("Accept-Encoding", "compress, gzip, identify");
        }
        try {
            responseCode = con.getResponseCode();
            logger.debug("responseCode=" + responseCode);
        } catch (FileNotFoundException e) {
            // assume it's a 503 response
            logger.error(requestURL, e);
            responseCode = HttpURLConnection.HTTP_UNAVAILABLE;
        }

        if (responseCode == HttpURLConnection.HTTP_UNAVAILABLE) {
            long retrySeconds = con.getHeaderFieldInt("Retry-After", -1);
            if (retrySeconds == -1) {
                long now = (new Date()).getTime();
                long retryDate = con.getHeaderFieldDate("Retry-After", now);
                retrySeconds = retryDate - now;
            }
            if (retrySeconds == 0) { // Apparently, it's a bad URL
                throw new FileNotFoundException("Bad URL?");
            }
            logger.warn("Server response: Retry-After=" + retrySeconds);
            if (retrySeconds > 0) {
                try {
                    Thread.sleep(retrySeconds * 1000);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        }
    } while (responseCode == HttpURLConnection.HTTP_UNAVAILABLE);
    String contentEncoding = con.getHeaderField("Content-Encoding");
    logger.debug("contentEncoding=" + contentEncoding);
    if ("compress".equals(contentEncoding)) {
        ZipInputStream zis = new ZipInputStream(con.getInputStream());
        zis.getNextEntry();
        in = zis;
    } else if ("gzip".equals(contentEncoding)) {
        in = new GZIPInputStream(con.getInputStream());
    } else if ("deflate".equals(contentEncoding)) {
        in = new InflaterInputStream(con.getInputStream());
    } else {
        in = con.getInputStream();
    }

    byte[] inputBytes = IOUtils.toByteArray(in);
    InputSource data = new InputSource(new ByteArrayInputStream(inputBytes));

    Thread t = Thread.currentThread();
    DocumentBuilder builder = builderMap.get(t);
    if (builder == null) {
        builder = factory.newDocumentBuilder();
        builderMap.put(t, builder);
    }
    try {
        doc = builder.parse(data);
    } catch (SAXException e) {
        try {
            //Here we can try to recover the xml from known typical problems

            //Recover from invalid characters
            //we assume this is UTF-8...
            String xmlString = new String(inputBytes, "UTF-8");
            xmlString = XmlUtil.removeInvalidXMLCharacters(xmlString);

            data = new InputSource(new ByteArrayInputStream(xmlString.getBytes("UTF-8")));
            doc = builder.parse(data);
        } catch (Exception e2) {
            //the recovered version did not work either. Throw the original exception
            throw e;
        }
    } catch (IOException e3) {
        System.out.println("e = " + e3.getMessage());
    } catch (Exception e4) {
        System.out.println("e = " + e4.getMessage());
    }

    StringTokenizer tokenizer = new StringTokenizer(getSingleString("/*/@xsi:schemaLocation"), " ");
    StringBuffer sb = new StringBuffer();
    while (tokenizer.hasMoreTokens()) {
        if (sb.length() > 0)
            sb.append(" ");
        sb.append(tokenizer.nextToken());
    }
    this.schemaLocation = sb.toString();
    this.defaultNamespace = getDocument().getDocumentElement().getNamespaceURI();
}