Example usage for java.util.zip Inflater inflate

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

Introduction

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

Prototype

public int inflate(ByteBuffer output) throws DataFormatException 

Source Link

Document

Uncompresses bytes into specified buffer.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception { // main method
    // Encode a String into bytes
    String inputString = "this is a test";
    byte[] input = inputString.getBytes("UTF-8");

    // Compress the bytes
    byte[] output1 = new byte[input.length];
    Deflater compresser = new Deflater();
    compresser.setInput(input);/*  w  w  w.  j  a v  a2 s .  c o m*/
    compresser.finish();
    int compressedDataLength = compresser.deflate(output1);
    compresser.end();

    String str = new String(Base64.getEncoder().encode(output1));
    System.out.println("Deflated String:" + str);

    byte[] output2 = Base64.getDecoder().decode(str);

    // Decompress the bytes
    Inflater decompresser = new Inflater();
    decompresser.setInput(output2);
    byte[] result = str.getBytes();
    int resultLength = decompresser.inflate(result);
    decompresser.end();

    // Decode the bytes into a String
    String outputString = new String(result, 0, resultLength, "UTF-8");
    System.out.println("Deflated String:" + outputString);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    byte[] compressedData = null;
    Inflater decompressor = new Inflater();
    decompressor.setInput(compressedData);
    ByteArrayOutputStream bos = new ByteArrayOutputStream(compressedData.length);
    byte[] buf = new byte[1024];
    while (!decompressor.finished()) {
        int count = decompressor.inflate(buf);
        bos.write(buf, 0, count);//  w ww. j  av a2s.c  om

    }
    bos.close();
    byte[] decompressedData = bos.toByteArray();

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    byte[] input = "this is a test".getBytes();

    Deflater compressor = new Deflater();
    compressor.setLevel(Deflater.BEST_COMPRESSION);

    compressor.setInput(input);//from   w  w w . ja  va 2  s.c  o  m
    compressor.finish();

    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);

    byte[] buf = new byte[1024];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }
    bos.close();

    byte[] compressedData = bos.toByteArray();
    Inflater decompressor = new Inflater();
    decompressor.setInput(compressedData);

    bos = new ByteArrayOutputStream(compressedData.length);

    buf = new byte[1024];
    while (!decompressor.finished()) {
        int count = decompressor.inflate(buf);
        bos.write(buf, 0, count);
    }
    bos.close();

    byte[] decompressedData = bos.toByteArray();
    System.out.println(new String(decompressedData));
}

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);//from   w ww .  ja v  a2  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

private static byte[] expand(byte[] bytes, int skip) {
    byte[] newBytes = new byte[bytes.length - skip];
    Inflater inflater = new Inflater();

    inflater.setInput(bytes, skip, newBytes.length);
    try {/* w  w w.  j  a  v a  2 s  .co  m*/
        int outCount = inflater.inflate(newBytes);
        System.arraycopy(newBytes, 0, bytes, skip, outCount);
        Arrays.fill(bytes, skip + outCount, bytes.length, (byte) 0);
        return bytes;
    } catch (DataFormatException e) {
    }

    return null;
}

From source file:org.samlsnort.util.EncodingTool.java

public static String inflate(byte[] deflated) throws DataFormatException {
    Inflater inflater = new Inflater(true);
    inflater.setInput(deflated);//from   w  ww .  j a  va  2s  . co  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);
}

From source file:v7db.files.Compression.java

/**
 * assumes that the result buffer has exactly the needed size
 * /*from   w  w w. ja  va  2 s . c o  m*/
 * @throws DataFormatException
 */
static void inflate(byte[] data, int off, int len, byte[] out) throws DataFormatException {
    Inflater inflater = new Inflater(true);
    inflater.setInput(data, off, len);
    int size = inflater.inflate(out);
    if (size != out.length)
        throw new DataFormatException(
                "unexpected size of deflated data: " + size + " instead of " + out.length);
}

From source file:Main.java

public static String uncompress(byte[] data) throws IOException, DataFormatException {

    Inflater inflater = new Inflater();
    inflater.setInput(data);//from   w w w.j  a v  a  2  s . c  o m
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);

    byte[] buffer = new byte[1024];
    while (!inflater.finished()) {
        int count = inflater.inflate(buffer);
        outputStream.write(buffer, 0, count);
    }
    outputStream.close();
    byte[] output = outputStream.toByteArray();

    // Decode the bytes into a String
    return new String(output, 0, output.length, "UTF-8");
}

From source file:Main.java

/**
 * Decompresses the given byte[] and returns the Object.
 * //from  w  ww  .  ja v  a  2s. c o  m
 * @param <T> Type of expected Object
 * @param bytes compressed byte[]
 * @param bufferSize size of buffer to be used (in bytes)
 * 
 * @return decompressed Object
 * 
 * @throws IOException if failed to decompress
 * @throws ClassCastException if cannot cast to specified type
 */
@SuppressWarnings("unchecked")
/* Ignore Unchecked Cast Warning */
public static <T> T decompress(byte[] bytes, int bufferSize) throws IOException, ClassCastException {

    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream(bufferSize);
        Inflater inflater = new Inflater();
        inflater.setInput(bytes);

        // Decompress
        byte[] buf = new byte[bufferSize];
        while (!inflater.finished()) {
            int count = inflater.inflate(buf);
            bos.write(buf, 0, count);
        }
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));

        return (T) ois.readObject();
    } catch (Exception e) {
        throw new IOException(e);
    }
}

From source file:Main.java

public static byte[] decompress(byte[] data) throws IOException, DataFormatException {
    Inflater inflater = new Inflater();
    inflater.setInput(data);//from w w w  .  j  a  v a  2  s . c om
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    byte[] buffer = new byte[1024];
    while (!inflater.finished()) {
        int count = inflater.inflate(buffer);
        outputStream.write(buffer, 0, count);
    }
    outputStream.close();
    byte[] output = outputStream.toByteArray();
    /*
    LOG.debug("Original: " + data.length);
    LOG.debug("Compressed: " + output.length);
     */
    return output;
}