1 22 package org.jboss.util; 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.ByteArrayOutputStream ; 26 import java.io.ObjectInputStream ; 27 import java.io.ObjectOutputStream ; 28 import java.io.IOException ; 29 30 import org.jboss.util.stream.CustomObjectInputStreamWithClassloader; 31 import org.jboss.util.stream.CustomObjectOutputStream; 32 33 37 public class Conversion 38 { 39 44 public static byte[] toByteArray(Object obj) 45 { 46 try { 47 ByteArrayOutputStream os = new ByteArrayOutputStream (); 48 ObjectOutputStream oos = new CustomObjectOutputStream(os); 49 50 oos.writeObject(obj); 51 oos.flush(); 52 byte[] a = os.toByteArray(); 53 os.close(); 54 return a; 55 } 56 catch (IOException ioe) { 57 throw new RuntimeException ("Object id serialization error:\n" + ioe); 58 } 59 } 60 61 67 public static Object toObject(byte[] a, ClassLoader cl) 68 throws IOException , ClassNotFoundException 69 { 70 ByteArrayInputStream is = new ByteArrayInputStream (a); 71 ObjectInputStream ois = 72 new CustomObjectInputStreamWithClassloader(is, cl); 73 Object obj = ois.readObject(); 74 is.close(); 75 return obj; 76 } 77 78 84 public static Object toObject(byte[] a) 85 throws IOException , ClassNotFoundException 86 { 87 return toObject(a, Thread.currentThread().getContextClassLoader()); 88 } 89 90 } 91 | Popular Tags |