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:Main.java

public static byte[] decompress(byte[] data) {
    byte[] output = new byte[0];

    Inflater decompresser = new Inflater();
    decompresser.reset();//from w w  w .  j  a  v  a2 s  .  co m
    decompresser.setInput(data);

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

    decompresser.end();
    return output;
}

From source file:Main.java

public static InputStream inflate(byte[] deflatedToken, boolean nowrap) throws DataFormatException {
    Inflater inflater = new Inflater(nowrap);
    inflater.setInput(deflatedToken);

    byte[] input = new byte[deflatedToken.length * 2];
    int inflatedLen = 0;
    int inputLen = 0;
    byte[] inflatedToken = input;
    while (!inflater.finished()) {
        inputLen = inflater.inflate(input);
        if (!inflater.finished()) {

            if (inputLen == 0) {
                if (inflater.needsInput()) {
                    throw new DataFormatException("Inflater can not inflate all the token bytes");
                } else {
                    break;
                }/*from   w w w  . j a v a  2 s. c  o m*/
            }

            inflatedToken = new byte[input.length + inflatedLen];
            System.arraycopy(input, 0, inflatedToken, inflatedLen, inputLen);
            inflatedLen += inputLen;
        }
    }
    InputStream is = new ByteArrayInputStream(input, 0, inputLen);
    if (inflatedToken != input) {
        is = new SequenceInputStream(new ByteArrayInputStream(inflatedToken, 0, inflatedLen), is);
    }
    return is;
}

From source file:org.ojbc.util.helper.ZipUtils.java

public static byte[] unzip(byte[] data) {
    Inflater inflater = new Inflater();
    inflater.setInput(data);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    byte[] buffer = new byte[1024];
    try {//  w  w w .ja  v  a 2s  .c o m
        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.samlsnort.util.EncodingTool.java

public static String inflate(byte[] deflated) throws DataFormatException {
    Inflater inflater = new Inflater(true);
    inflater.setInput(deflated);
    byte[] inflatedBytes = new byte[4096];
    int len = inflater.inflate(inflatedBytes);
    inflater.setInput(new byte[0]);
    len += inflater.inflate(inflatedBytes, len, 1);
    inflater.end();//from   www  .j  ava2 s.  c  o m
    return new String(inflatedBytes, 0, len, CHARSET);
}

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 .j a  va2  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:Main.java

/**
 * @param data                  -- the data to decompress
 * @param uncompressedChunkSize -- an estimate of the uncompressed chunk size.  This need not be exact.
 * @return/*from w  ww  .j a v  a2s.c om*/
 */
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:org.javaweb.utils.IOUtils.java

/**
 * Inflater //from w  w w .j  ava  2  s  .c o  m
 *
 * @param data
 * @return
 * @throws DataFormatException
 */
public static byte[] decompressInflater(byte[] data) throws DataFormatException {
    Inflater inflater = new Inflater();
    inflater.setInput(data);
    ByteArrayOutputStream out = new ByteArrayOutputStream(data.length);
    byte[] buffer = new byte[2048];

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

    return out.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 {//  w w  w.  j ava  2s. co 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:com.qatickets.common.ZIPHelper.java

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

    if (data == null) {
        return null;
    }/*from   w  w  w  . j av a2s. c om*/

    // 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:org.wso2.identity.integration.test.requestPathAuthenticator.RequestPathAuthenticatorInvalidUserTestCase.java

/**
 * Decoding and deflating the encoded AuthReq
 *
 * @param encodedStr encoded AuthReq/*w w  w.  j  ava 2 s . c  om*/
 * @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", e);
        return "";
    }
}