KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > helper > SerializationHelper


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2005, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.helper;
23
24 import oracle.toplink.essentials.exceptions.ValidationException;
25 import java.io.ByteArrayInputStream JavaDoc;
26 import java.io.ByteArrayOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.ObjectInputStream JavaDoc;
30 import java.io.ObjectOutputStream JavaDoc;
31 import java.io.OutputStream JavaDoc;
32 import java.io.Serializable JavaDoc;
33
34 /**
35  * <p>Provide common functionalities for serialization of object.
36  * </p>
37  *
38  * <p>This class throws exceptions for invalid <code>null</code> inputs.
39  * Each method documents its behaviour in more detail.</p>
40  *
41  * @author Steven Vo
42  * @since OracleAS 10.0.3
43  */

44 public class SerializationHelper {
45
46     /**
47      * <p>Deep clone a Serializable object using serialization.
48      * @param the serializable object
49      * @return the deep cloned object
50      * @throws IOException, ClassNotFoundException
51      */

52     public static Object JavaDoc clone(Serializable JavaDoc object) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
53         return deserialize(serialize(object));
54     }
55
56     /**
57      * Serialize the object to an OutputStream
58      *
59      * @param obj the object to serialize to bytes
60      * @param outputStream the stream to write to, can not be null
61      * @throws IOException
62      */

63     public static void serialize(Serializable JavaDoc obj, OutputStream JavaDoc outputStream) throws IOException JavaDoc {
64         if (outputStream == null) {
65             throw ValidationException.invalidNullMethodArguments();
66         }
67         ObjectOutputStream JavaDoc outStream = null;
68
69         try {
70             // stream closed in the finally
71
outStream = new ObjectOutputStream JavaDoc(outputStream);
72             outStream.writeObject(obj);
73         } finally {
74             try {
75                 if (outStream != null) {
76                     outStream.close();
77                 }
78             } catch (IOException JavaDoc ex) {
79                 // ignore;
80
}
81         }
82     }
83
84     /**
85      * Serialize the object to a byte array
86      *
87      * @param obj the object to serialize to bytes
88      * @return a byte[] of the obj
89      * @throws IOException
90      */

91     public static byte[] serialize(Serializable JavaDoc obj) throws IOException JavaDoc {
92         ByteArrayOutputStream JavaDoc outStream = new ByteArrayOutputStream JavaDoc(512);
93         serialize(obj, outStream);
94         return outStream.toByteArray();
95     }
96
97     /**
98      * Deserialze an object from an InputStream
99      *
100      * @param inputStream the serialized object input stream, must not be null
101      * @return the deserialized object
102      * @throws IOException, ClassNotFoundException
103      */

104     public static Object JavaDoc deserialize(InputStream JavaDoc inputStream) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
105         if (inputStream == null) {
106             throw new IllegalArgumentException JavaDoc("The inputStream argument cannot be null");
107         }
108         ObjectInputStream JavaDoc inStream = null;
109         try {
110             // stream closed in the finally
111
inStream = new ObjectInputStream JavaDoc(inputStream);
112             return inStream.readObject();
113
114         } finally {
115             try {
116                 if (inStream != null) {
117                     inStream.close();
118                 }
119             } catch (IOException JavaDoc ex) {
120                 // ignore
121
}
122         }
123     }
124
125     /**
126      * Deserialize an object from a byte array
127      *
128      * @param objectBytes the serialized object, can not be null
129      * @return the deserialized object
130      * @throws IOException, ClassNotFoundException
131      */

132     public static Object JavaDoc deserialize(byte[] objectBytes) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
133         if (objectBytes == null) {
134             throw ValidationException.invalidNullMethodArguments();
135         }
136         ByteArrayInputStream JavaDoc inStream = new ByteArrayInputStream JavaDoc(objectBytes);
137         return deserialize(inStream);
138     }
139 }
140
Popular Tags