Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;

public class Main {

    public static byte[] zlibDecompress(byte[] data) {
        return zlibDecompress(data, 0, data.length);
    }

    public static byte[] zlibDecompress(byte[] data, int offset, int length) {
        byte[] output = null;

        Inflater decompresser = new Inflater();
        decompresser.reset();
        try {
            decompresser.setInput(data, offset, length);
        } catch (Exception e) {
            return null;
        }

        ByteArrayOutputStream o = new ByteArrayOutputStream(data.length);
        try {
            byte[] buf = new byte[1024];
            while (!decompresser.finished()) {
                int i = decompresser.inflate(buf);
                o.write(buf, 0, i);
            }
            output = o.toByteArray();
        } catch (Exception e) {
            output = data;
            e.printStackTrace();
        } finally {
            try {
                o.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        decompresser.end();
        return output;
    }

    public static byte[] zlibDecompress(InputStream is) {
        if (null == is)
            return null;
        InflaterInputStream iis = new InflaterInputStream(is);
        ByteArrayOutputStream o = new ByteArrayOutputStream(1024);
        try {
            int i = 1024;
            byte[] buf = new byte[i];

            while ((i = iis.read(buf, 0, i)) > 0) {
                o.write(buf, 0, i);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return o.toByteArray();
    }
}