1 18 package org.apache.activemq.wireformat; 19 20 import java.io.DataInput ; 21 import java.io.DataInputStream ; 22 import java.io.DataOutput ; 23 import java.io.DataOutputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.ObjectOutputStream ; 27 import java.io.OutputStream ; 28 29 import org.apache.activemq.util.ByteArrayInputStream; 30 import org.apache.activemq.util.ByteArrayOutputStream; 31 import org.apache.activemq.util.ByteSequence; 32 import org.apache.activemq.util.ClassLoadingAwareObjectInputStream; 33 34 39 public class ObjectStreamWireFormat implements WireFormat { 40 41 public ByteSequence marshal(Object command) throws IOException { 42 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 43 DataOutputStream ds = new DataOutputStream (baos); 44 marshal(command, ds); 45 ds.close(); 46 return baos.toByteSequence(); 47 } 48 49 public Object unmarshal(ByteSequence packet) throws IOException { 50 return unmarshal(new DataInputStream (new ByteArrayInputStream(packet))); 51 } 52 53 public void marshal(Object command, DataOutput ds) throws IOException { 54 ObjectOutputStream out = new ObjectOutputStream ((OutputStream )ds); 55 out.writeObject(command); 56 out.flush(); 57 out.reset(); 58 } 59 60 public Object unmarshal(DataInput ds) throws IOException { 61 try { 62 ClassLoadingAwareObjectInputStream in = new ClassLoadingAwareObjectInputStream((InputStream )ds); 63 Object command; 64 command = in.readObject(); 65 in.close(); 66 return command; 67 } catch (ClassNotFoundException e) { 68 throw (IOException )new IOException ("unmarshal failed: "+e).initCause(e); 69 } 70 } 71 72 public void setVersion(int version) { 73 } 74 75 public int getVersion() { 76 return 0; 77 } 78 79 } 80 | Popular Tags |