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:divconq.ctp.stream.UngzipStream.java

public UngzipStream() {
    this.inflater = new Inflater(true);
    this.crc = new CRC32();
}

From source file:de.hofuniversity.iisys.neo4j.websock.query.encoding.unsafe.DeflateJsonQueryHandler.java

public DeflateJsonQueryHandler() {
    fLogger = Logger.getLogger(this.getClass().getName());
    fDebug = (fLogger.getLevel() == Level.FINEST);
    fCompression = DEFAULT_COMPRESSION_LEVEL;

    fInflater = new Inflater(true);
}

From source file:de.hofuniversity.iisys.neo4j.websock.query.encoding.unsafe.DeflateJsonQueryHandler.java

public DeflateJsonQueryHandler(final String compression) {
    fLogger = Logger.getLogger(this.getClass().getName());
    fDebug = (fLogger.getLevel() == Level.FINEST);

    fInflater = new Inflater(true);

    int tmpComp = DEFAULT_COMPRESSION_LEVEL;

    if (WebsockConstants.FASTEST_COMPRESSION.equals(compression)) {
        tmpComp = Deflater.BEST_SPEED;
    } else if (WebsockConstants.BEST_COMPRESSION.equals(compression)) {
        tmpComp = Deflater.BEST_COMPRESSION;
    } else {//  w  w  w .  j  av  a2  s .  com
        fLogger.log(Level.WARNING, "unknown compression level '" + compression + "'; using default.");
    }

    fCompression = tmpComp;
}

From source file:com.tremolosecurity.unison.u2f.util.U2fUtil.java

private static 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;/*from   w  w  w.j a v a 2s .  co m*/
    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;
}

From source file:com.nesscomputing.tinyhttp.HttpContentResponseHandler.java

/**
 * Processes the client response./*from  w w w.j a  v  a2s .  c  o  m*/
 */
public T handle(final HttpRequest request, final HttpResponse response) throws IOException {
    // Find the response stream - the error stream may be valid in cases
    // where the input stream is not.
    InputStream is = null;
    try {
        final HttpEntity httpEntity = response.getEntity();
        if (httpEntity != null) {
            is = httpEntity.getContent();
        }
    } catch (IOException e) {
        log.warn("Could not locate response body stream", e);
        // normal for 401, 403 and 404 responses, for example...
    }

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

    final Header header = response.getFirstHeader("Content-Encoding");
    if (header != null) {
        final String encoding = StringUtils.trimToEmpty(header.getValue());

        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(request, response, is);
}

From source file:org.slc.sli.sandbox.idp.saml.SamlRequestDecoder.java

public SamlRequest decode(String encodedSamlRequest) {
    byte[] decodedCompressed = Base64.decodeBase64(encodedSamlRequest);
    Inflater inflater = new Inflater(true);
    InflaterInputStream xmlInputStream = new InflaterInputStream(new ByteArrayInputStream(decodedCompressed),
            inflater);// w w w. ja  v a  2 s.  co  m
    Document doc;

    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
        factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
        DocumentBuilder docBuilder = factory.newDocumentBuilder();

        doc = docBuilder.parse(xmlInputStream);
    } catch (ParserConfigurationException e) {
        throw new SamlProcessException(e);
    } catch (SAXException e) {
        throw new SamlProcessException(e);
    } catch (IOException e) {
        throw new SamlProcessException(e);
    }
    Element element = doc.getDocumentElement();
    String id = element.getAttribute("ID");
    boolean forceAuthn = Boolean.valueOf(element.getAttribute("ForceAuthn"));
    String simpleIDPDestination = element.getAttribute("Destination");
    NodeList nodes = element.getElementsByTagName("saml:Issuer");
    String issuer = null;
    if (nodes.getLength() > 0) {
        Node item = nodes.item(0);
        issuer = item.getFirstChild().getNodeValue();
    } else {
        throw new IllegalArgumentException("No Issuer element on AuthnRequest");
    }

    if (id == null) {
        throw new IllegalArgumentException("No ID attribute on AuthnRequest.");
    }
    String responseDestination = cot.get(issuer);
    if (responseDestination == null) {
        throw new IllegalArgumentException("Issuer of AuthnRequest is unknown.");
    }

    return new SamlRequest(responseDestination, simpleIDPDestination, id, forceAuthn);
}

From source file:org.resthub.rpc.AMQPProxy.java

/**
 * Handles the object invocation.//from w ww  .j a  v a2  s .  c  om
 *
 * @param proxy  the proxy object to invoke
 * @param method the method to call
 * @param args   the arguments to the proxy object
 */
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    String methodName = method.getName();
    Class<?>[] params = method.getParameterTypes();

    // equals and hashCode are special cased
    if (methodName.equals("equals") && params.length == 1 && params[0].equals(Object.class)) {
        Object value = args[0];
        if (value == null || !Proxy.isProxyClass(value.getClass())) {
            return Boolean.FALSE;
        }

        AMQPProxy handler = (AMQPProxy) Proxy.getInvocationHandler(value);

        return _factory.equals(handler._factory);
    } else if (methodName.equals("hashCode") && params.length == 0) {
        return _factory.hashCode();
    } else if (methodName.equals("toString") && params.length == 0) {
        return "[HessianProxy " + proxy.getClass() + "]";
    }

    ConnectionFactory connectionFactory = _factory.getConnectionFactory();

    Message response = sendRequest(connectionFactory, method, args);

    if (response == null) {
        throw new TimeoutException();
    }

    MessageProperties props = response.getMessageProperties();
    boolean compressed = "deflate".equals(props.getContentEncoding());

    InputStream is = new ByteArrayInputStream(response.getBody());
    if (compressed) {
        is = new InflaterInputStream(is, new Inflater(true));
    }

    return _factory.getSerializationHandler().readObject(method.getReturnType(), is);
}

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

/**
 * Decoding and deflating the encoded AuthReq
 *
 * @param encodedStr encoded AuthReq/*  w w w  .  j a  v  a  2s .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();
            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", e);
        return "";
    }
}

From source file:servlets.ProcessResponseServlet.java

private String decodeAuthnRequestXML(String encodedRequestXmlString) throws SamlException {
    try {/* w  ww .  ja  v a2  s.c o 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:cn.vko.hessian.core.VkoHessianProxy.java

@Override
protected InputStream getInputStream(HessianConnection conn) throws IOException {
    InputStream is = conn.getInputStream();
    byte[] content = IOUtils.toByteArray(is);
    is.close();//from   w w w  .j  a  v a2 s  .co m
    if (factory.isEncrypt()) {
        // 
        content = factory.getEncrypt().decrypt(content);
    }
    if ("lz4".equals(conn.getContentEncoding())) {
        content = Lz4Compress.uncompress(content);
        is = new ByteArrayInputStream(content);
    } else if ("deflate".equals(conn.getContentEncoding())) {
        is = new InflaterInputStream(new ByteArrayInputStream(content), new Inflater(true));
    }
    return is;
}