Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import java.util.zip.Deflater;

public class Main {
    public final static int BUFFER_SIZE = 4092;

    public static byte[] compress(byte[] data, int level) throws IOException {
        if (data == null || data.length == 0) {
            return data;
        }
        ByteArrayOutputStream bout = new ByteArrayOutputStream(data.length);
        Deflater deflater = new Deflater();
        deflater.setLevel(level);
        deflater.setInput(data);
        deflater.finish();
        byte[] buf = new byte[BUFFER_SIZE];
        while (!deflater.finished()) {
            int count = deflater.deflate(buf);
            bout.write(buf, 0, count);
        }
        deflater.end();
        bout.close();
        return bout.toByteArray();
    }
}