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.wso2.carbon.identity.sso.saml.tomcat.agent.Util.java

/**
 * Decoding and deflating the encoded AuthReq
 *
 * @param encodedStr/*from   w  ww  .j  a v  a2s  .  c o  m*/
 *            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.ojbc.util.helper.ZipUtils.java

public static byte[] unzip(byte[] data) {
    Inflater inflater = new Inflater();
    inflater.setInput(data);// w  w w  .ja v a 2s .com
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    byte[] buffer = new byte[1024];
    try {
        while (!inflater.finished()) {
            int count;
            count = inflater.inflate(buffer);
            outputStream.write(buffer, 0, count);
        }
        outputStream.close();
    } catch (Exception e) {
        log.error("Failed to unzip data", e);
    }

    byte[] output = outputStream.toByteArray();
    log.debug("Original: " + data.length + " bytes");
    log.debug("Decompressed: " + output.length + " bytes");
    return output;
}

From source file:org.odk.collect.android.utilities.CompressionUtils.java

public static String decompress(String compressedString) throws IOException, DataFormatException {
    if (compressedString == null || compressedString.length() == 0) {
        return compressedString;
    }/*from w  w w  .jav a 2  s.  c o  m*/

    // Decode from base64
    byte[] output = Base64.decodeBase64(compressedString);

    Inflater inflater = new Inflater();
    inflater.setInput(output);

    // Decompresses the bytes
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(output.length);
    byte[] buffer = new byte[1024];
    while (!inflater.finished()) {
        int count = inflater.inflate(buffer);
        outputStream.write(buffer, 0, count);
    }
    outputStream.close();
    byte[] result = outputStream.toByteArray();

    // Decode the bytes into a String
    String outputString = new String(result, "UTF-8");
    Timber.i("Compressed : %d", output.length);
    Timber.i("Decompressed : %d", result.length);
    return outputString;

}

From source file:org.jahia.utils.Url.java

/**
 * Decode facet filter URL parameter/* w ww .j av a  2s  .  c o  m*/
 * @param inputString enocded facet filter URL query parameter
 * @return decoded facet filter parameter
 */
public static String decodeUrlParam(String inputString) {
    if (StringUtils.isEmpty(inputString)) {
        return inputString;
    }
    byte[] input = Base64.decodeBase64(inputString);
    // Decompress the bytes
    Inflater decompresser = new Inflater();
    decompresser.setInput(input, 0, input.length);
    byte[] result = new byte[2048];
    String outputString = "";
    try {
        int resultlength = decompresser.inflate(result);
        decompresser.end();
        outputString = new String(result, 0, resultlength, "UTF-8");
    } catch (DataFormatException e) {
        logger.warn("Not able to decode facet URL: " + inputString, e);
    } catch (UnsupportedEncodingException e) {
        logger.warn("Not able to decode facet URL: " + inputString, e);
    }
    return outputString;
}

From source file:Main.java

/**
 * <p>Decompresses a the content of a file from {@code startPosition} of {@code compressedSize}
 * and return the bytes of it.</p>
 *
 * @param file           the file/*from   w w  w  . ja va 2 s.com*/
 * @param startPosition  the start position
 * @param compressedSize the compresse
 * @return the decompressed bytes
 * @throws IOException         if there was any io issues
 * @throws DataFormatException if there was an issue decompressing content
 */
public static byte[] decompress(File file, int startPosition, int compressedSize)
        throws IOException, DataFormatException {

    try (FileChannel fileChannel = FileChannel.open(file.toPath())) {
        ByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, startPosition, compressedSize);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Inflater inflater = new Inflater();
        byte[] compressedBytes = new byte[compressedSize];
        byte[] inflated = new byte[BUFSIZ];
        int read;

        buffer.get(compressedBytes);
        inflater.setInput(compressedBytes);

        // unzip contents
        while ((read = inflater.inflate(inflated)) != 0) {
            baos.write(inflated, 0, read);
        }

        inflater.end();

        return baos.toByteArray();
    }
}

From source file:Main.java

public static byte[] decompressZLIB(byte[] input) throws IOException {
    Inflater decompressor = new Inflater();
    decompressor.setInput(input);/*from  www . j  a  v  a 2 s  .c  o  m*/
    // 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()) {
        try {
            int count = decompressor.inflate(buf);
            bos.write(buf, 0, count);
        } catch (DataFormatException e) {
            throw new IOException(e.toString());
        }
    }
    bos.close();

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

From source file:Main.java

/**
 * Inflate the given byte array by {@link #INFLATED_ARRAY_LENGTH}.
 *
 * @param bytes the bytes/*from  www. j  a  v a2s . c o m*/
 * @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:com.asual.lesscss.ResourcePackage.java

private static byte[] inflate(byte[] output) throws DataFormatException, IOException {
    Inflater inflater = new Inflater();
    inflater.setInput(output);/*from w w  w.ja v a  2s .c  o  m*/
    ByteArrayOutputStream baos = new ByteArrayOutputStream(output.length);
    byte[] buf = new byte[1024];
    while (!inflater.finished()) {
        int count = inflater.inflate(buf);
        baos.write(buf, 0, count);
    }
    baos.close();
    return baos.toByteArray();
}

From source file:com.predic8.membrane.core.util.ByteUtil.java

public static byte[] getDecompressedData(byte[] compressedData) throws IOException {
    Inflater decompressor = new Inflater(true);
    decompressor.setInput(compressedData);

    byte[] buf = new byte[1024];

    List<Chunk> chunks = new ArrayList<Chunk>();

    while (!decompressor.finished()) {
        int count;
        try {/*from w  w  w .j  av  a2  s.  c  o  m*/
            count = decompressor.inflate(buf);
        } catch (DataFormatException e) {
            throw new IOException(e);
        }
        if (buf.length == count) {
            Chunk chunk = new Chunk(buf);
            chunks.add(chunk);
        } else if (count < buf.length) {
            byte[] shortContent = new byte[count];
            for (int j = 0; j < count; j++) {
                shortContent[j] = buf[j];
            }
            Chunk chunk = new Chunk(shortContent);
            chunks.add(chunk);
        }
    }

    log.debug("Number of decompressed chunks: " + chunks.size());
    if (chunks.size() > 0) {

        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        for (Chunk chunk : chunks) {
            chunk.write(bos);
        }

        try {
            bos.close();
        } catch (IOException e) {
        }
        return bos.toByteArray();
    }

    return null;
}

From source file:Main.java

public static byte[] decompress(byte[] data, int off, int len) {
    byte[] output = null;
    Inflater decompresser = new Inflater();
    decompresser.reset();/*w w  w.j a  v a  2 s .  co  m*/
    //      decompresser.setInput(data);
    decompresser.setInput(data, off, len);

    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) {
        throw new RuntimeException(e);
    } finally {
        try {
            o.close();
            decompresser.end();
        } catch (Exception e) {
        }
    }

    return output;
}