2008년 08월 10일
deep copy
http://www.javaworld.com/javaworld/javatips/jw-javatip76.html?page=1
정말 많은 것을 모르고 지냈구나 하는 생각이 든다.
지금 프로젝트 소스를 보다 보니, 이 소스를 사용하는거 같아서.. 시간 내서.. 다음에 공부 해야 할거 같다.
이런 것도 모르고.. 어디 가서 절대 자바 한다고 하지 말자.
import java.io.*;
import java.util.*;
import java.awt.*;
public class ObjectCloner
{
// so that nobody can accidentally create an ObjectCloner object
private ObjectCloner(){}
// returns a deep copy of an object
static public Object deepCopy(Object oldObj) throws Exception
{
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
try
{
ByteArrayOutputStream bos =
new ByteArrayOutputStream(); // A
oos = new ObjectOutputStream(bos); // B
// serialize and pass the object
oos.writeObject(oldObj); // C
oos.flush(); // D
ByteArrayInputStream bin =
new ByteArrayInputStream(bos.toByteArray()); // E
ois = new ObjectInputStream(bin); // F
// return the new object
return ois.readObject(); // G
}
catch(Exception e)
{
System.out.println("Exception in ObjectCloner = " + e);
throw(e);
}
finally
{
oos.close();
ois.close();
}
}
}
정말 많은 것을 모르고 지냈구나 하는 생각이 든다.
지금 프로젝트 소스를 보다 보니, 이 소스를 사용하는거 같아서.. 시간 내서.. 다음에 공부 해야 할거 같다.
이런 것도 모르고.. 어디 가서 절대 자바 한다고 하지 말자.
import java.io.*;
import java.util.*;
import java.awt.*;
public class ObjectCloner
{
// so that nobody can accidentally create an ObjectCloner object
private ObjectCloner(){}
// returns a deep copy of an object
static public Object deepCopy(Object oldObj) throws Exception
{
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
try
{
ByteArrayOutputStream bos =
new ByteArrayOutputStream(); // A
oos = new ObjectOutputStream(bos); // B
// serialize and pass the object
oos.writeObject(oldObj); // C
oos.flush(); // D
ByteArrayInputStream bin =
new ByteArrayInputStream(bos.toByteArray()); // E
ois = new ObjectInputStream(bin); // F
// return the new object
return ois.readObject(); // G
}
catch(Exception e)
{
System.out.println("Exception in ObjectCloner = " + e);
throw(e);
}
finally
{
oos.close();
ois.close();
}
}
}
# by | 2008/08/10 23:44 | java | 트랙백 | 덧글(4)





☞ 내 이글루에 이 글과 관련된 글 쓰기 (트랙백 보내기) [도움말]
<T extends Serializable>
org.apache.commons.lang.SerializationUtils.clone(t)
가 있습니다. 진짜로 쓰실땐 이걸 쓰는것이 나으실듯..
家に戻ったら、確認してみます
좋은 댓글 주셔서 감사 합니다.
이름을 몰라 아쉽네요.
저도 회사였어서 로그인 안했었네요..