package com.uas.search.console.b2b.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.NotSerializableException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * 对象辅助类
 * 
 * @author sunyj
 * @since 2017年3月24日 上午10:28:40
 */
public class ObjectUtil {

	/**
	 * 深克隆对象
	 * 
	 * @param t
	 *            将克隆的对象，需实现Serializable
	 * @return
	 * @throws IOException
	 * @throws ClassNotFoundException
	 */
	@SuppressWarnings("unchecked")
	public static <T> T clone(T t) throws IOException, ClassNotFoundException, NotSerializableException {
		if (t == null) {
			return null;
		}
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		new ObjectOutputStream(out).writeObject(t);
		ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(out.toByteArray()));
		return (T) in.readObject();
	}

}
