Example usage for java.util.zip Inflater Inflater

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

Introduction

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

Prototype

public Inflater(boolean nowrap) 

Source Link

Document

Creates a new decompressor.

Usage

From source file:net.awl.edoc.pdfa.compression.FlateDecode.java

public static void main(String[] args) throws Exception {
    Inflater inf = new Inflater(false);

    File f = new File("resources/content_2_0.ufd");
    FileInputStream fis = new FileInputStream(f);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    IOUtils.copy(fis, baos);//ww w  . j  av  a  2 s . co  m
    IOUtils.closeQuietly(fis);
    IOUtils.closeQuietly(baos);
    byte[] buf = baos.toByteArray();
    inf.setInput(buf);
    byte[] res = new byte[buf.length];
    int size = inf.inflate(res);
    String s = new String(res, 0, size, "utf8");
    System.err.println(s);

}

From source file:Main.java

/**
 * Inflate the given byte array by {@link #INFLATED_ARRAY_LENGTH}.
 *
 * @param bytes the bytes//from  ww w.  java 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:Main.java

public static InputStream getInputEncoding(URLConnection connection) throws IOException {
    InputStream in;/*from  w ww . ja  va  2s  .  c om*/
    String encoding = connection.getContentEncoding();
    if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
        in = new GZIPInputStream(connection.getInputStream());
    } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
        in = new InflaterInputStream(connection.getInputStream(), new Inflater(true));
    } else {
        in = connection.getInputStream();
    }
    return in;
}

From source file:Main.java

static String downloadHtml(String urlString) {
    StringBuffer buffer = new StringBuffer();

    try {/*from ww  w  . ja va2s .  c o  m*/
        URL url = new URL(urlString);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        HttpURLConnection.setFollowRedirects(true);
        conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
        String encoding = conn.getContentEncoding();
        InputStream inStr = null;

        if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
            inStr = new GZIPInputStream(conn.getInputStream());
        } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
            inStr = new InflaterInputStream(conn.getInputStream(), new Inflater(true));
        } else {
            inStr = conn.getInputStream();
        }
        int ptr = 0;
        InputStreamReader inStrReader = new InputStreamReader(inStr, Charset.forName("GB2312"));

        while ((ptr = inStrReader.read()) != -1) {
            buffer.append((char) ptr);
        }
        inStrReader.close();
        conn.disconnect();
        inStr.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return buffer.toString();
}

From source file:org.xdi.zip.CompressionHelper.java

public static byte[] inflate(byte[] data, boolean nowrap) throws IOException, DataFormatException {
    Inflater inflater = new Inflater(nowrap);
    inflater.setInput(data);/*  w  w w  .  ja v  a 2 s. c  o  m*/

    ByteArrayOutputStream os = new ByteArrayOutputStream(data.length);
    try {
        byte[] buffer = new byte[1024];
        while (!inflater.finished()) {
            int count = inflater.inflate(buffer);
            os.write(buffer, 0, count);
        }
    } finally {
        IOUtils.closeQuietly(os);
    }

    return os.toByteArray();
}

From source file:Main.java

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

    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[] decompress(byte[] input, boolean GZIPFormat) throws IOException, DataFormatException {
    Inflater decompressor = new Inflater(GZIPFormat);
    decompressor.setInput(input);/*from   ww w. ja  v a 2 s  . c  om*/
    ByteArrayOutputStream bao = new ByteArrayOutputStream();
    byte[] readBuffer = new byte[1024];
    int readCount = 0;

    while (!decompressor.finished()) {
        readCount = decompressor.inflate(readBuffer);
        if (readCount > 0) {
            bao.write(readBuffer, 0, readCount);
        }
    }
    decompressor.end();
    return bao.toByteArray();
}

From source file:org.openmrs.module.shr.contenthandler.DataUtil.java

public static byte[] uncompressDeflate(byte[] content) throws IOException {
    InflaterInputStream deflateIn = new InflaterInputStream(new ByteArrayInputStream(content),
            new Inflater(true));
    return copyToByteArray(deflateIn);
}

From source file:org.jasig.cas.util.CompressionUtils.java

/**
 * Inflate the given byte array by {@link #INFLATED_ARRAY_LENGTH}.
 *
 * @param bytes the bytes/*from ww  w .  j  a  v  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:org.samlsnort.util.EncodingTool.java

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