KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > util > marshalling > Serialization


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@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: Serialization.java 944 2006-07-26 09:34:44Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.util.marshalling;
27
28 import java.io.ByteArrayInputStream JavaDoc;
29 import java.io.ByteArrayOutputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.ObjectOutputStream JavaDoc;
32 import java.io.Serializable JavaDoc;
33
34 /**
35  * Allow to get the bytes of an object or to recreate an object from bytes.<br>
36  * It also used a custom reader when recreating object as it uses the context
37  * classloader in order to have an object with the right classloader.
38  * @author Florent Benoit
39  */

40 public final class Serialization {
41
42     /**
43      * Utility class, no public constructor.
44      */

45     private Serialization() {
46
47     }
48
49
50     /**
51      * Gets an array of bytes corresponding to the given object.
52      * @param object the object to serialize
53      * @return an array of bytes.
54      * @throws IOException if the object can't be turned into an array of bytes.
55      */

56     public static byte[] storeObject(final Serializable JavaDoc object) throws IOException JavaDoc {
57         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
58         ObjectOutputStream JavaDoc oos = null;
59         try {
60             oos = new ObjectOutputStream JavaDoc(baos);
61             oos.writeObject(object);
62             oos.flush();
63             return baos.toByteArray();
64         } finally {
65             if (oos != null) {
66                 oos.close();
67             }
68             if (baos != null) {
69                 baos.close();
70             }
71         }
72     }
73
74
75
76     /**
77      * Transforms the given array of bytes into an object.
78      * @param bytes given array of bytes representing the object
79      * @return object which has been unmarshalled.
80      * @throws IOException if marshalling fails
81      * @throws ClassNotFoundException if the class required for loading the object is not found.
82      */

83     public static Object JavaDoc loadObject(final byte[] bytes)
84             throws IOException JavaDoc, ClassNotFoundException JavaDoc {
85         // Do nothing if there aren't any bytes.
86
if (bytes == null) {
87             return null;
88         }
89
90         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(bytes);
91         CtxClassLoaderObjectInputStream ctxClassLoaderObjectInputStream = new CtxClassLoaderObjectInputStream(bais);
92         try {
93             return ctxClassLoaderObjectInputStream.readObject();
94         } finally {
95             if (ctxClassLoaderObjectInputStream != null) {
96                 ctxClassLoaderObjectInputStream.close();
97             }
98             if (ctxClassLoaderObjectInputStream != null) {
99                 bais.close();
100             }
101
102         }
103
104     }
105
106
107
108
109
110
111 }
112
Popular Tags