/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.glaf.core.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;

/**
 * ZLibѹ
 * 
 */
public class ZlibUtils {
	private static final int BUFFER = 8192;

	/**
	 * ѹֽ
	 * 
	 * @param data
	 *            ѹ
	 * @return byte[] ѹ
	 */
	public static byte[] compress(byte[] data) {
		byte[] output = null;
		Deflater compresser = new Deflater();
		compresser.reset();
		compresser.setInput(data);
		compresser.finish();
		ByteArrayOutputStream baos = null;
		try {
			baos = new ByteArrayOutputStream(data.length);
			byte[] buff = new byte[BUFFER];
			while (!compresser.finished()) {
				int i = compresser.deflate(buff);
				baos.write(buff, 0, i);
			}
			output = baos.toByteArray();
			compresser.end();
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new RuntimeException(ex);
		} finally {
			try {
				baos.close();
			} catch (IOException e) {
			}
		}
		return output;
	}

	/**
	 * ѹֽ
	 * 
	 * @param data
	 *            ѹ
	 * 
	 * @param os
	 *            
	 */
	public static void compress(byte[] data, OutputStream os) {
		DeflaterOutputStream dos = null;
		try {
			dos = new DeflaterOutputStream(os);
			dos.write(data, 0, data.length);
			dos.finish();
			dos.flush();
		} catch (IOException ex) {
			ex.printStackTrace();
			throw new RuntimeException(ex);
		} finally {
			IOUtils.closeStream(dos);
		}
	}

	/**
	 * ѹ
	 * 
	 * @param data
	 *            Ѿѹ
	 * @return byte[] ѹ
	 */
	public static byte[] uncompress(byte[] data) {
		byte[] output = null;
		Inflater decompresser = new Inflater();
		decompresser.reset();
		decompresser.setInput(data);
		ByteArrayOutputStream baos = null;
		try {
			baos = new ByteArrayOutputStream(data.length);
			byte[] buf = new byte[BUFFER];
			while (!decompresser.finished()) {
				int i = decompresser.inflate(buf);
				baos.write(buf, 0, i);
			}
			output = baos.toByteArray();
			decompresser.end();
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new RuntimeException(ex);
		} finally {
			try {
				baos.close();
			} catch (IOException e) {
			}
		}
		return output;
	}

	/**
	 * ѹ
	 * 
	 * @param inputStream
	 *            Ѿѹ
	 * @return byte[] ѹ
	 */
	public static byte[] uncompress(InputStream inputStream) {
		byte[] bytes = null;
		InflaterInputStream iis = null;
		ByteArrayOutputStream baos = null;
		try {
			iis = new InflaterInputStream(inputStream);
			baos = new ByteArrayOutputStream(BUFFER);
			int i = BUFFER;
			byte[] buff = new byte[i];
			while ((i = iis.read(buff, 0, i)) > 0) {
				baos.write(buff, 0, i);
			}
			bytes = baos.toByteArray();
		} catch (IOException ex) {
			ex.printStackTrace();
			throw new RuntimeException(ex);
		} finally {
			IOUtils.closeStream(iis);
			IOUtils.closeStream(baos);
		}
		return bytes;
	}

	public static void main(String[] args) {
		String filename = "user.xml";
		if (args != null && args.length == 1) {
			filename = args[0];
		}
		byte[] bytes = FileUtils.getBytes(filename);
		byte[] input = null;
		byte[] zipBytes = null;
		try {
			zipBytes = ZlibUtils.compress(bytes);
			FileUtils.save("output", zipBytes);
			input = FileUtils.getBytes("output");
			bytes = ZlibUtils.uncompress(input);
			FileUtils.save("input", bytes);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

@@ -1830,32 +1830,56 @@
 ;%0A%09%09%7D finally %7B%0A
+%09%09%09if (baos != null) %7B%0A%09
 %09%09%09try %7B%0A%09%09%09%09bao
@@ -1867,32 +1867,33 @@
 %7B%0A%09%09%09%09try %7B%0A%09%09%09%09
+%09
 baos.close();%0A%09%09
@@ -1882,32 +1882,33 @@
 %09%09baos.close();%0A
+%09
 %09%09%09%7D catch (IOEx
@@ -1912,32 +1912,38 @@
 OException e) %7B%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D%0A%09%09retur
@@ -3037,16 +3037,40 @@
 nally %7B%0A
+%09%09%09if (baos != null) %7B%0A%09
 %09%09%09try %7B
@@ -3070,24 +3070,25 @@
 %09%09try %7B%0A%09%09%09%09
+%09
 baos.close()
@@ -3089,16 +3089,17 @@
 lose();%0A
+%09
 %09%09%09%7D cat
@@ -3119,16 +3119,22 @@
 on e) %7B%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D

update ZlibUtils