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.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;

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

    public static byte[] decompress(byte[] bytes) throws IOException {

        GZIPInputStream gzip = null;
        ByteArrayOutputStream baos = null;
        try {
            baos = new ByteArrayOutputStream();

            gzip = new GZIPInputStream(new ByteArrayInputStream(bytes));

            int len = 0;
            byte data[] = new byte[BUFFER_SIZE];

            while ((len = gzip.read(data, 0, BUFFER_SIZE)) != -1) {
                baos.write(data, 0, len);
            }

            gzip.close();
            baos.flush();

            return baos.toByteArray();

        } finally {
            if (gzip != null) {
                gzip.close();
            }
            if (baos != null) {
                baos.close();
            }
        }
    }
}