MarshallTool.java

00001 
00027 package org.objectweb.jonas_ejb.lib;
00028 
00029 import java.io.ByteArrayInputStream;
00030 import java.io.ByteArrayOutputStream;
00031 import java.io.IOException;
00032 import java.io.InputStream;
00033 import java.io.ObjectInputStream;
00034 import java.io.ObjectOutputStream;
00035 import java.io.ObjectStreamClass;
00036 import java.io.Serializable;
00037 
00045 public class MarshallTool {
00046 
00053     public static synchronized byte[] toBytes(Serializable o)
00054             throws IOException {
00055 
00056         ByteArrayOutputStream bas = new ByteArrayOutputStream();
00057         ObjectOutputStream oos = new ObjectOutputStream(bas);
00058 
00059         oos.writeObject(o);
00060         oos.close();
00061 
00062         return bas.toByteArray();
00063     }
00064 
00072     public static synchronized Serializable fromBytes(byte[] bytes)
00073             throws IOException, ClassNotFoundException {
00074 
00079         class SpecializedOIS extends ObjectInputStream {
00080 
00086             SpecializedOIS(InputStream is) throws IOException {
00087                 super(is);
00088             }
00089 
00098             protected Class resolveClass(ObjectStreamClass osc) throws IOException, ClassNotFoundException {
00099                 try {
00100                     return super.resolveClass(osc);
00101                 } catch (ClassNotFoundException e) {
00102                     return Thread.currentThread().getContextClassLoader().loadClass(osc.getName());
00103                 }
00104             }
00105         }
00106 
00107         if (bytes == null) {
00108             return null;
00109         }
00110 
00111         ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
00112         SpecializedOIS sois = new SpecializedOIS(bis);
00113         return (Serializable) sois.readObject();
00114     }
00115 
00116 }
00117 

Generated on Tue Feb 15 15:05:49 2005 for JOnAS by  doxygen 1.3.9.1