Example usage for java.util.zip Inflater getRemaining

List of usage examples for java.util.zip Inflater getRemaining

Introduction

In this page you can find the example usage for java.util.zip Inflater getRemaining.

Prototype

public int getRemaining() 

Source Link

Document

Returns the total number of bytes remaining in the input buffer.

Usage

From source file:Main.java

/**
 * @param data                  -- the data to decompress
 * @param uncompressedChunkSize -- an estimate of the uncompressed chunk size.  This need not be exact.
 * @return/*  w w  w . ja v  a  2s . com*/
 */
public static byte[] decompress(byte[] data, int uncompressedChunkSize) {

    // mpd: new code
    int rem = data.length;

    // Create an expandable byte array to hold the decompressed data
    ByteArrayOutputStream bos = new ByteArrayOutputStream(uncompressedChunkSize);

    // Decompress the data
    byte[] outbuf = new byte[uncompressedChunkSize];

    Inflater decompressor = new Inflater();
    decompressor.setInput(data);
    while (rem > 0) {

        // If we are finished with the current chunk start a new one
        if (decompressor.finished()) {
            decompressor = new Inflater();
            int offset = data.length - rem;
            decompressor.setInput(data, offset, rem);
        }

        try {
            int count = decompressor.inflate(outbuf, 0, outbuf.length);
            rem = decompressor.getRemaining();
            bos.write(outbuf, 0, count);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    try {
        bos.close();
    } catch (IOException e) {
        // Ignore -- no resources open
    }

    // Return the decompressed data
    return bos.toByteArray();
}

From source file:MSUmpire.SpectrumParser.mzXMLReadUnit.java

public byte[] ZlibUncompressBuffer(byte[] compressed) throws IOException, DataFormatException {

    Inflater decompressor = new Inflater();
    decompressor.setInput(compressed);/* w  ww .j  av  a  2  s  . c  om*/

    ByteArrayOutputStream bos = null;
    try {

        bos = new ByteArrayOutputStream(compressed.length);

        // Decompress the data
        byte[] buf = new byte[decompressor.getRemaining() * 2];
        while (decompressor.getRemaining() > 0) {
            int count = decompressor.inflate(buf);
            bos.write(buf, 0, count);
        }

    } finally {
        try {
            bos.close();
        } catch (Exception nope) {
            /* This exception doesn't matter */ }
    }
    decompressor.end();
    compressed = null;
    decompressor = null;
    byte[] result = bos.toByteArray();
    bos = null;
    return result;
}

From source file:org.wso2.carbon.identity.application.authenticator.samlsso.util.SSOUtils.java

/**
 * Decoding and deflating the encoded AuthReq
 *
 * @param encodedStr encoded AuthReq//from  w  ww .jav a  2s .co m
 * @return decoded AuthReq
 */
public static String decode(String encodedStr) throws SAMLSSOException {
    try {
        if (log.isDebugEnabled()) {
            log.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.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 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 (log.isDebugEnabled()) {
                log.debug("Request message " + decodedStr);
            }
            return decodedStr;
        }
    } catch (IOException e) {
        throw new SAMLSSOException("Error when decoding the SAML Request.", e);
    }

}

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

/**
 * Decoding and deflating the encoded AuthReq
 *
 * @param encodedStr encoded AuthReq//  w w w. jav  a2  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);
    }

}

From source file:org.wso2.carbon.identity.sso.saml.tomcat.agent.Util.java

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

From source file:org.wso2.carbon.identity.sso.saml.util.SAMLSSOUtil.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
 */
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);
    }

}