1 21 package com.presumo.jms.message; 22 23 import java.io.IOException ; 24 import java.io.DataInput ; 25 import java.io.DataOutput ; 26 27 33 public final class MessageEncoder 34 { 35 38 static final byte MESSAGE = 0; 39 static final byte TEXT_MESSAGE = 1; 40 static final byte BYTE_MESSAGE = 2; 41 static final byte MAP_MESSAGE = 3; 42 static final byte STREAM_MESSAGE = 4; 43 static final byte OBJECT_MESSAGE = 5; 44 45 49 57 public static void encode(JmsMessage msg, DataOutput out) throws IOException 58 { 59 if (msg == null) 60 return; 61 62 out.write(msg.getMarshalingID()); 63 msg.marshal(out); 64 } 65 66 76 public static void encode(JmsMessage [] msgs, DataOutput out) throws IOException 77 { 78 if (msgs == null) 79 return; 80 81 for (int i=0; i < msgs.length; ++i) { 82 out.write(msgs[i].getMarshalingID()); 83 msgs[i].marshal(out); 84 } 85 } 86 87 99 public static JmsMessage decode(DataInput in) throws IOException 100 { 101 byte messageID = in.readByte(); 102 JmsMessage msg = null; 103 switch (messageID) 104 { 105 case (MESSAGE): 106 msg = new JmsMessage(); 107 msg.unmarshal(in); 108 109 break; 110 case (TEXT_MESSAGE): 111 msg = new JmsTextMessage(); 112 msg.unmarshal(in); 113 114 break; 115 case (BYTE_MESSAGE): 116 msg = new JmsBytesMessage(); 117 msg.unmarshal(in); 118 119 break; 120 case (MAP_MESSAGE): 121 msg = new JmsMapMessage(); 122 msg.unmarshal(in); 123 124 break; 125 case (STREAM_MESSAGE): 126 msg = new JmsStreamMessage(); 127 msg.unmarshal(in); 128 129 break; 130 case (OBJECT_MESSAGE): 131 msg = new JmsObjectMessage(); 132 msg.unmarshal(in); 133 134 break; 135 default: 136 throw new IOException ("The input stream is corrupted."); 137 } 138 return msg; 139 } 140 141 } 142 | Popular Tags |