Example usage for java.util.zip InflaterInputStream close

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

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes this input stream and releases any system resources associated with the stream.

Usage

From source file:org.wso2.carbon.appfactory.apiManager.integration.utils.Utils.java

public static String decode(String encodedStr) throws AppFactoryException {
    try {/*from w w w  . ja  v a 2s  . c o  m*/
        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.finished()) {
                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 AppFactoryException("Error when decoding the SAML Request.", e);
    }

}

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

/**
 * Decoding and deflating the encoded AuthReq
 *
 * @param encodedStr encoded AuthReq// www.  j  a  v  a  2s.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();
            return new String(xmlMessageBytes, 0, resultLength, (DEFAULT_CHARSET));

        } 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();

            return new String(byteArrayOutputStream.toByteArray(), StandardCharsets.UTF_8);
        }
    } catch (IOException e) {
        Assert.fail("Error while decoding SAML response", e);
        return "";
    }
}

From source file:org.wso2.carbon.identity.saml.inbound.util.SAMLSSOUtil.java

/**
 * Decoding and deflating the encoded AuthReq
 *
 * @param encodedStr encoded AuthReq/*  ww w.  j av a 2 s  .  c  o m*/
 * @return decoded AuthReq
 */
public static String decode(String encodedStr) throws IdentityException {
    try {
        org.apache.commons.codec.binary.Base64 base64Decoder = new org.apache.commons.codec.binary.Base64();
        byte[] xmlBytes = encodedStr.getBytes(StandardCharsets.UTF_8.name());
        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, StandardCharsets.UTF_8.name());
            if (log.isDebugEnabled()) {
                log.debug("Request message " + decodedString);
            }
            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);
            if (log.isDebugEnabled()) {
                log.debug("Request message " + decodedStr, e);
            }
            return decodedStr;
        }
    } catch (IOException e) {
        throw IdentityException.error("Error when decoding the SAML Request.", e);
    }

}

From source file:fr.mby.saml2.sp.impl.helper.SamlHelper.java

/**
 * Decode a SAML2 anthentication request for the HTTP-redirect binding.
 * /*from w  ww.j av a 2s  .c o m*/
 * @param authnRequest
 *            the authn request
 * @return the encoded request
 * @throws IOException
 */
public static String httpRedirectDecode(final String encodedRequest) throws IOException {
    String inflatedRequest = null;

    ByteArrayInputStream bytesIn = null;
    InflaterInputStream inflater = null;

    final byte[] decodedBytes = Base64.decode(encodedRequest);

    try {
        bytesIn = new ByteArrayInputStream(decodedBytes);
        inflater = new InflaterInputStream(bytesIn, new Inflater(true));
        final Writer writer = new StringWriter();
        final char[] buffer = new char[1024];

        final Reader reader = new BufferedReader(new InputStreamReader(inflater, "UTF-8"));
        int n;
        while ((n = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, n);
        }

        inflatedRequest = writer.toString();
    } finally {
        if (bytesIn != null) {
            bytesIn.close();
        }
        if (inflater != null) {
            inflater.close();
        }
    }

    return inflatedRequest;
}

From source file:com.uber.hoodie.common.HoodieJsonPayload.java

private String unCompressData(byte[] data) throws IOException {
    InflaterInputStream iis = new InflaterInputStream(new ByteArrayInputStream(data));
    try {/*from w  w w.jav a  2  s  .  c  o m*/
        StringWriter sw = new StringWriter(dataSize);
        IOUtils.copy(iis, sw);
        return sw.toString();
    } finally {
        iis.close();
    }
}

From source file:org.wso2.carbon.identity.sso.saml.util.SAMLSSOUtil.java

/**
 * Decoding and deflating the encoded AuthReq
 *
 * @param encodedStr encoded AuthReq//  w w w  . jav a 2 s . c om
 * @return decoded AuthReq
 */
public static String decode(String encodedStr) throws IdentityException {
    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();
            String decodedString = new String(xmlMessageBytes, 0, resultLength, "UTF-8");
            if (log.isDebugEnabled()) {
                log.debug("Request message " + decodedString);
            }
            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);
            if (log.isDebugEnabled()) {
                log.debug("Request message " + decodedStr, e);
            }
            return decodedStr;
        }
    } catch (IOException e) {
        throw new IdentityException("Error when decoding the SAML Request.", e);
    }

}

From source file:com.alibaba.citrus.service.requestcontext.session.encoder.AbstractSerializationEncoder.java

/** ? */
public Map<String, Object> decode(String encodedValue, StoreContext storeContext)
        throws SessionEncoderException {
    // 1. base64?
    byte[] cryptotext = null;

    try {/*from   w w  w.  j av  a  2 s .co m*/
        encodedValue = URLDecoder.decode(assertNotNull(encodedValue, "encodedValue is null"), "ISO-8859-1");
        cryptotext = Base64.decodeBase64(encodedValue.getBytes("ISO-8859-1"));

        if (isEmptyArray(cryptotext)) {
            throw new SessionEncoderException("Session state is empty: " + encodedValue);
        }
    } catch (Exception e) {
        throw new SessionEncoderException("Failed to decode session state: ", e);
    }

    // 2. 
    byte[] plaintext = decrypt(cryptotext);

    if (isEmptyArray(plaintext)) {
        throw new SessionEncoderException("Decrypted session state is empty: " + encodedValue);
    }

    // 3. 
    ByteArrayInputStream bais = new ByteArrayInputStream(plaintext);
    Inflater inf = new Inflater(false);
    InflaterInputStream iis = new InflaterInputStream(bais, inf);

    // 4. ???
    try {
        @SuppressWarnings("unchecked")
        Map<String, Object> attrs = (Map<String, Object>) serializer.deserialize(iis);
        return attrs;
    } catch (Exception e) {
        throw new SessionEncoderException("Failed to parse session state", e);
    } finally {
        try {
            iis.close();
        } catch (IOException e) {
        }

        inf.end();
    }
}

From source file:servlets.ProcessResponseServlet.java

private String decodeAuthnRequestXML(String encodedRequestXmlString) throws SamlException {
    try {//from ww  w.j av a 2 s. c  om
        // URL decode
        // No need to URL decode: auto decoded by request.getParameter() method

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

        //Uncompress the AuthnRequest data
        //First attempt to unzip the byte array according to DEFLATE (rfc 1951)
        try {

            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");
            }

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

        } catch (DataFormatException e) {

            // if DEFLATE fails, then attempt to unzip the byte array according to
            // zlib (rfc 1950)      
            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 (UnsupportedEncodingException e) {
        throw new SamlException("Error decoding AuthnRequest: " + "Check decoding scheme - " + e.getMessage());
    } catch (IOException e) {
        throw new SamlException("Error decoding AuthnRequest: " + "Check decoding scheme - " + e.getMessage());
    }
}

From source file:auth.ProcessResponseServlet.java

private String decodeAuthnRequestXML(String encodedRequestXmlString) throws SamlException {
    try {/* w  w  w  . j  ava 2s  . co m*/
        // URL decode
        // No need to URL decode: auto decoded by request.getParameter() method

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

        //Uncompress the AuthnRequest data
        //First attempt to unzip the byte array according to DEFLATE (rfc 1951)
        try {

            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");
            }

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

        } catch (DataFormatException e) {

            // if DEFLATE fails, then attempt to unzip the byte array according to
            // zlib (rfc 1950)
            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 (UnsupportedEncodingException e) {
        throw new SamlException("Error decoding AuthnRequest: " + "Check decoding scheme - " + e.getMessage());
    } catch (IOException e) {
        throw new SamlException("Error decoding AuthnRequest: " + "Check decoding scheme - " + e.getMessage());
    }
}

From source file:fabiogentile.powertutor.ui.UMLogger.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case MENU_PREFERENCES:
        startActivity(new Intent(this, EditPreferences.class));
        return true;
    case MENU_SAVE_LOG:
        new Thread() {
            public void start() {
                File writeFile = new File(Environment.getExternalStorageDirectory(),
                        "PowerTrace" + System.currentTimeMillis() + ".log");

                try {
                    InflaterInputStream logIn = new InflaterInputStream(openFileInput("PowerTrace.log"));
                    BufferedOutputStream logOut = new BufferedOutputStream(new FileOutputStream(writeFile));

                    byte[] buffer = new byte[20480];
                    for (int ln = logIn.read(buffer); ln != -1; ln = logIn.read(buffer)) {
                        logOut.write(buffer, 0, ln);
                    }//  www. j a v  a  2  s . c  om
                    logIn.close();
                    logOut.close();
                    Toast.makeText(UMLogger.this, "Wrote log to " + writeFile.getAbsolutePath(),
                            Toast.LENGTH_SHORT).show();
                    return;

                } catch (EOFException e) {
                    Toast.makeText(UMLogger.this, "Wrote log to " + writeFile.getAbsolutePath(),
                            Toast.LENGTH_SHORT).show();
                    return;
                } catch (IOException e) {
                    Log.e(TAG, "failed to save log: " + e.getMessage());
                }
                Toast.makeText(UMLogger.this, "Failed to write log to sdcard", Toast.LENGTH_SHORT).show();
            }
        }.start();
        return true;
    }
    return super.onOptionsItemSelected(item);
}