package com.gx.obe.server.im.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.slf4j.Logger;

public class InputStreamUtil {
	
	final static int BUFFER_SIZE = 4096;
	private static Logger LOG = org.slf4j.LoggerFactory.getLogger(InputStreamUtil.class);
	
	/**
	 * 将InputStream转换成String
	 * 
	 * @param in InputStream
	 * @return String
	 * @throws Exception
	 */
	public static String inputStreamTOString(InputStream in) throws Exception {
		
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] data = new byte[BUFFER_SIZE];
		int count = -1;
		while ((count = in.read(data, 0, BUFFER_SIZE)) != -1)
			outStream.write(data, 0, count);
		
		outStream.flush();
		outStream.close();
		data = null;
		return new String(outStream.toByteArray(), "ISO-8859-1");
	}
	
	/**
	 * 将InputStream转换成某种字符编码的String
	 * 
	 * @param in
	 * @param encoding
	 * @return
	 * @throws Exception
	 */
	public static String inputStreamTOString(InputStream in, String encoding) throws Exception {
		
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] data = new byte[BUFFER_SIZE];
		int count = -1;
		while ((count = in.read(data, 0, BUFFER_SIZE)) != -1) {
			outStream.write(data, 0, count);
		}
		outStream.flush();
		outStream.close();
		data = null;
		return new String(outStream.toByteArray(), "ISO-8859-1");
	}
	
	/**
	 * 将String转换成InputStream
	 * 
	 * @param in
	 * @return
	 * @throws Exception
	 */
	public static InputStream stringTOInputStream(String in) throws Exception {
		
		ByteArrayInputStream is = new ByteArrayInputStream(in.getBytes("ISO-8859-1"));
		return is;
	}
	
	/**
	 * 将InputStream转换成byte数组
	 * 
	 * @param in InputStream
	 * @return byte[]
	 * @throws IOException
	 */
	public static byte[] inputStreamTOByte(InputStream in) {
		
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] data = new byte[BUFFER_SIZE];
		int count = -1;
		try {
			while ((count = in.read(data, 0, BUFFER_SIZE)) != -1) {
				outStream.write(data, 0, count);
			}
			
		} catch (IOException e) {
			LOG.error(e.getMessage());
		} finally {
			if (null != outStream) {
				try {
					outStream.flush();
				} catch (IOException e) {
					LOG.error(e.getMessage());
				}
				try {
					outStream.close();
				} catch (IOException e) {
					LOG.error(e.getMessage());
				}
			}
		}
		
		data = null;
		return outStream.toByteArray();
	}
	
	public static byte[] inputStreamToByte(File file) {
		byte[] date = null;
		try {
			InputStream in = new FileInputStream(file);
			date = inputStreamTOByte(in);
			try {
				in.close();
			} catch (IOException e) {
				LOG.error(e.getMessage());
			}
		} catch (FileNotFoundException e) {}
		return date;
	}
	
	/**
	 * 将byte数组转换成InputStream
	 * 
	 * @param in
	 * @return
	 * @throws Exception
	 */
	public static InputStream byteTOInputStream(byte[] in) {
		if (null == in) {
			return null;
		}
		ByteArrayInputStream is = new ByteArrayInputStream(in);
		return is;
	}
	
	/**
	 * 将byte数组转换成String
	 * 
	 * @param in
	 * @return
	 * @throws Exception
	 */
	public static String byteTOString(byte[] in) throws Exception {
		if (null == in) {
			return null;
		}
		InputStream is = byteTOInputStream(in);
		return inputStreamTOString(is);
	}
	
	/**
	 * 将输入流写成文件
	 * 
	 * @param in
	 * @return
	 * @throws Exception
	 */
	public static boolean writeFile(InputStream is, String path) {
		boolean flag = true;
		FileOutputStream fos = null;
		if (null == is) {
			return false;
		}
		try {
			if (!new File(path).getParentFile().exists()) {
				new File(path).getParentFile().mkdirs();
			}
			byte[] buf = new byte[1024];
			fos = new FileOutputStream(path);
			while (is.read(buf) != -1) {
				fos.write(buf);
			}
			fos.flush();
		} catch (FileNotFoundException e) {
			LOG.error(e.getMessage());
			flag = false;
		} catch (IOException e) {
			LOG.error(e.getMessage());
			flag = false;
		} finally {
			if (null != fos) {
				try {
					fos.close();
				} catch (IOException e) {
					LOG.error(e.getMessage());
				}
			}
		}
		return flag;
		
	}
	
	/**
	 * 将字节流写成文件
	 * 
	 * @param in
	 * @return
	 * @throws Exception
	 */
	public static boolean writeFile(byte[] bytes, String path) {
		boolean flag = true;
		FileOutputStream fos = null;
		if (flag) {
			try {
				if (!new File(path).getParentFile().exists()) {
					new File(path).getParentFile().mkdirs();
				}
				fos = new FileOutputStream(path);
				fos.write(bytes);
				fos.flush();
			} catch (FileNotFoundException e) {
				LOG.error(e.getMessage());
			} catch (IOException e) {
				LOG.error(e.getMessage());
			} finally {
				if (null != fos) {
					try {
						fos.close();
					} catch (IOException e) {}
				}
			}
		} else {
			InputStream is = null;
			try {
				if (!new File(path).getParentFile().exists()) {
					new File(path).getParentFile().mkdirs();
				}
				is = byteTOInputStream(bytes);
				if (null == is) {
					return false;
				}
				byte[] buf = new byte[1024];
				fos = new FileOutputStream(path);
				while (is.read(buf) != -1) {
					fos.write(buf);
				}
			} catch (FileNotFoundException e) {
				flag = false;
				LOG.error(e.getMessage());
			} catch (IOException e) {
				LOG.error(e.getMessage());
				flag = false;
			} finally {
				if (null != is) {
					try {
						is.close();
					} catch (IOException e) {}
				}
				if (null != fos) {
					try {
						fos.flush();
					} catch (IOException e1) {}
					try {
						fos.close();
					} catch (IOException e) {}
				}
			}
		}
		return flag;
		
	}
}