Example usage for java.util.zip Inflater end

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

Introduction

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

Prototype

public void end() 

Source Link

Document

Closes the decompressor and discards any unprocessed input.

Usage

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 www  . j  a  va2s .  c  om*/
 * @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 void decompress(byte[] data, int off, int len, OutputStream out) {
    Inflater decompresser = new Inflater();
    decompresser.reset();//  w  w  w  .j ava  2  s. co m
    decompresser.setInput(data, off, len);
    byte[] buf = new byte[1024];

    try {
        while (!decompresser.finished()) {
            int i = decompresser.inflate(buf);
            out.write(buf, 0, i);
            out.flush();
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        decompresser.end();
    }
}

From source file:org.wso2.identity.integration.test.requestPathAuthenticator.RequestPathAuthenticatorInvalidUserTestCase.java

/**
 * Decoding and deflating the encoded AuthReq
 *
 * @param encodedStr encoded AuthReq//from w  ww  .  ja  va2s. 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 "";
    }
}

From source file:radixcore.network.ByteBufIO.java

/**
 * Decompresses a compressed byte array.
 * /*  ww w .j a v  a2  s .c o m*/
 * @param input The byte array to be decompressed.
 * @return The byte array in its decompressed, readable form.
 */
public static byte[] decompress(byte[] input) {
    try {
        final Inflater inflater = new Inflater();
        final ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(input.length);
        final byte[] buffer = new byte[1024];
        inflater.setInput(input);

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

        inflater.end();
        byteOutput.close();
        return byteOutput.toByteArray();
    }

    catch (final DataFormatException e) {
        RadixExcept.logFatalCatch(e, "Error decompressing byte array.");
        return null;
    }

    catch (final IOException e) {
        RadixExcept.logFatalCatch(e, "Error decompressing byte array.");
        return null;
    }
}

From source file:Main.java

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

    Inflater decompresser = new Inflater();
    decompresser.reset();// www. j  a  v  a 2s  .c o 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:org.wso2.carbon.identity.sso.saml.tomcat.agent.Util.java

/**
 * Decoding and deflating the encoded AuthReq
 *
 * @param encodedStr/*from   w w w. ja  v  a  2s .  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.jasig.cas.util.CompressionUtils.java

/**
 * Inflate the given byte array by {@link #INFLATED_ARRAY_LENGTH}.
 *
 * @param bytes the bytes//from   w w  w  .  jav  a 2 s  .c  o  m
 * @return the array as a string with <code>UTF-8</code> 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, UTF8_ENCODING);
    } catch (final DataFormatException e) {
        LOGGER.error("Data format is not supported", e);
        return null;
    } catch (final UnsupportedEncodingException e) {
        throw new RuntimeException("Cannot find encoding:" + UTF8_ENCODING, e);
    }
}

From source file:Main.java

public static byte[] decompress(byte[] data) throws IOException {
    Inflater inflater = new Inflater();
    inflater.setInput(data);//from  www  . j  a v a  2  s. co m
    inflater.finished();

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    try {
        byte[] buffer = new byte[1024];
        while (!inflater.finished()) {
            int count = inflater.inflate(buffer);
            outputStream.write(buffer, 0, count);
        }
        outputStream.close();
    } catch (DataFormatException ex) {
        throw new IOException(ex);
    }

    byte[] output = outputStream.toByteArray();
    inflater.end();
    return output;
}

From source file:Main.java

public static byte[] decompress(byte[] compressedBuffer) {
    Inflater inflater = new Inflater();
    inflater.setInput(compressedBuffer);

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(compressedBuffer.length);
    try {/*from w  ww  .j  a  v a2 s.c  o m*/
        byte[] buffer = new byte[1024];
        while (!inflater.finished()) {
            int count = inflater.inflate(buffer);
            outputStream.write(buffer, 0, count);
        }
        byte[] output = outputStream.toByteArray();
        return output;
    } catch (DataFormatException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            inflater.end();
            outputStream.close();
        } catch (IOException e) {
            throw new RuntimeException(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   ww w  .j av  a  2s.  c  o 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);
    }
}