Example usage for java.util.zip Inflater inflate

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

Introduction

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

Prototype

public int inflate(ByteBuffer output) throws DataFormatException 

Source Link

Document

Uncompresses bytes into specified buffer.

Usage

From source file:org.atricore.idbus.capabilities.sso.main.binding.SamlR2HttpRedirectBinding.java

public static String inflateFromRedirect(String redirStr, boolean decode) throws Exception {

    if (redirStr == null || redirStr.length() == 0) {
        throw new RuntimeException("Redirect string cannot be null or empty");
    }/*from w  w w. j a  va  2 s . c  o m*/

    byte[] redirBin = null;
    if (decode)
        redirBin = new Base64().decode(removeNewLineChars(redirStr).getBytes());
    else
        redirBin = redirStr.getBytes();

    // Decompress the bytes
    Inflater inflater = new Inflater(true);
    inflater.setInput(redirBin);

    ByteArrayOutputStream baos = new ByteArrayOutputStream(8192);

    try {
        int resultLength = 0;
        int buffSize = 1024;
        byte[] buff = new byte[buffSize];
        while (!inflater.finished()) {
            resultLength = inflater.inflate(buff);
            baos.write(buff, 0, resultLength);
        }

    } catch (DataFormatException e) {
        throw new RuntimeException("Cannot inflate SAML message : " + e.getMessage(), e);
    }

    inflater.end();

    // Decode the bytes into a String
    String outputString = null;
    try {
        outputString = new String(baos.toByteArray(), "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("Cannot convert byte array to string " + e.getMessage(), e);
    }
    return outputString;
}

From source file:edu.umn.msi.tropix.proteomics.conversion.impl.ConversionUtils.java

public static byte[] decompress(byte[] compressedData) {
    byte[] decompressedData;

    // using a ByteArrayOutputStream to not having to define the result array size beforehand
    Inflater decompressor = new Inflater();

    decompressor.setInput(compressedData);
    // Create an expandable byte array to hold the decompressed data
    ByteArrayOutputStream bos = new ByteArrayOutputStream(compressedData.length);
    byte[] buf = new byte[1024];
    while (!decompressor.finished()) {
        try {/*from w w w .  j  a  va 2s . c  o  m*/
            int count = decompressor.inflate(buf);
            if (count == 0 && decompressor.needsInput()) {
                break;
            }
            bos.write(buf, 0, count);
        } catch (DataFormatException e) {
            throw new IllegalStateException(
                    "Encountered wrong data format " + "while trying to decompress binary data!", e);
        }
    }
    try {
        bos.close();
    } catch (IOException e) {
        // ToDo: add logging
        e.printStackTrace();
    }
    // Get the decompressed data
    decompressedData = bos.toByteArray();

    if (decompressedData == null) {
        throw new IllegalStateException("Decompression of binary data produced no result (null)!");
    }
    return decompressedData;
}

From source file:r.base.Connections.java

public static byte[] decompress1(byte buffer[]) throws IOException, DataFormatException {
    DataInputStream in = new DataInputStream(new ByteArrayInputStream(buffer));
    int outLength = in.readInt();

    Inflater inflater = new Inflater();
    inflater.setInput(buffer, 4, buffer.length - 4);

    byte[] result = new byte[outLength];
    inflater.inflate(result);
    inflater.end();/*  w  ww  .j  a  va 2s  .com*/

    return result;
}

From source file:aarddict.Volume.java

static String decompressZlib(byte[] bytes) throws IOException, DataFormatException {
    Inflater decompressor = new Inflater();
    decompressor.setInput(bytes);//from w  w w . j a  v a 2s .co  m
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        byte[] buf = new byte[1024];
        while (!decompressor.finished()) {
            int count = decompressor.inflate(buf);
            out.write(buf, 0, count);
        }
    } finally {
        out.close();
    }
    return utf8(out.toByteArray());
}

From source file:com.bigdata.dastor.utils.FBUtilities.java

public static byte[] decompress(byte[] compressedData, int off, int len)
        throws IOException, DataFormatException {
    // Create the decompressor and give it the data to compress
    Inflater decompressor = new Inflater();
    decompressor.setInput(compressedData, off, len);

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

    // Decompress the data
    byte[] buf = new byte[1024];
    while (!decompressor.finished()) {
        int count = decompressor.inflate(buf);
        bos.write(buf, 0, count);/* w w w.ja va2  s  . com*/
    }
    bos.close();

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

From source file:org.apache.marmotta.kiwi.io.KiWiIO.java

/**
 * Read a potentially compressed string from the data input.
 *
 * @param in// w  ww.  jav  a  2  s.  c om
 * @return
 * @throws IOException
 */
private static String readContent(DataInput in) throws IOException {
    int mode = in.readByte();

    if (mode == MODE_COMPRESSED) {
        try {
            int strlen = in.readInt();
            int buflen = in.readInt();

            byte[] buffer = new byte[buflen];
            in.readFully(buffer);

            Inflater decompressor = new Inflater(true);
            decompressor.setInput(buffer);

            byte[] data = new byte[strlen];
            decompressor.inflate(data);
            decompressor.end();

            return new String(data, "UTF-8");
        } catch (DataFormatException ex) {
            throw new IllegalStateException("input data is not valid", ex);
        }
    } else {
        return DataIO.readString(in);
    }
}

From source file:org.apache.geode.management.internal.cli.CliUtil.java

public static DeflaterInflaterData uncompressBytes(byte[] output, int compressedDataLength)
        throws DataFormatException {
    Inflater decompresser = new Inflater();
    decompresser.setInput(output, 0, compressedDataLength);
    byte[] buffer = new byte[512];
    byte[] result = new byte[0];
    int bytesRead;
    while (!decompresser.needsInput()) {
        bytesRead = decompresser.inflate(buffer);
        byte[] newResult = new byte[result.length + bytesRead];
        System.arraycopy(result, 0, newResult, 0, result.length);
        System.arraycopy(buffer, 0, newResult, result.length, bytesRead);
        result = newResult;//from w  w w  .j  a v a 2s .  com
    }
    decompresser.end();

    return new DeflaterInflaterData(result.length, result);
}

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

/**
 * Decoding and deflating the encoded AuthReq
 *
 * @param encodedStr encoded AuthReq//from  w  ww .j ava2 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(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:com.kactech.otj.Utils.java

public static byte[] zlibDecompress(byte[] data) throws IOException, DataFormatException {
    Inflater inflater = new Inflater(false);

    inflater.setInput(data);//from  w ww. j a v a2  s  .c  o  m

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    byte[] buffer = new byte[1024];
    while (!inflater.finished()) {
        int count = inflater.inflate(buffer);
        if (count == 0)
            throw new DataFormatException("probably bad, has infinite loop at encoded message");
        outputStream.write(buffer, 0, count);
    }
    outputStream.close();
    byte[] output = outputStream.toByteArray();

    return output;
}

From source file:acp.sdk.SecureUtil.java

/**
 * ./*w  ww  . j  a va2s . c om*/
 * 
 * @param inputByte
 *            byte[]?
 * @return ??
 * @throws IOException
 */
public static byte[] inflater(final byte[] inputByte) throws IOException {
    int compressedDataLength = 0;
    Inflater compresser = new Inflater(false);
    compresser.setInput(inputByte, 0, inputByte.length);
    ByteArrayOutputStream o = new ByteArrayOutputStream(inputByte.length);
    byte[] result = new byte[1024];
    try {
        while (!compresser.finished()) {
            compressedDataLength = compresser.inflate(result);
            if (compressedDataLength == 0) {
                break;
            }
            o.write(result, 0, compressedDataLength);
        }
    } catch (Exception ex) {
        System.err.println("Data format error!\n");
        ex.printStackTrace();
    } finally {
        o.close();
    }
    compresser.end();
    return o.toByteArray();
}