KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > common > ObjectSerializer


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * ObjectSerializer.java
20  *
21  * This class is responsible for serializing and deserializing objects.
22  */

23
24 // package path
25
package com.rift.coad.lib.common;
26
27 // java imports
28
import java.io.IOException JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.io.ByteArrayInputStream JavaDoc;
31 import java.io.ByteArrayOutputStream JavaDoc;
32 import java.io.ObjectInputStream JavaDoc;
33 import java.io.ObjectOutputStream JavaDoc;
34 import java.io.ObjectStreamClass JavaDoc;
35
36 /**
37  * This class is responsible for serializing and deserializing objects.
38  *
39  * @author Brett Chaldecott
40  */

41 public class ObjectSerializer {
42     
43     /**
44      * This class overrides the resolve to use the class loader on the
45      * thread to find the specified class.
46      */

47     public static class ClassLoaderObjectInputStream extends ObjectInputStream JavaDoc {
48         /**
49          * This default constructor of the class loader object input stream.
50          *
51          * @exception IOException
52          */

53         public ClassLoaderObjectInputStream() throws IOException JavaDoc {
54             super();
55         }
56         
57         
58         /**
59          * This default constructor of the class loader object input stream.
60          *
61          * @param in The input stream for this object.
62          * @exception IOException
63          */

64         public ClassLoaderObjectInputStream(InputStream JavaDoc in) throws IOException JavaDoc {
65             super(in);
66         }
67         
68         
69         /**
70          * This method returns the class definition for the requested object.
71          *
72          * @return The class definition for the requested object.
73          * @param desc The description of the object.
74          * @exception IOException
75          * @exception ClassNotFoundException
76          */

77         protected Class JavaDoc resolveClass(ObjectStreamClass JavaDoc desc) throws IOException JavaDoc,
78                 ClassNotFoundException JavaDoc {
79             return Thread.currentThread().getContextClassLoader().loadClass(
80                     desc.getName());
81         }
82         
83     }
84     
85     /**
86      * Creates a new instance of ObjectSerializer
87      */

88     private ObjectSerializer() {
89     }
90     
91     
92     /**
93      * This method serializes the object passed to it.
94      *
95      * @return A byte array of the object.
96      * @param ref The reference to the object to serialize.
97      * @exception CommonException
98      */

99     public static byte[] serialize(Object JavaDoc ref) throws CommonException {
100         try {
101             if (!(ref instanceof java.io.Serializable JavaDoc)) {
102                 throw new CommonException("This object is not serializable. " +
103                         "Must implement from java.io.Serializable.");
104             }
105             ByteArrayOutputStream JavaDoc byteOutput = new ByteArrayOutputStream JavaDoc();
106             ObjectOutputStream JavaDoc objOutput = new ObjectOutputStream JavaDoc(byteOutput);
107             objOutput.writeObject(ref);
108             objOutput.flush();
109             objOutput.close();
110             return byteOutput.toByteArray();
111         } catch (CommonException ex) {
112             throw ex;
113         } catch (Exception JavaDoc ex) {
114             throw new CommonException("Failed to serialize the object : " +
115                     ex.getMessage(),ex);
116         }
117     }
118     
119     
120     /**
121      * This method serializes the object passed to it.
122      *
123      * @return A byte array of the object.
124      * @param ref The reference to the object to serialize.
125      * @exception CommonException
126      */

127     public static Object JavaDoc deserialize(byte[] input) throws CommonException {
128         try {
129             ByteArrayInputStream JavaDoc byteInput = new ByteArrayInputStream JavaDoc(input);
130             ClassLoaderObjectInputStream objInput =
131                     new ClassLoaderObjectInputStream(byteInput);
132             Object JavaDoc ref = objInput.readObject();
133             objInput.close();
134             return ref;
135         } catch (Exception JavaDoc ex) {
136             throw new CommonException("Failed to deserialize the object : " +
137                     ex.getMessage(),ex);
138         }
139     }
140 }
141
Popular Tags