/*
 * Decompiled with CFR 0.152.
 */
package com.hst.common.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterInputStream;

public class ZlibCompress {
    public static byte[] Compression(String str) throws IOException {
        byte[] bytes = null;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        DeflaterOutputStream zlib = new DeflaterOutputStream(out);
        try {
            try {
                zlib.write(str.getBytes());
                zlib.flush();
                zlib.close();
                bytes = out.toByteArray();
                out.close();
            }
            catch (Exception e) {
                e.printStackTrace();
                throw new IOException(e);
            }
        }
        finally {
            try {
                zlib.close();
                out.close();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
        return bytes;
    }

    public static byte[] CompressionByte(byte[] reqByte) throws IOException {
        byte[] bytes = null;
        if (reqByte == null || reqByte.length <= 0) {
            return bytes;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        DeflaterOutputStream zlib = new DeflaterOutputStream(out);
        try {
            try {
                zlib.write(reqByte);
                zlib.flush();
                zlib.close();
                bytes = out.toByteArray();
                out.close();
            }
            catch (Exception e) {
                e.printStackTrace();
                throw new IOException(e);
            }
        }
        finally {
            try {
                zlib.close();
                out.close();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
        return bytes;
    }

    public static String Decompression(byte[] bytes) throws IOException {
        if (bytes == null || bytes.length <= 0) {
            return "";
        }
        String str = "";
        ByteArrayInputStream ine = new ByteArrayInputStream(bytes);
        InflaterInputStream zlibz = new InflaterInputStream(ine);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            try {
                int n;
                byte[] buffer = new byte[1024];
                while ((n = zlibz.read(buffer)) >= 0) {
                    out.write(buffer, 0, n);
                }
                zlibz.close();
                ((InputStream)ine).close();
                str = out.toString();
            }
            catch (Exception e) {
                e.printStackTrace();
                throw new IOException(e);
            }
        }
        finally {
            try {
                zlibz.close();
                ((InputStream)ine).close();
                out.close();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
        return str;
    }

    public static byte[] DecompressionByte(byte[] bytes) throws IOException {
        byte[] resByte = null;
        if (bytes == null || bytes.length <= 0) {
            return resByte;
        }
        ByteArrayInputStream ine = new ByteArrayInputStream(bytes);
        InflaterInputStream zlibz = new InflaterInputStream(ine);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            try {
                int n;
                byte[] buffer = new byte[1024];
                while ((n = zlibz.read(buffer)) >= 0) {
                    out.write(buffer, 0, n);
                }
                zlibz.close();
                ((InputStream)ine).close();
                resByte = out.toByteArray();
            }
            catch (Exception e) {
                e.printStackTrace();
                throw new IOException(e);
            }
        }
        finally {
            try {
                zlibz.close();
                ((InputStream)ine).close();
                out.close();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
        return resByte;
    }
}
