package com.eactive.eai.inbound.filter; 

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class ZIPUtil { 
	
    public static byte[] compress(byte[] message){
    	byte[] obj = null;
		ByteArrayOutputStream os = null;
		GZIPOutputStream gos = null;
		BufferedOutputStream bos = null;
		try{
			os = new ByteArrayOutputStream();
			gos = new GZIPOutputStream(os);
			bos = new BufferedOutputStream(gos);
			bos.write(message);
			
			bos.close();
			gos.close();
			os.close();
			
			obj = os.toByteArray();
			
		}catch(Exception e){
			obj = message;
		}finally{
			if (bos!=null) try {bos.close();} catch (IOException e) {}
			if (gos!=null) try {gos.close();} catch (IOException e) {}
			if (os!=null) try {os.close();} catch (IOException e) {}
		}
    	return obj;
    }
	

    public static byte[] decompress(byte[] message){
    	byte[] obj = null;
		ByteArrayInputStream is = null;
		GZIPInputStream gis = null;
		BufferedInputStream bis = null;
		ByteArrayOutputStream os = null;

		try{
			is = new ByteArrayInputStream(message);
			gis = new GZIPInputStream(is);
			bis = new BufferedInputStream(gis);
			os = new ByteArrayOutputStream();
			 
			byte[] buffer = new byte[1024];
			 
			int length;
			while((length = bis.read(buffer)) > 0) {
				os.write(buffer, 0, length);
			}
			os.close();
			bis.close();
			gis.close();
			is.close();
			obj = os.toByteArray();
		}catch(Exception e){
			obj = message;
		}finally {
			if (os!=null) try {os.close();} catch (IOException e) {}
			if (bis!=null) try {bis.close();} catch (IOException e) {}
			if (gis!=null) try {gis.close();} catch (IOException e) {}
			if (is!=null) try {is.close();} catch (IOException e) {}

        }
    	
    	return obj;
    }
    
//    public static void main(String[] args) throws Exception{
//    	//전체
//    	//byte[] message = "00001048 000DEVCHAP_0120120509140619000002002SR10                                       EAI               000000                                      000IFWFW555555IFWCD950112TSTSYNC             A2   S Y                                       0000        0000  0000000000000000000000000000000                              KORN000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000                              0000000000000000000000000000000000000000000000000000000000000000000000000000N0000000000000000000000000000000000000000000000000000000000000000000000000000000000000FILLER                                                                                                                                                                                                                                IBS                                     IO00000036IFW                       0000100010@@".getBytes();
//    	//150byte 이후만
//    	byte[] message = "000IFWFW555555IFWCD950112TSTSYNC             A2   S Y                                       0000        0000  0000000000000000000000000000000                              KORN000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000                              0000000000000000000000000000000000000000000000000000000000000000000000000000N0000000000000000000000000000000000000000000000000000000000000000000000000000000000000FILLER                                                                                                                                                                                                                                IBS                                     IO00000036IFW                       0000100010@@".getBytes();
//    	byte[] com = (byte[]) ZIPUtil.compress(message);
//    	System.out.println("compress="+ new String(com));
//    	byte[] decom= (byte[]) ZIPUtil.decompress(com);
//    	System.out.println("decompress="+new String(decom));
//    	
//    	;
//    	
//    }
    
} 
