手动 序列化 clone

package com.beetle.framework.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Random;
import java.io.*;

public class OtherUtil {

	/**
	 * 对象深度克隆
	 * 
	 * @param originObj
	 * @return
	 */
	public final static Object objectClone(Object originObj) {
		ByteArrayOutputStream bao = null;
		ByteArrayInputStream bai = null;
		ObjectOutputStream oos = null;
		ObjectInputStream ois = null;
		try {
			bao = new ByteArrayOutputStream();
			oos = new ObjectOutputStream(bao);
			oos.writeObject(originObj);
			oos.flush();
			oos.close();
			bai = new ByteArrayInputStream(bao.toByteArray());
			ois = new ObjectInputStream(bai);
			Object obj = ois.readObject();
			ois.close();
			oos = null;
			ois = null;
			return obj;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			try {
				if (bao != null) {
					bao.close();
					bao = null;
				}
				if (bai != null) {
					bai.close();
					bai = null;
				}
			} catch (IOException e) {
			}
		}
	}
}

你可能感兴趣的:(java,.net)