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:com.asual.lesscss.ResourcePackage.java

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

From source file:org.wso2.carbon.identity.sso.saml.tomcat.agent.Util.java

/**
 * Decoding and deflating the encoded AuthReq
 *
 * @param encodedStr//w w  w  . j a v a 2 s .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:ch.cern.security.saml2.utils.xml.XMLUtils.java

public static String xmlDecodeAndInflate(String encodedXmlString, boolean isDebugEnabled)
        throws UnsupportedEncodingException, DataFormatException {

    // URL decode
    // No need to URL decode: auto decoded by request.getParameter() method (GET)

    if (isDebugEnabled)
        nc.notice("Encoded XML String: " + encodedXmlString);

    // Base64 decode
    Base64 base64Decoder = new Base64();
    byte[] xmlBytes = encodedXmlString.getBytes("UTF-8");
    byte[] base64DecodedByteArray = base64Decoder.decode(xmlBytes);

    if (isDebugEnabled)
        nc.notice("Base64 decoded bytes: " + new String(base64DecodedByteArray, "UTF-8"));

    // Inflate (uncompress) the AuthnRequest data
    // First attempt to unzip the byte array according to DEFLATE (rfc 1951)
    Inflater inflater = new Inflater(true);
    inflater.setInput(base64DecodedByteArray);
    // since we are decompressing, it's impossible to know how much space we
    // might need; hopefully this number is suitably big
    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");
    }// ww w .ja v  a  2  s.  c o  m

    inflater.end();

    String decodedString = new String(xmlMessageBytes, 0, resultLength, "UTF-8");

    if (isDebugEnabled)
        nc.notice("Decoded and inflated string (UTF-8): " + new String(base64DecodedByteArray));

    return decodedString;
}

From source file:ZipUtil.java

/**
 * Inflates a previously deflated file.//from   w  w w.j av a 2  s .com
 */
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;
}

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  ww.  j  a va 2 s.co m
 * @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:org.ow2.proactive.utils.ObjectByteConverter.java

/**
 * Convert the given byte array into the corresponding object.
 * <p>//from  ww w. j a  v  a 2 s. c  om
 * The given byteArray can be uncompressed if it has been compressed before.
 * 
 * @param input the byteArray to be convert as an object.
 * @param uncompress true if the given byteArray must be also uncompressed, false if no compression was made on it.
 * @return the object corresponding to the given byteArray.
 * @throws IOException if an I/O exception occurs when writing the returned object
 * @throws ClassNotFoundException if class represented by given byteArray is not found.
 */
public static Object byteArrayToObject(byte[] input, boolean uncompress)
        throws IOException, ClassNotFoundException {
    if (input == null) {
        return null;
    }
    if (uncompress) {
        // Uncompress the bytes
        Inflater decompressor = new Inflater();
        decompressor.setInput(input);

        ByteArrayOutputStream bos = null;
        try {
            // Create an expandable byte array to hold the compressed data.
            bos = new ByteArrayOutputStream();
            // Compress the data
            byte[] buf = new byte[512];
            while (!decompressor.finished()) {
                int count = decompressor.inflate(buf);
                bos.write(buf, 0, count);
            }
            decompressor.end();
            // set the UNCOMPRESSED data
            input = bos.toByteArray();
        } catch (DataFormatException dfe) {
            //convert into io exception to fit previous behavior
            throw new IOException("Compressed data format is invalid : " + dfe.getMessage(), dfe);
        } finally {
            if (bos != null) {
                bos.close();
            }
        }
    }
    //here, input byteArray is uncompressed if needed
    ByteArrayInputStream bais = null;
    ObjectInputStream ois = null;
    try {
        bais = new ByteArrayInputStream(input);
        ois = new ObjectInputStream(bais);
        return ois.readObject();
    } finally {
        if (ois != null) {
            ois.close();
        }
        if (bais != null) {
            bais.close();
        }
    }
}

From source file:edu.stanford.junction.addon.JSONObjWrapper.java

private static String decompressString(String str) {
    byte[] compressedData = Base64.decode(str);
    // Create the decompressor and give it the data to compress 
    Inflater decompressor = new Inflater();
    decompressor.setInput(compressedData);
    // Create an expandable byte array to hold the decompressed data 
    ByteArrayOutputStream bos = new ByteArrayOutputStream(compressedData.length);
    // Decompress the data 
    byte[] buf = new byte[1024];
    while (!decompressor.finished()) {
        try {/* w  ww. j  a  v a  2s . c om*/
            int count = decompressor.inflate(buf);
            bos.write(buf, 0, count);
        } catch (DataFormatException e) {
        }
    }

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

    // Get the decompressed data 
    byte[] decompressedData = bos.toByteArray();
    try {
        return new String(decompressedData, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        return new String(decompressedData);
    }
}

From source file:Main.java

/**
 * Inflate the given byte array by {@link #INFLATED_ARRAY_LENGTH}.
 *
 * @param bytes the bytes// w  w w .  ja  v a  2s.  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:org.socraticgrid.workbench.security.wso2.Saml2Util.java

/**
 * Decoding and deflating the encoded AuthReq
 *
 * @param encodedStr encoded AuthReq/*ww w. j ava 2s.  c om*/
 * @return decoded AuthReq
 */
public static String decode(String encodedStr) throws Exception {
    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.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();
            String decodedStr = new String(baos.toByteArray());
            return decodedStr;
        }
    } catch (IOException e) {
        throw new Exception("Error when decoding the SAML Request.", e);
    }

}

From source file:radixcore.network.ByteBufIO.java

/**
 * Decompresses a compressed byte array.
 * /* w ww . j  a  va  2s .  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;
    }
}