KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v2 > ser > SerializableUtils


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

22
23
24 package com.mchange.v2.ser;
25
26 import java.io.*;
27 import com.mchange.v1.io.*;
28 import com.mchange.v2.log.*;
29
30 public final class SerializableUtils
31 {
32     final static MLogger logger = MLog.getLogger( SerializableUtils.class );
33
34     private SerializableUtils()
35     {}
36
37
38     public static byte[] toByteArray(Object JavaDoc obj) throws NotSerializableException
39     { return serializeToByteArray( obj ); }
40
41     public static byte[] toByteArray(Object JavaDoc obj, Indirector indirector, IndirectPolicy policy) throws NotSerializableException
42     {
43     try
44         {
45         if (policy == IndirectPolicy.DEFINITELY_INDIRECT)
46             {
47             if (indirector == null)
48                 throw new IllegalArgumentException JavaDoc("null indirector is not consistent with " + policy);
49
50             IndirectlySerialized indirect = indirector.indirectForm( obj );
51             return toByteArray( indirect );
52             }
53         else if ( policy == IndirectPolicy.INDIRECT_ON_EXCEPTION )
54             {
55             if (indirector == null)
56                 throw new IllegalArgumentException JavaDoc("null indirector is not consistent with " + policy);
57
58             try { return toByteArray( obj ); }
59             catch ( NotSerializableException e )
60                 { return toByteArray( obj, indirector, IndirectPolicy.DEFINITELY_INDIRECT ); }
61             }
62         else if (policy == IndirectPolicy.DEFINITELY_DIRECT)
63             return toByteArray( obj );
64         else
65             throw new InternalError JavaDoc("unknown indirecting policy: " + policy);
66         }
67     catch ( NotSerializableException e )
68         { throw e; }
69     catch ( Exception JavaDoc e )
70         {
71         //e.printStackTrace();
72
if ( logger.isLoggable( MLevel.WARNING ) )
73             logger.log( MLevel.WARNING, "An Exception occurred while serializing an Object to a byte[] with an Indirector.", e );
74         throw new NotSerializableException( e.toString() );
75         }
76     }
77
78     /**
79      * @deprecated use SerialializableUtils.toByteArray() [shorter name is better!]
80      */

81     public static byte[] serializeToByteArray(Object JavaDoc obj) throws NotSerializableException
82     {
83     try
84     {
85         ByteArrayOutputStream baos = new ByteArrayOutputStream();
86         ObjectOutputStream out = new ObjectOutputStream(baos);
87         out.writeObject(obj);
88         return baos.toByteArray();
89     }
90     catch (NotSerializableException e)
91     {
92         //this is the only IOException that
93
//shouldn't signal a bizarre error...
94
e.fillInStackTrace();
95         throw e;
96     }
97     catch (IOException e)
98     {
99         //e.printStackTrace();
100
if ( logger.isLoggable( MLevel.SEVERE ) )
101         logger.log( MLevel.SEVERE, "An IOException occurred while writing into a ByteArrayOutputStream?!?", e );
102         throw new Error JavaDoc("IOException writing to a byte array!");
103     }
104     }
105     
106     /**
107      * By default, unwraps IndirectlySerialized objects, returning the original
108      */

109     public static Object JavaDoc fromByteArray(byte[] bytes) throws IOException, ClassNotFoundException JavaDoc
110     {
111     Object JavaDoc out = deserializeFromByteArray( bytes );
112     if (out instanceof IndirectlySerialized)
113         return ((IndirectlySerialized) out).getObject();
114     else
115         return out;
116     }
117
118     public static Object JavaDoc fromByteArray(byte[] bytes, boolean ignore_indirects) throws IOException, ClassNotFoundException JavaDoc
119     {
120     if (ignore_indirects)
121         return deserializeFromByteArray( bytes );
122     else
123         return fromByteArray( bytes );
124     }
125
126     /**
127      * @deprecated use SerialializableUtils.fromByteArray() [shorter name is better!]
128      */

129     public static Object JavaDoc deserializeFromByteArray(byte[] bytes) throws IOException, ClassNotFoundException JavaDoc
130     {
131     ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes));
132     return in.readObject();
133     }
134
135
136     public static Object JavaDoc testSerializeDeserialize( Object JavaDoc o ) throws IOException, ClassNotFoundException JavaDoc
137     { return deepCopy( o ); }
138
139     public static Object JavaDoc deepCopy( Object JavaDoc o ) throws IOException, ClassNotFoundException JavaDoc
140     {
141     byte[] bytes = serializeToByteArray( o );
142     return deserializeFromByteArray( bytes );
143     }
144
145     public final static Object JavaDoc unmarshallObjectFromFile(File file)
146     throws IOException, ClassNotFoundException JavaDoc
147     {
148       ObjectInputStream in = null;
149       try
150     {
151       in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file)));
152       return in.readObject();
153     }
154       finally
155     {InputStreamUtils.attemptClose(in);}
156     }
157
158   public final static void marshallObjectToFile(Object JavaDoc o, File file)
159       throws IOException
160     {
161       ObjectOutputStream out = null;
162       try
163     {
164       out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
165       out.writeObject(o);
166     }
167       finally
168     {OutputStreamUtils.attemptClose(out);}
169     }
170 }
171
172
Popular Tags