1 25 26 27 package org.objectweb.jonas_ejb.lib; 28 29 import java.io.ByteArrayInputStream ; 30 import java.io.ByteArrayOutputStream ; 31 import java.io.IOException ; 32 import java.io.InputStream ; 33 import java.io.ObjectInputStream ; 34 import java.io.ObjectOutputStream ; 35 import java.io.ObjectStreamClass ; 36 import java.io.Serializable ; 37 38 44 45 public class MarshallTool { 46 47 53 public static synchronized byte[] toBytes(Serializable o) 54 throws IOException { 55 56 ByteArrayOutputStream bas = new ByteArrayOutputStream (); 57 ObjectOutputStream oos = new ObjectOutputStream (bas); 58 59 oos.writeObject(o); 60 oos.close(); 61 62 return bas.toByteArray(); 63 } 64 65 72 public static synchronized Serializable fromBytes(byte[] bytes) 73 throws IOException , ClassNotFoundException { 74 75 79 class SpecializedOIS extends ObjectInputStream { 80 81 86 SpecializedOIS(InputStream is) throws IOException { 87 super(is); 88 } 89 90 98 protected Class resolveClass(ObjectStreamClass osc) throws IOException , ClassNotFoundException { 99 try { 100 return super.resolveClass(osc); 101 } catch (ClassNotFoundException e) { 102 return Thread.currentThread().getContextClassLoader().loadClass(osc.getName()); 103 } 104 } 105 } 106 107 if (bytes == null) { 108 return null; 109 } 110 111 ByteArrayInputStream bis = new ByteArrayInputStream (bytes); 112 SpecializedOIS sois = new SpecializedOIS(bis); 113 return (Serializable ) sois.readObject(); 114 } 115 116 } 117 118 | Popular Tags |