KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ejb > lib > MarshallTool


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: MarshallTool.java,v 1.5 2004/12/16 09:28:10 joaninh Exp $
23  * --------------------------------------------------------------------------
24  */

25
26
27 package org.objectweb.jonas_ejb.lib;
28
29 import java.io.ByteArrayInputStream JavaDoc;
30 import java.io.ByteArrayOutputStream JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.io.ObjectInputStream JavaDoc;
34 import java.io.ObjectOutputStream JavaDoc;
35 import java.io.ObjectStreamClass JavaDoc;
36 import java.io.Serializable JavaDoc;
37
38 /**
39  * Contains static methods to marshall/unmarshall objects to/from a byte[]
40  *
41  * @author Vincent Trussart (vincent@linuxfreak.com) : Initial developer
42  * @author Helene Joanin
43  */

44
45 public class MarshallTool {
46
47     /**
48      * Converts an instance of java.io.Serializable to a serialized byte array
49      * @param o Object to serialize
50      * @return byte array containing the serialization of the Serializable object
51      * @throws IOException in error case
52      */

53     public static synchronized byte[] toBytes(Serializable JavaDoc o)
54             throws IOException JavaDoc {
55
56         ByteArrayOutputStream JavaDoc bas = new ByteArrayOutputStream JavaDoc();
57         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(bas);
58
59         oos.writeObject(o);
60         oos.close();
61
62         return bas.toByteArray();
63     }
64
65     /**
66      * Converts byte array to an instance of java.io.Serializable
67      * @param bytes A byte array containing the a serialized object
68      * @return the unmarshalled object
69      * @throws IOException in error case
70      * @throws ClassNotFoundException in error case
71      */

72     public static synchronized Serializable JavaDoc fromBytes(byte[] bytes)
73             throws IOException JavaDoc, ClassNotFoundException JavaDoc {
74
75         /**
76          * Specialization of the java.io.ObjectInputStream to re-implement the
77          * resolveClass method
78          */

79         class SpecializedOIS extends ObjectInputStream JavaDoc {
80
81             /**
82              * Default constructor
83              * @param is input stream to read from
84              * @throws IOException in error case
85              */

86             SpecializedOIS(InputStream JavaDoc is) throws IOException JavaDoc {
87                 super(is);
88             }
89
90             /**
91              * Load the local class equivalent of the specified stream class
92              * description using the context class loader.
93              * @param osc an instance of class ObjectStreamClass
94              * @return a Class object corresponding to the osc
95              * @throws IOException in error case
96              * @throws ClassNotFoundException in error case
97              */

98             protected Class JavaDoc resolveClass(ObjectStreamClass JavaDoc osc) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
99                 try {
100                     return super.resolveClass(osc);
101                 } catch (ClassNotFoundException JavaDoc e) {
102                     return Thread.currentThread().getContextClassLoader().loadClass(osc.getName());
103                 }
104             }
105         }
106
107         if (bytes == null) {
108             return null;
109         }
110
111         ByteArrayInputStream JavaDoc bis = new ByteArrayInputStream JavaDoc(bytes);
112         SpecializedOIS sois = new SpecializedOIS(bis);
113         return (Serializable JavaDoc) sois.readObject();
114     }
115
116 }
117
118
Popular Tags