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.DataFormatException;

import java.util.zip.Inflater;

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

    public final static byte[] decompress(byte[] input) throws IOException {
        if (input == null || input.length == 0) {
            return input;
        }
        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();
    }
}