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

public static byte[] decompress(byte[] data) throws IOException {
    Inflater inflater = new Inflater();
    inflater.setInput(data);
    inflater.finished();// www . j ava  2 s.c  o m

    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  w w .  j  av  a  2  s .  c  om
        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:Main.java

public static byte[] decompressZLIB(byte[] input) throws IOException {
    Inflater decompressor = new Inflater();
    decompressor.setInput(input);
    // 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 {/*  www  . ja v a2 s. c o  m*/
            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:Main.java

public static byte[] decompress(byte[] data) throws IOException, DataFormatException {
    Inflater decompresser = new Inflater();
    decompresser.setInput(data);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    byte[] buffer = new byte[1024];
    while (!decompresser.finished()) {
        int count = decompresser.inflate(buffer);
        outputStream.write(buffer, 0, count);
    }/*  w w  w .ja  v  a  2  s.  c o  m*/
    decompresser.end();
    outputStream.close();
    return outputStream.toByteArray();
}

From source file:Main.java

public final static byte[] decompress(byte[] input) throws IOException {
    if (input == null || input.length == 0) {
        return input;
    }//from   w  ww  .j ava2  s  .c o  m
    Inflater inflator = new Inflater();
    inflator.setInput(input);
    ByteArrayOutputStream bin = new ByteArrayOutputStream(input.length);
    byte[] buf = new byte[BUFFER_SIZE];
    try {
        while (true) {
            int count = inflator.inflate(buf);
            if (count > 0) {
                bin.write(buf, 0, count);
            } else if (count == 0 && inflator.finished()) {
                break;
            } else {
                throw new IOException("bad zip data, size:" + input.length);
            }
        }
    } catch (DataFormatException t) {
        throw new IOException(t);
    } finally {
        inflator.end();
    }
    return bin.toByteArray();
}

From source file:Main.java

private static byte[] uncompressBytesInflateDeflate(byte[] inBytes) throws IOException {
    Inflater inflater = new Inflater();
    inflater.setInput(inBytes);
    ByteArrayOutputStream bos = new ByteArrayOutputStream(inBytes.length);
    byte[] buffer = new byte[1024 * 8];
    while (!inflater.finished()) {
        int count;
        try {//from w  w w .  j ava2  s .  c o  m
            count = inflater.inflate(buffer);
        } catch (DataFormatException e) {
            throw new IOException(e);
        }
        bos.write(buffer, 0, count);
    }
    byte[] output = bos.toByteArray();
    return output;
}

From source file:Main.java

public static byte[] zipDecompress(byte[] input) throws IOException {
    Inflater inflator = new Inflater();
    inflator.setInput(input);
    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
    byte[] buf = new byte[1024];
    try {/*  www .java2  s  .c om*/
        while (true) {
            int count = inflator.inflate(buf);
            if (count > 0) {
                bos.write(buf, 0, count);
            } else if (count == 0 && inflator.finished()) {
                break;
            } else {
                throw new RuntimeException("bad zip data, size:" + input.length);
            }
        }
    } catch (DataFormatException t) {
        throw new RuntimeException(t);
    } finally {
        inflator.end();
    }
    return bos.toByteArray();
}

From source file:Main.java

/**
 * Decompress the zlib-compressed bytes and return an array of decompressed
 * bytes//www.j  av a 2  s.  co  m
 * 
 */
public static byte[] decompress(byte compressedBytes[]) throws DataFormatException {

    Inflater decompresser = new Inflater();

    decompresser.setInput(compressedBytes);

    byte[] resultBuffer = new byte[compressedBytes.length * 2];
    byte[] resultTotal = new byte[0];

    int resultLength = decompresser.inflate(resultBuffer);

    while (resultLength > 0) {
        byte previousResult[] = resultTotal;
        resultTotal = new byte[resultTotal.length + resultLength];
        System.arraycopy(previousResult, 0, resultTotal, 0, previousResult.length);
        System.arraycopy(resultBuffer, 0, resultTotal, previousResult.length, resultLength);
        resultLength = decompresser.inflate(resultBuffer);
    }

    decompresser.end();

    return resultTotal;
}

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

    ByteArrayOutputStream os = new ByteArrayOutputStream(data.length);
    try {/*from w w  w. j a  v  a  2  s .  co m*/
        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

/**
 * Decompresses the given byte[] and returns the Object.
 * //  w w w .ja va2  s  .  co 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);
    }
}