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:org.javaweb.utils.IOUtils.java

/**
 * Inflater // ww  w  . j av  a  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:Main.java

public static InputStream inflate(byte[] deflatedToken, boolean nowrap) throws DataFormatException {
    Inflater inflater = new Inflater(nowrap);
    inflater.setInput(deflatedToken);//from  ww  w.  j a  va  2 s .  c om

    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;
                }
            }

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

public static byte[] decompressZLIB(byte[] input) throws IOException {
    Inflater decompressor = new Inflater();
    decompressor.setInput(input);//  w w w. j  a v a2 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: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 ww w  .  j  a va2s. c om

    // 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: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   www  . j  a  v  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: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  a2 s. 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:org.wso2.identity.integration.test.requestPathAuthenticator.RequestPathAuthenticatorInvalidUserTestCase.java

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

From source file:Main.java

public static byte[] decompress(byte[] data, int off, int len) {
    byte[] output = null;
    Inflater decompresser = new Inflater();
    decompresser.reset();/*from   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;
}

From source file:Main.java

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

    ByteArrayOutputStream o = new ByteArrayOutputStream(srcLen);
    try {
        o.reset();
        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;
}

From source file:ZipUtil.java

/**
 * Inflates a previously deflated file./*from  w  w w.  j  av  a 2 s.  c o  m*/
 */
public static byte[] unzipByteArray(byte[] file) throws IOException {
    byte[] byReturn = null;

    Inflater oInflate = new Inflater(false);
    oInflate.setInput(file);

    ByteArrayOutputStream oZipStream = new ByteArrayOutputStream();
    try {
        while (!oInflate.finished()) {
            byte[] byRead = new byte[ZIP_BUFFER_SIZE];
            int iBytesRead = oInflate.inflate(byRead);
            if (iBytesRead == byRead.length) {
                oZipStream.write(byRead);
            } else {
                oZipStream.write(byRead, 0, iBytesRead);
            }
        }
        byReturn = oZipStream.toByteArray();
    } catch (DataFormatException ex) {
        throw new IOException("Attempting to unzip file that is not zipped.");
    } finally {
        oZipStream.close();
    }
    return byReturn;
}