1 22 package org.jboss.mx.util; 23 24 import org.jboss.mx.server.ObjectInputStreamWithClassLoader; 25 26 import java.io.*; 27 28 33 public class SerializationHelper 34 { 35 43 public static Object deserialize(byte[] byteArray) 44 throws IOException, ClassNotFoundException 45 { 46 return deserialize(byteArray,Thread.currentThread().getContextClassLoader()); 47 } 48 57 public static Object deserialize(byte[] byteArray, ClassLoader cl) 58 throws IOException, ClassNotFoundException 59 { 60 if (byteArray == null) 61 { 62 return null; 63 } 64 if (byteArray.length == 0) 65 { 66 return null; 67 } 68 try 69 { 70 if (cl==null) 71 { 72 cl = SerializationHelper.class.getClassLoader(); 74 } 75 ByteArrayInputStream bytearrayinputstream = new ByteArrayInputStream(byteArray); 76 ObjectInputStream objectinputstream = new ObjectInputStreamWithClassLoader(bytearrayinputstream,cl); 77 Object obj = objectinputstream.readObject(); 78 return obj; 79 } 80 catch (OptionalDataException optionaldataexception) 81 { 82 throw new IOException(optionaldataexception.getMessage()); 83 } 84 } 85 86 93 public static byte[] serialize(Object obj) 94 throws IOException 95 { 96 ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream(); 97 ObjectOutputStream objectoutputstream = new ObjectOutputStream(bytearrayoutputstream); 98 objectoutputstream.writeObject(obj); 99 return bytearrayoutputstream.toByteArray(); 100 } 101 } 102 | Popular Tags |