KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > SerializationUtils


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.lang;
17
18 import java.io.ByteArrayInputStream JavaDoc;
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.ObjectInputStream JavaDoc;
23 import java.io.ObjectOutputStream JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.io.Serializable JavaDoc;
26
27 /**
28  * <p>Assists with the serialization process and performs additional functionality based
29  * on serialization.</p>
30  * <p>
31  * <ul>
32  * <li>Deep clone using serialization
33  * <li>Serialize managing finally and IOException
34  * <li>Deserialize managing finally and IOException
35  * </ul>
36  *
37  * <p>This class throws exceptions for invalid <code>null</code> inputs.
38  * Each method documents its behaviour in more detail.</p>
39  *
40  * @author <a HREF="mailto:nissim@nksystems.com">Nissim Karpenstein</a>
41  * @author <a HREF="mailto:janekdb@yahoo.co.uk">Janek Bogucki</a>
42  * @author <a HREF="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
43  * @author Stephen Colebourne
44  * @author Jeff Varszegi
45  * @author Gary Gregory
46  * @since 1.0
47  * @version $Id: SerializationUtils.java 161243 2005-04-14 04:30:28Z ggregory $
48  */

49 public class SerializationUtils {
50     
51     /**
52      * <p>SerializationUtils instances should NOT be constructed in standard programming.
53      * Instead, the class should be used as <code>SerializationUtils.clone(object)</code>.</p>
54      *
55      * <p>This constructor is public to permit tools that require a JavaBean instance
56      * to operate.</p>
57      * @since 2.0
58      */

59     public SerializationUtils() {
60         super();
61     }
62
63     // Clone
64
//-----------------------------------------------------------------------
65
/**
66      * <p>Deep clone an <code>Object</code> using serialization.</p>
67      *
68      * <p>This is many times slower than writing clone methods by hand
69      * on all objects in your object graph. However, for complex object
70      * graphs, or for those that don't support deep cloning this can
71      * be a simple alternative implementation. Of course all the objects
72      * must be <code>Serializable</code>.</p>
73      *
74      * @param object the <code>Serializable</code> object to clone
75      * @return the cloned object
76      * @throws SerializationException (runtime) if the serialization fails
77      */

78     public static Object JavaDoc clone(Serializable JavaDoc object) {
79         return deserialize(serialize(object));
80     }
81     
82     // Serialize
83
//-----------------------------------------------------------------------
84
/**
85      * <p>Serializes an <code>Object</code> to the specified stream.</p>
86      *
87      * <p>The stream will be closed once the object is written.
88      * This avoids the need for a finally clause, and maybe also exception
89      * handling, in the application code.</p>
90      *
91      * <p>The stream passed in is not buffered internally within this method.
92      * This is the responsibility of your application if desired.</p>
93      *
94      * @param obj the object to serialize to bytes, may be null
95      * @param outputStream the stream to write to, must not be null
96      * @throws IllegalArgumentException if <code>outputStream</code> is <code>null</code>
97      * @throws SerializationException (runtime) if the serialization fails
98      */

99     public static void serialize(Serializable JavaDoc obj, OutputStream JavaDoc outputStream) {
100         if (outputStream == null) {
101             throw new IllegalArgumentException JavaDoc("The OutputStream must not be null");
102         }
103         ObjectOutputStream JavaDoc out = null;
104         try {
105             // stream closed in the finally
106
out = new ObjectOutputStream JavaDoc(outputStream);
107             out.writeObject(obj);
108             
109         } catch (IOException JavaDoc ex) {
110             throw new SerializationException(ex);
111         } finally {
112             try {
113                 if (out != null) {
114                     out.close();
115                 }
116             } catch (IOException JavaDoc ex) {
117                 // ignore;
118
}
119         }
120     }
121
122     /**
123      * <p>Serializes an <code>Object</code> to a byte array for
124      * storage/serialization.</p>
125      *
126      * @param obj the object to serialize to bytes
127      * @return a byte[] with the converted Serializable
128      * @throws SerializationException (runtime) if the serialization fails
129      */

130     public static byte[] serialize(Serializable JavaDoc obj) {
131         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc(512);
132         serialize(obj, baos);
133         return baos.toByteArray();
134     }
135
136     // Deserialize
137
//-----------------------------------------------------------------------
138
/**
139      * <p>Deserializes an <code>Object</code> from the specified stream.</p>
140      *
141      * <p>The stream will be closed once the object is written. This
142      * avoids the need for a finally clause, and maybe also exception
143      * handling, in the application code.</p>
144      *
145      * <p>The stream passed in is not buffered internally within this method.
146      * This is the responsibility of your application if desired.</p>
147      *
148      * @param inputStream the serialized object input stream, must not be null
149      * @return the deserialized object
150      * @throws IllegalArgumentException if <code>inputStream</code> is <code>null</code>
151      * @throws SerializationException (runtime) if the serialization fails
152      */

153     public static Object JavaDoc deserialize(InputStream JavaDoc inputStream) {
154         if (inputStream == null) {
155             throw new IllegalArgumentException JavaDoc("The InputStream must not be null");
156         }
157         ObjectInputStream JavaDoc in = null;
158         try {
159             // stream closed in the finally
160
in = new ObjectInputStream JavaDoc(inputStream);
161             return in.readObject();
162             
163         } catch (ClassNotFoundException JavaDoc ex) {
164             throw new SerializationException(ex);
165         } catch (IOException JavaDoc ex) {
166             throw new SerializationException(ex);
167         } finally {
168             try {
169                 if (in != null) {
170                     in.close();
171                 }
172             } catch (IOException JavaDoc ex) {
173                 // ignore
174
}
175         }
176     }
177
178     /**
179      * <p>Deserializes a single <code>Object</code> from an array of bytes.</p>
180      *
181      * @param objectData the serialized object, must not be null
182      * @return the deserialized object
183      * @throws IllegalArgumentException if <code>objectData</code> is <code>null</code>
184      * @throws SerializationException (runtime) if the serialization fails
185      */

186     public static Object JavaDoc deserialize(byte[] objectData) {
187         if (objectData == null) {
188             throw new IllegalArgumentException JavaDoc("The byte[] must not be null");
189         }
190         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(objectData);
191         return deserialize(bais);
192     }
193     
194 }
195
Popular Tags