01: /**
02: * LibreSource
03: * Copyright (C) 2004-2008 Artenum SARL / INRIA
04: * http://www.libresource.org - contact@artenum.com
05: *
06: * This file is part of the LibreSource software,
07: * which can be used and distributed under license conditions.
08: * The license conditions are provided in the LICENSE.TXT file
09: * at the root path of the packaging that enclose this file.
10: * More information can be found at
11: * - http://dev.libresource.org/home/license
12: *
13: * Initial authors :
14: *
15: * Guillaume Bort / INRIA
16: * Francois Charoy / Universite Nancy 2
17: * Julien Forest / Artenum
18: * Claude Godart / Universite Henry Poincare
19: * Florent Jouille / INRIA
20: * Sebastien Jourdain / INRIA / Artenum
21: * Yves Lerumeur / Artenum
22: * Pascal Molli / Universite Henry Poincare
23: * Gerald Oster / INRIA
24: * Mariarosa Penzi / Artenum
25: * Gerard Sookahet / Artenum
26: * Raphael Tani / INRIA
27: *
28: * Contributors :
29: *
30: * Stephane Bagnier / Artenum
31: * Amadou Dia / Artenum-IUP Blois
32: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33: */package org.libresource.so6.core.engine.util;
34:
35: import java.io.ByteArrayInputStream;
36: import java.io.ByteArrayOutputStream;
37: import java.io.ObjectInputStream;
38: import java.io.ObjectOutputStream;
39:
40: public class ObjectCloner {
41: // so that nobody can accidentally create an ObjectCloner object
42: private ObjectCloner() {
43: }
44:
45: // returns a deep copy of an object
46: static public Object deepCopy(Object oldObj) throws Exception {
47: ObjectOutputStream oos = null;
48: ObjectInputStream ois = null;
49:
50: try {
51: ByteArrayOutputStream bos = new ByteArrayOutputStream(); // A
52: oos = new ObjectOutputStream(bos); // B
53:
54: // serialize and pass the object
55: oos.writeObject(oldObj); // C
56: oos.flush(); // D
57:
58: ByteArrayInputStream bin = new ByteArrayInputStream(bos
59: .toByteArray()); // E
60: ois = new ObjectInputStream(bin); // F
61:
62: // return the new object
63: return ois.readObject(); // G
64: } finally {
65: oos.close();
66: ois.close();
67: }
68: }
69: }
|