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.apache.tez.common.TezCommonUtils.java

@Private
public static Inflater newInflater() {
    return new Inflater(NO_WRAP);
}

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

/**
 * Decode a SAML2 anthentication request for the HTTP-redirect binding.
 * /* ww  w. ja va  2 s  . c  om*/
 * @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:guru.benson.pinch.Pinch.java

/**
 * Download and inflate file from a ZIP stored on a HTTP server.
 *
 * @param entry//from  ww  w  .j a  va  2s  .c  o m
 *     Entry representing file to download.
 * @param name
 *     Path where to store the downloaded file.
 * @param listener
 *
 * @throws IOException
 *     If an error occurred while reading from network or writing to disk.
 * @throws InterruptedException
 *     If the thread was interrupted.
 */
public void downloadFile(ExtendedZipEntry entry, String dir, String name, ProgressListener listener)
        throws IOException, InterruptedException {
    HttpURLConnection conn = null;
    InputStream is = null;
    FileOutputStream fos = null;

    try {
        File outFile = new File(dir != null ? dir + File.separator + name : name);

        if (!outFile.exists()) {
            if (outFile.getParentFile() != null) {
                outFile.getParentFile().mkdirs();
            }
        }

        // no need to download 0 byte size directories
        if (entry.isDirectory()) {
            return;
        }

        fos = new FileOutputStream(outFile);

        byte[] buf = new byte[2048];
        int read, bytes = 0;

        conn = getEntryInputStream(entry);

        // this is a stored (non-deflated) file, read it raw without inflating it
        if (entry.getMethod() == ZipEntry.STORED) {
            is = new BufferedInputStream(conn.getInputStream());
        } else {
            is = new InflaterInputStream(conn.getInputStream(), new Inflater(true));
        }

        long totalSize = entry.getSize();
        while ((read = is.read(buf)) != -1) {
            if (Thread.currentThread().isInterrupted()) {
                throw new InterruptedException("Download was interrupted");
            }
            // Ignore any extra data
            if (totalSize < read + bytes) {
                read = ((int) totalSize) - bytes;
            }

            fos.write(buf, 0, read);
            bytes += read;
            if (listener != null) {
                listener.onProgress(bytes, read, totalSize);
            }
        }

        log("Wrote " + bytes + " bytes to " + name);
    } finally {
        close(fos);
        close(is);
        disconnect(conn);
    }
}

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

/**
 * Decoding and deflating the encoded AuthReq
 *
 * @param encodedStr encoded AuthReq//from  www.  ja v  a2  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("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:org.wso2.identity.integration.test.requestPathAuthenticator.SAMLWithRequestPathAuthenticationTest.java

/**
 * Decoding and deflating the encoded AuthReq
 *
 * @param encodedStr encoded AuthReq//from  w  w  w. j a v a 2 s . c  o m
 * @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.asimba.wa.integrationtest.saml2.model.AuthnRequest.java

/**
 * Parse an incoming AuthnRequest message from HTTP Redirect bound request;<br/> 
 * also takes care of base64-decoding and deflating the message
 * @param authnRequestString//w  w  w.j  av  a2 s .  c o m
 * @throws IOException 
 */
public static AuthnRequest loadAuthnRequest(String authnRequestString) throws IOException {
    // Decode incoming request
    _logger.debug("B64-encoded string: {}", authnRequestString);
    byte[] decoded = Base64.decodeBase64(authnRequestString);

    // Now inflate it:
    ByteArrayInputStream bais = new ByteArrayInputStream(decoded);
    Inflater inflater = new Inflater(true); // require specific Inflater because header is omitted
    InflaterInputStream inflaterInputStream = new InflaterInputStream(bais, inflater);

    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setNamespaceAware(true);

    DocumentBuilder documentBuilder;
    Document document = null;
    try {
        documentBuilder = builderFactory.newDocumentBuilder();
        document = documentBuilder.parse(inflaterInputStream);

        if (_logger.isDebugEnabled()) {
            String s = docToString(document);
            _logger.debug("Incoming AuthnRequest:\n{}", s);
        }

        AuthnRequest authnRequestResult = new AuthnRequest(null, null);
        authnRequestResult._authnRequestDocument = document;

        return authnRequestResult;

    } catch (ParserConfigurationException | SAXException | IOException e) {
        _logger.error("Could not get AuthnRequest document from parameter: {}", e.getMessage(), e);
        return null;
    }
}

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

public static String decode(String encodedStr) throws AppFactoryException {
    try {//from  w ww .  j a  v  a 2  s .c  om
        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.parosproxy.paros.core.proxy.ProxyThread.java

private FilterInputStream buildStreamDecoder(String encoding, ByteArrayInputStream bais) throws IOException {
    if (encoding.equalsIgnoreCase(HttpHeader.DEFLATE)) {
        return new InflaterInputStream(bais, new Inflater(true));
    } else {/*from w  w  w  . j  a v  a  2 s  . c  om*/
        return new GZIPInputStream(bais);
    }
}

From source file:org.adeptnet.auth.saml.SAMLClient.java

private byte[] inflate(final byte[] content) throws java.io.IOException {
    try (final java.io.InputStream is = new java.io.ByteArrayInputStream(content);
            final java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream(content.length * 2);
            final InflaterInputStream iis = new InflaterInputStream(is, new Inflater(true))) {
        final byte[] buffer = new byte[4096];
        int n = 0;
        while (-1 != (n = iis.read(buffer))) {
            baos.write(buffer, 0, n);/*from   ww  w . j a  va 2s . c  o m*/
        }
        baos.flush();
        return baos.toByteArray();
    }
}

From source file:com.tremolosecurity.idp.providers.Saml2Idp.java

private 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;//  w w  w.  j  a  v  a 2  s.com
    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;
}