Example usage for java.util.zip Inflater setInput

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

Introduction

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

Prototype

public void setInput(ByteBuffer input) 

Source Link

Document

Sets input data for decompression.

Usage

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

public static String decode(String encodedStr) throws AppFactoryException {
    try {//from www.  j  a v a2 s.com
        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.jasig.cas.authentication.principal.GoogleAccountsService.java

private static String inflate(final byte[] bytes) {
    final Inflater inflater = new Inflater(true);
    final byte[] xmlMessageBytes = new byte[10000];

    final byte[] extendedBytes = new byte[bytes.length + 1];
    System.arraycopy(bytes, 0, extendedBytes, 0, bytes.length);
    extendedBytes[bytes.length] = 0;//from   w  ww . j  a  va 2s  . co  m

    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, "UTF-8");
    } catch (final DataFormatException e) {
        return null;
    } catch (final UnsupportedEncodingException e) {
        throw new RuntimeException("Cannot find encoding: UTF-8", e);
    }
}

From source file:aarddict.Volume.java

static String decompressZlib(byte[] bytes) throws IOException, DataFormatException {
    Inflater decompressor = new Inflater();
    decompressor.setInput(bytes);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {/*w w w.j  a  v a  2s  . com*/
        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:org.apache.marmotta.kiwi.io.KiWiIO.java

/**
 * Read a potentially compressed string from the data input.
 *
 * @param in/*from   ww w.  j  av a2 s . c  o  m*/
 * @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:com.kactech.otj.Utils.java

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

    inflater.setInput(data);

    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);
    }//from  w ww.j av a2 s.  c om
    outputStream.close();
    byte[] output = outputStream.toByteArray();

    return output;
}

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

/**
 * Decoding and deflating the encoded AuthReq
 *
 * @param encodedStr encoded AuthReq//from   ww w. j a 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(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:nl.nn.adapterframework.util.Misc.java

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

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

    // Decompress the data
    byte[] buf = new byte[1024];
    while (!decompressor.finished()) {
        int count = decompressor.inflate(buf);
        bos.write(buf, 0, count);/*from  w  ww .  jav a2s  . co m*/
    }
    bos.close();

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

From source file:com.ctriposs.r2.filter.compression.DeflateCompressor.java

@Override
public byte[] inflate(InputStream data) throws CompressionException {
    byte[] input;
    try {//w w w  . j av  a  2  s.c o  m
        input = IOUtils.toByteArray(data);
    } catch (IOException e) {
        throw new CompressionException(CompressionConstants.DECODING_ERROR + CompressionConstants.BAD_STREAM,
                e);
    }

    Inflater zlib = new Inflater();
    zlib.setInput(input);

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    byte[] temp = new byte[CompressionConstants.BUFFER_SIZE];

    int bytesRead;
    while (!zlib.finished()) {
        try {
            bytesRead = zlib.inflate(temp);
        } catch (DataFormatException e) {
            throw new CompressionException(CompressionConstants.DECODING_ERROR + getContentEncodingName(), e);
        }
        if (bytesRead == 0) {
            if (!zlib.needsInput()) {
                throw new CompressionException(CompressionConstants.DECODING_ERROR + getContentEncodingName());
            } else {
                break;
            }
        }

        if (bytesRead > 0) {
            output.write(temp, 0, bytesRead);
        }
    }

    zlib.end();
    return output.toByteArray();
}

From source file:org.getspout.spoutapi.packet.PacketSendPrecache.java

public void decompress() {
    if (compressed) {
        Inflater decompressor = new Inflater();
        decompressor.setInput(fileData);
        ByteArrayOutputStream bos = new ByteArrayOutputStream(fileData.length);
        byte[] buf = new byte[1024];
        while (!decompressor.finished()) {
            try {
                int count = decompressor.inflate(buf);
                bos.write(buf, 0, count);
            } catch (DataFormatException e) {
            }//from w  w  w  .  j a  v a  2 s  .  co m
        }
        try {
            bos.close();
        } catch (IOException e) {
        }
        fileData = bos.toByteArray();
    }
}

From source file:org.hyperic.hq.livedata.agent.commands.LiveData_result.java

private String decompress(String s) throws IOException, DataFormatException {
    Inflater decompressor = new Inflater();
    decompressor.setInput(Base64.decode(s));

    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    byte[] buf = new byte[1024];
    while (!decompressor.finished()) {
        int count = decompressor.inflate(buf);
        bos.write(buf, 0, count);//from   w w  w .ja v  a2s .c o m
    }

    bos.close();

    byte[] decompressedData = bos.toByteArray();
    return new String(decompressedData, 0, decompressedData.length, "UTF-8");
}