1 package org.apache.ojb.otm.copy;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 import java.io.*;
19 import org.apache.ojb.broker.PersistenceBroker;
20
21
22
23
24
25
26
27 public final class SerializeObjectCopyStrategy implements ObjectCopyStrategy
28 {
29
30
31
32
33
34 public Object copy(final Object obj, PersistenceBroker broker)
35 throws ObjectCopyException
36 {
37 ObjectOutputStream oos = null;
38 ObjectInputStream ois = null;
39 try
40 {
41 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
42 oos = new ObjectOutputStream(bos);
43
44 oos.writeObject(obj);
45 oos.flush();
46 final ByteArrayInputStream bin =
47 new ByteArrayInputStream(bos.toByteArray());
48 ois = new ObjectInputStream(bin);
49
50 return ois.readObject();
51 }
52 catch (Exception e)
53 {
54 throw new ObjectCopyException(e);
55 }
56 finally
57 {
58 try
59 {
60 if (oos != null)
61 {
62 oos.close();
63 }
64 if (ois != null)
65 {
66 ois.close();
67 }
68 }
69 catch (IOException ioe)
70 {
71
72 }
73 }
74 }
75 }