1 22 package org.jboss.resource.binding.remote; 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.OutputStream ; 29 import java.io.Serializable ; 30 31 import org.jboss.serial.io.JBossObjectInputStream; 32 import org.jboss.serial.io.JBossObjectOutputStream; 33 34 40 public class RemoteSerializerImpl implements RemoteSerializer, Serializable 41 { 42 43 private static final RemoteSerializer serializer = new RemoteSerializerImpl(); 44 45 46 private static final long serialVersionUID = 6386719587282465130L; 47 48 public byte[] serializeToByte(final Object target) throws Exception 49 { 50 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 51 OutputStream oos = new ObjectOutputStream (baos); 52 JBossObjectOutputStream jbos = new JBossObjectOutputStream(oos); 53 jbos.writeObject(target); 54 jbos.close(); 55 56 return baos.toByteArray(); 57 58 } 59 60 public Object serialize(final Object target) throws Exception { 61 62 return shouldSerialize(target) ? serialize(target) : target; 63 64 } 65 66 public Object deserialize(Object target) throws Exception { 67 68 SerializableWrapper wrapper = (SerializableWrapper)target; 69 byte[] payload = wrapper.getPayload(); 70 ByteArrayInputStream bais = new ByteArrayInputStream (payload); 71 ObjectInputStream ois = new ObjectInputStream (bais); 72 JBossObjectInputStream jbis = new JBossObjectInputStream(ois); 73 Object result = jbis.readObject(); 74 return result; 75 76 } 77 public void serialize(final Object [] targets) throws Exception 78 { 79 80 for (int i = 0; i < targets.length; i++) 81 { 82 final Object target = targets[i]; 83 targets[i] = serialize(target); 84 85 } 86 87 } 88 89 public boolean shouldSerialize(Object target) 90 { 91 return !(target instanceof Serializable ); 92 93 } 94 95 static RemoteSerializer getInstance(){ 96 97 return serializer; 98 99 } 100 } 101 | Popular Tags |