1 14 15 package org.apache.activemq.kaha; 16 17 import java.io.ByteArrayInputStream ; 18 import java.io.ByteArrayOutputStream ; 19 import java.io.DataInput ; 20 import java.io.DataOutput ; 21 import java.io.IOException ; 22 import java.io.ObjectInputStream ; 23 import java.io.ObjectOutputStream ; 24 25 30 public class ObjectMarshaller implements Marshaller{ 31 32 39 public void writePayload(Object object,DataOutput dataOut) throws IOException { 40 ByteArrayOutputStream bytesOut=new ByteArrayOutputStream (); 41 ObjectOutputStream objectOut=new ObjectOutputStream (bytesOut); 42 objectOut.writeObject(object); 43 objectOut.close(); 44 byte[] data=bytesOut.toByteArray(); 45 dataOut.writeInt(data.length); 46 dataOut.write(data); 47 } 48 49 56 public Object readPayload(DataInput dataIn) throws IOException { 57 int size=dataIn.readInt(); 58 byte[] data=new byte[size]; 59 dataIn.readFully(data); 60 ByteArrayInputStream bytesIn=new ByteArrayInputStream (data); 61 ObjectInputStream objectIn=new ObjectInputStream (bytesIn); 62 try{ 63 return objectIn.readObject(); 64 }catch(ClassNotFoundException e){ 65 throw new IOException (e.getMessage()); 66 } 67 } 68 } 69 | Popular Tags |