Example usage for java.util.zip Deflater setInput

List of usage examples for java.util.zip Deflater setInput

Introduction

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

Prototype

public void setInput(byte[] input, int off, int len) 

Source Link

Document

Sets input data for compression.

Usage

From source file:Main.java

public static void main(String[] args) throws IOException {
    Deflater def = new Deflater();
    byte[] input = new byte[1024];
    byte[] output = new byte[1024];

    FileInputStream fin = new FileInputStream("a.dat");
    FileOutputStream fout = new FileOutputStream("b.dat");

    int numRead = fin.read(input);

    def.setInput(input, 0, numRead);

    while (!def.needsInput()) {
        int numCompressedBytes = def.deflate(output, 0, output.length);
        if (numCompressedBytes > 0) {
            fout.write(output, 0, numCompressedBytes);
        }// w w w  .  j ava2s  . c o m
    }
    def.finish();
    fin.close();
    fout.flush();
    fout.close();
    def.reset();
}

From source file:Main.java

public static byte[] compressInZlib(byte[] originalData, int offset, int length) throws IOException {

    Deflater compresser = new Deflater();
    compresser.setInput(originalData, offset, length);
    compresser.finish();/*from www. j  ava 2s . co  m*/

    ByteArrayOutputStream bos = new ByteArrayOutputStream(length);

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

    byte[] compressData = bos.toByteArray();
    bos.close();

    return compressData;
}

From source file:v7db.files.Compression.java

/**
 * @return 0, if the "deflated" data fills the whole output array
 *//*  w  w w  .  jav  a2s . co  m*/
static int deflate(byte[] data, int off, int len, byte[] out) {
    Deflater deflater = new Deflater(BEST_COMPRESSION, true);
    deflater.setInput(data, off, len);
    deflater.finish();
    int size = deflater.deflate(out);
    if (size == 0 || size == out.length)
        return 0;
    return size;
}