1 48 49 package org.exolab.jms.message; 50 51 import java.io.ByteArrayInputStream ; 52 import java.io.ByteArrayOutputStream ; 53 import java.io.IOException ; 54 import java.io.ObjectInput ; 55 import java.io.ObjectInputStream ; 56 import java.io.ObjectOutput ; 57 import java.io.ObjectOutputStream ; 58 import java.io.Serializable ; 59 60 import javax.jms.JMSException ; 61 import javax.jms.MessageFormatException ; 62 import javax.jms.MessageNotWriteableException ; 63 import javax.jms.ObjectMessage ; 64 65 66 86 public final class ObjectMessageImpl extends MessageImpl 87 implements ObjectMessage { 88 89 92 static final long serialVersionUID = 1; 93 94 97 private byte[] _bytes = null; 98 99 104 public ObjectMessageImpl() throws JMSException { 105 setJMSType("ObjectMessage"); 106 } 107 108 115 public final Object clone() throws CloneNotSupportedException { 116 ObjectMessageImpl result = (ObjectMessageImpl) super.clone(); 117 if (_bytes != null) { 118 result._bytes = new byte[_bytes.length]; 119 System.arraycopy(_bytes, 0, result._bytes, 0, _bytes.length); 120 } 121 return result; 122 } 123 124 130 public final void writeExternal(ObjectOutput out) throws IOException { 131 super.writeExternal(out); 132 out.writeLong(serialVersionUID); 133 if (_bytes != null) { 134 out.writeInt(_bytes.length); 135 out.write(_bytes); 136 } else { 137 out.writeInt(0); 138 } 139 } 140 141 149 public final void readExternal(ObjectInput in) 150 throws ClassNotFoundException , IOException { 151 super.readExternal(in); 152 long version = in.readLong(); 153 if (version == serialVersionUID) { 154 int length = in.readInt(); 155 if (length != 0) { 156 _bytes = new byte[length]; 157 in.readFully(_bytes); 158 } else { 159 _bytes = null; 160 } 161 } else { 162 throw new IOException ( 163 "Incorrect version enountered: " + version + 164 ". This version = " + serialVersionUID); 165 } 166 } 167 168 179 public final void setObject(Serializable object) 180 throws MessageFormatException , MessageNotWriteableException { 181 checkWrite(); 182 183 try { 184 ByteArrayOutputStream byteOut = new ByteArrayOutputStream (); 185 ObjectOutputStream out = new ObjectOutputStream (byteOut); 186 187 out.writeObject(object); 188 out.flush(); 189 _bytes = byteOut.toByteArray(); 190 out.close(); 191 } catch (IOException exception) { 192 MessageFormatException error = new MessageFormatException ( 193 exception.getMessage()); 194 error.setLinkedException(exception); 195 throw error; 196 } 197 } 198 199 207 public final Serializable getObject() throws MessageFormatException { 208 Serializable result = null; 209 if (_bytes != null) { 210 try { 211 ByteArrayInputStream byteIn = 212 new ByteArrayInputStream (_bytes); 213 ObjectInputStream in = new ObjectInputStream (byteIn); 214 215 result = (Serializable ) in.readObject(); 216 in.close(); 217 } catch (IOException exception) { 218 MessageFormatException error = 219 new MessageFormatException (exception.getMessage()); 220 error.setLinkedException(exception); 221 throw error; 222 } catch (ClassNotFoundException exception) { 223 MessageFormatException error = 224 new MessageFormatException (exception.getMessage()); 225 error.setLinkedException(exception); 226 throw error; 227 } 228 } 229 return result; 230 } 231 232 239 public final void clearBody() throws JMSException { 240 super.clearBody(); 241 _bytes = null; 242 } 243 244 } | Popular Tags |