Example usage for java.util.zip Inflater finished

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

Introduction

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

Prototype

boolean finished

To view the source code for java.util.zip Inflater finished.

Click Source Link

Usage

From source file:radixcore.network.ByteBufIO.java

/**
 * Decompresses a compressed byte array.
 * /*  w w  w.  j a v a 2 s.c o m*/
 * @param input The byte array to be decompressed.
 * @return The byte array in its decompressed, readable form.
 */
public static byte[] decompress(byte[] input) {
    try {
        final Inflater inflater = new Inflater();
        final ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(input.length);
        final byte[] buffer = new byte[1024];
        inflater.setInput(input);

        while (!inflater.finished()) {
            final int count = inflater.inflate(buffer);
            byteOutput.write(buffer, 0, count);
        }

        inflater.end();
        byteOutput.close();
        return byteOutput.toByteArray();
    }

    catch (final DataFormatException e) {
        RadixExcept.logFatalCatch(e, "Error decompressing byte array.");
        return null;
    }

    catch (final IOException e) {
        RadixExcept.logFatalCatch(e, "Error decompressing byte array.");
        return null;
    }
}

From source file:Main.java

public static byte[] zlibDecompress(byte[] data, int offset, int length) {
    byte[] output = null;

    Inflater decompresser = new Inflater();
    decompresser.reset();//from   w  ww.  j av  a 2  s .co  m
    try {
        decompresser.setInput(data, offset, length);
    } catch (Exception e) {
        return null;
    }

    ByteArrayOutputStream o = new ByteArrayOutputStream(data.length);
    try {
        byte[] buf = new byte[1024];
        while (!decompresser.finished()) {
            int i = decompresser.inflate(buf);
            o.write(buf, 0, i);
        }
        output = o.toByteArray();
    } catch (Exception e) {
        output = data;
        e.printStackTrace();
    } finally {
        try {
            o.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    decompresser.end();
    return output;
}

From source file:Main.java

public final static byte[] decompress(byte[] input) throws IOException {
    if (input == null || input.length == 0) {
        return input;
    }//from  ww  w  .j  a  v  a2s.c  o  m
    Inflater inflator = new Inflater();
    inflator.setInput(input);
    ByteArrayOutputStream bin = new ByteArrayOutputStream(input.length);
    byte[] buf = new byte[BUFFER_SIZE];
    try {
        while (true) {
            int count = inflator.inflate(buf);
            if (count > 0) {
                bin.write(buf, 0, count);
            } else if (count == 0 && inflator.finished()) {
                break;
            } else {
                throw new IOException("bad zip data, size:" + input.length);
            }
        }
    } catch (DataFormatException t) {
        throw new IOException(t);
    } finally {
        inflator.end();
    }
    return bin.toByteArray();
}

From source file:Main.java

public static byte[] zipDecompress(byte[] input) throws IOException {
    Inflater inflator = new Inflater();
    inflator.setInput(input);//from www .  j  a  v  a2s.co  m
    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
    byte[] buf = new byte[1024];
    try {
        while (true) {
            int count = inflator.inflate(buf);
            if (count > 0) {
                bos.write(buf, 0, count);
            } else if (count == 0 && inflator.finished()) {
                break;
            } else {
                throw new RuntimeException("bad zip data, size:" + input.length);
            }
        }
    } catch (DataFormatException t) {
        throw new RuntimeException(t);
    } finally {
        inflator.end();
    }
    return bos.toByteArray();
}

From source file:com.qatickets.common.ZIPHelper.java

public static byte[] decompressAsBytes(byte[] data) {

    if (data == null) {
        return null;
    }/*from ww  w  .j  av a 2 s .  co m*/

    // Create the decompressor and give it the data to compress
    final Inflater decompressor = new Inflater();
    decompressor.setInput(data);

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

    // Decompress the data
    final byte[] buf = new byte[1024];
    while (!decompressor.finished()) {
        try {
            final int count = decompressor.inflate(buf);
            bos.write(buf, 0, count);
        } catch (DataFormatException e) {
        }
    }
    try {
        bos.close();
    } catch (IOException e) {
    }

    // Get the decompressed data
    final byte[] decompressedData = bos.toByteArray();

    decompressor.end();

    return decompressedData;
}

From source file:Main.java

/**
 * Inflate the given byte array by {@link #INFLATED_ARRAY_LENGTH}.
 *
 * @param bytes the bytes/*from   w ww  .j av  a 2  s.c om*/
 * @return the array as a string with {@code UTF-8} encoding
 */
public static String inflate(final byte[] bytes) {
    final Inflater inflater = new Inflater(true);
    final byte[] xmlMessageBytes = new byte[INFLATED_ARRAY_LENGTH];

    final byte[] extendedBytes = new byte[bytes.length + 1];
    System.arraycopy(bytes, 0, extendedBytes, 0, bytes.length);
    extendedBytes[bytes.length] = 0;

    inflater.setInput(extendedBytes);

    try {
        final int resultLength = inflater.inflate(xmlMessageBytes);
        inflater.end();

        if (!inflater.finished()) {
            throw new RuntimeException("buffer not large enough.");
        }

        inflater.end();
        return new String(xmlMessageBytes, 0, resultLength, StandardCharsets.UTF_8);
    } catch (final DataFormatException e) {
        return null;
    }
}

From source file:de.unidue.inf.is.ezdl.dlcore.utils.StringUtils.java

/**
 * Decompresses a string.//from  ww w.j a va  2s  .c om
 * 
 * @param s
 *            The string to decompress
 * @return The decompressed string
 */
public static String decompress(String s) {
    ByteArrayOutputStream baos = null;
    try {
        Inflater ifl = new Inflater();
        ifl.setInput(Base64.decodeBase64(s));

        baos = new ByteArrayOutputStream();
        while (!ifl.finished()) {
            byte[] buff = new byte[1024];
            int count = ifl.inflate(buff);
            baos.write(buff, 0, count);
        }
        baos.flush();
        byte[] output = baos.toByteArray();

        return new String(output, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        logger.error(e.getMessage(), e);
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    } catch (DataFormatException e) {
        logger.error(e.getMessage(), e);
    } finally {
        ClosingUtils.close(baos);
    }
    return "";
}

From source file:gov.niem.ws.util.SecurityUtil.java

/**
 * Decode data by Base64 decoding, then decompressing with the DEFLATE
 * algorithm; reverses encodeHeader.//from   www  . j a  va 2 s.c o  m
 * 
 * @param encoded
 * @return the decoded, decompressed data
 * @throws DataFormatException
 */
public static String decodeHeader(byte[] encoded) throws DataFormatException {
    // TODO: length limit on encoded?
    byte[] compressedBytes = Base64.decodeBase64(encoded);
    ByteArrayOutputStream out = new ByteArrayOutputStream(compressedBytes.length);
    Inflater inflater = new Inflater();
    inflater.setInput(compressedBytes);
    byte[] buffer = new byte[1024];
    while (!inflater.finished()) {
        int count = inflater.inflate(buffer);
        if (count == 0)
            break;
        out.write(buffer, 0, count);
    }

    try {
        inflater.end();
        out.close();
    } catch (IOException e) {
    }

    return new String(out.toByteArray());
}

From source file:org.socraticgrid.workbench.security.wso2.Saml2Util.java

/**
 * Decoding and deflating the encoded AuthReq
 *
 * @param encodedStr encoded AuthReq//w w w . ja  v a2s .  c  o  m
 * @return decoded AuthReq
 */
public static String decode(String encodedStr) throws Exception {
    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.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();
            String decodedStr = new String(baos.toByteArray());
            return decodedStr;
        }
    } catch (IOException e) {
        throw new Exception("Error when decoding the SAML Request.", e);
    }

}

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  va 2  s  .co  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");
        return "";
    }
}