1 /***
2 * The contents of this file are subject to the Mozilla Public
3 * License Version 1.1 (the "License"); you may not use this file
4 * except in compliance with the License. You may obtain a copy of
5 * the License at http://www.mozilla.org/MPL/
6 *
7 * Software distributed under the License is distributed on an "AS
8 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9 * implied. See the License for the specific language governing
10 * rights and limitations under the License.
11 *
12 * The Original Code is pow2toolkit library.
13 *
14 * The Initial Owner of the Original Code is
15 * Dave Miller
16 *
17 * Portions created by Power Of Two S.R.L. are
18 * Copyright (C) Power Of Two S.R.L.
19 * All Rights Reserved.
20 *
21 * Contributor(s):
22 */
23
24 package com.pow2.util;
25
26
27 import java.io.ByteArrayInputStream;
28 import java.io.ByteArrayOutputStream;
29 import java.io.ObjectInputStream;
30 import java.io.ObjectOutputStream;
31
32 import org.apache.log4j.Category;
33
34
35 /***
36 * Utility class used to serialize Java objects.
37 *
38 * @author Dave Miller
39 * @author Luca Fossato
40 * @created 24 giugno 2002
41 */
42 public class Serializer
43 {
44 /*** log4j category */
45 private final static Category cat = Category.getInstance(Serializer.class.getName());
46
47
48 /*** Default private constructor; */
49 private Serializer()
50 {
51 }
52
53
54 /***
55 * Get a Java Object from its input byte stream representation.
56 *
57 * @param buf the Java object byte stream representation
58 * @return a Java Object retrieved from the input byte stream
59 * @exception Exception Description of the Exception
60 */
61 public static Object toObject(byte[] buf) throws Exception
62 {
63 ObjectInputStream ois = null;
64 Object obj = null;
65
66 try
67 {
68 ByteArrayInputStream bin = new ByteArrayInputStream(buf);
69
70 ois = new ObjectInputStream(bin);
71 obj = ois.readObject();
72 }
73 finally
74 {
75 if (ois != null)
76 ois.close();
77 }
78
79 return obj;
80 }
81
82
83 /***
84 * Serialize the input Java object and return its byte stream
85 * representation.
86 *
87 * @param obj the input Java object to serialize
88 * @return the byte stream representing the serialized Java object
89 * @exception Exception Description of the Exception
90 */
91 public static byte[] toByteArray(Object obj) throws Exception
92 {
93 ObjectOutputStream oos = null;
94 byte[] buf = null;
95
96 try
97 {
98 ByteArrayOutputStream bos = new ByteArrayOutputStream();
99
100 oos = new ObjectOutputStream(bos);
101 oos.writeObject(obj);
102 oos.flush();
103
104 buf = bos.toByteArray();
105 }
106 finally
107 {
108 if (oos != null)
109 oos.close();
110 }
111
112 return buf;
113 }
114
115
116 /***
117 * Return a deep copy of an object
118 *
119 * @param oldObj the object to copy.
120 * @return the object that's the deep-copy of the input one.
121 * @exception Exception Description of the Exception
122 */
123 public static Object deepCopy(Object oldObj) throws Exception
124 {
125 ObjectOutputStream oos = null;
126 ObjectInputStream ois = null;
127
128 try
129 {
130 ByteArrayOutputStream bos = new ByteArrayOutputStream();
131
132 oos = new ObjectOutputStream(bos);
133
134
135 oos.writeObject(oldObj);
136 oos.flush();
137
138 ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray());
139
140 ois = new ObjectInputStream(bin);
141
142
143 return ois.readObject();
144 }
145 finally
146 {
147 if (oos != null) oos.close();
148 if (ois != null) ois.close();
149 }
150 }
151 }