1 7 package org.jboss.jms; 8 9 import javax.jms.BytesMessage ; 10 import javax.jms.JMSException ; 11 import javax.jms.MessageNotReadableException ; 12 import javax.jms.MessageNotWriteableException ; 13 import java.io.ByteArrayInputStream ; 14 import java.io.ByteArrayOutputStream ; 15 import java.io.DataInputStream ; 16 import java.io.DataOutputStream ; 17 import java.io.IOException ; 18 19 23 public class BytesMessageImpl extends MessageImpl implements BytesMessage 24 { 25 26 private ByteArrayOutputStream outputStream = null; 27 28 public BytesMessageImpl() 29 { 30 super.type = MessageImpl.BYTES_MESSAGE_NAME; 31 this.outputStream = new ByteArrayOutputStream (); 32 super.body = new DataOutputStream (this.outputStream); 33 } 34 35 public long getBodyLength() throws JMSException 36 { 37 return 0; 38 } 39 40 private DataInputStream getReadableBody() 41 throws MessageNotReadableException 42 { 43 if (!this.isReadOnly()) 44 { 45 throw new MessageNotReadableException ( 46 "Unable to read message body. The message body" 47 + "is currently marked as write only."); 48 } 49 return (DataInputStream ) super.body; 50 } 51 52 private DataOutputStream getWritableBody() 53 throws MessageNotWriteableException 54 { 55 super.throwExceptionIfReadOnly(); 56 return (DataOutputStream ) super.body; 57 } 58 59 public boolean readBoolean() throws JMSException 60 { 61 try 62 { 63 return this.getReadableBody().readBoolean(); 64 } 65 catch (IOException exception) 66 { 67 throw new JMSException (""); 68 } 70 } 71 72 public byte readByte() throws JMSException 73 { 74 try 75 { 76 return this.getReadableBody().readByte(); 77 } 78 catch (IOException exception) 79 { 80 throw new JMSException (""); 81 } 83 } 84 85 public int readBytes(byte[] value) throws JMSException 86 { 87 try 88 { 89 return this.getReadableBody().read(value); 90 } 91 catch (IOException exception) 92 { 93 throw new JMSException (""); 94 } 96 } 97 98 public int readBytes(byte[] value, int length) throws JMSException 99 { 100 try 101 { 102 return this.getReadableBody().read(value, 0, length); 103 } 105 catch (IOException exception) 106 { 107 throw new JMSException (""); 108 } 110 } 111 112 public char readChar() throws JMSException 113 { 114 try 115 { 116 return this.getReadableBody().readChar(); 117 } 118 catch (IOException exception) 119 { 120 throw new JMSException (""); 121 } 123 } 124 125 public double readDouble() throws JMSException 126 { 127 try 128 { 129 return this.getReadableBody().readDouble(); 130 } 131 catch (IOException exception) 132 { 133 throw new JMSException (""); 134 } 136 } 137 138 public float readFloat() throws JMSException 139 { 140 try 141 { 142 return this.getReadableBody().readFloat(); 143 } 144 catch (IOException exception) 145 { 146 throw new JMSException (""); 147 } 149 } 150 151 public int readInt() throws JMSException 152 { 153 try 154 { 155 return this.getReadableBody().readInt(); 156 } 157 catch (IOException exception) 158 { 159 throw new JMSException (""); 160 } 162 } 163 164 public long readLong() throws JMSException 165 { 166 try 167 { 168 return this.getReadableBody().readLong(); 169 } 170 catch (IOException exception) 171 { 172 throw new JMSException (""); 173 } 175 } 176 177 public short readShort() throws JMSException 178 { 179 try 180 { 181 return this.getReadableBody().readShort(); 182 } 183 catch (IOException exception) 184 { 185 throw new JMSException (""); 186 } 188 } 189 190 public int readUnsignedByte() throws JMSException 191 { 192 try 193 { 194 return this.getReadableBody().readUnsignedByte(); 195 } 196 catch (IOException exception) 197 { 198 throw new JMSException (""); 199 } 201 } 202 203 public int readUnsignedShort() throws JMSException 204 { 205 try 206 { 207 return this.getReadableBody().readUnsignedShort(); 208 } 209 catch (IOException exception) 210 { 211 throw new JMSException (""); 212 } 214 } 215 216 public String readUTF() throws JMSException 217 { 218 try 219 { 220 return this.getReadableBody().readUTF(); 221 } 222 catch (IOException exception) 223 { 224 throw new JMSException (""); 225 } 227 } 228 229 public void reset() throws JMSException 230 { 231 this.setReadOnly(true); 232 super.body = 233 new DataInputStream ( 234 new ByteArrayInputStream (this.outputStream.toByteArray())); 235 try 236 { 237 this.outputStream.close(); 238 } 239 catch (IOException exception) 240 { 241 } 243 finally 244 { 245 this.outputStream = null; 246 } 247 } 248 249 public void writeBoolean(boolean value) throws JMSException 250 { 251 try 252 { 253 this.getWritableBody().writeBoolean(value); 254 } 255 catch (IOException exception) 256 { 257 } 259 } 260 261 public void writeByte(byte value) throws JMSException 262 { 263 try 264 { 265 this.getWritableBody().writeByte(value); 266 } 267 catch (IOException exception) 268 { 269 } 271 } 272 273 public void writeBytes(byte[] value) throws JMSException 274 { 275 try 276 { 277 this.getWritableBody().write(value); 278 } 279 catch (IOException exception) 280 { 281 } 283 } 284 285 public void writeBytes(byte[] value, int offset, int length) 286 throws JMSException 287 { 288 try 289 { 290 this.getWritableBody().write(value, offset, length); 291 } 292 catch (IOException exception) 293 { 294 } 296 } 297 298 public void writeChar(char value) throws JMSException 299 { 300 try 301 { 302 this.getWritableBody().writeChar(value); 303 } 304 catch (IOException exception) 305 { 306 } 308 } 309 310 public void writeDouble(double value) throws JMSException 311 { 312 try 313 { 314 this.getWritableBody().writeDouble(value); 315 } 316 catch (IOException exception) 317 { 318 } 320 } 321 322 public void writeFloat(float value) throws JMSException 323 { 324 try 325 { 326 this.getWritableBody().writeFloat(value); 327 } 328 catch (IOException exception) 329 { 330 } 332 } 333 334 public void writeInt(int value) throws JMSException 335 { 336 try 337 { 338 this.getWritableBody().writeInt(value); 339 } 340 catch (IOException exception) 341 { 342 } 344 } 345 346 public void writeLong(long value) throws JMSException 347 { 348 try 349 { 350 this.getWritableBody().writeLong(value); 351 } 352 catch (IOException exception) 353 { 354 } 356 } 357 358 public void writeObject(Object value) throws JMSException 359 { 360 if (value instanceof Boolean ) 361 { 362 this.writeBoolean(((Boolean ) value).booleanValue()); 363 } 364 else if (value instanceof Byte ) 365 { 366 this.writeByte(((Byte ) value).byteValue()); 367 } 368 else if (value instanceof Byte []) 369 { 370 this.writeBytes((byte[]) value); 371 } 372 else if (value instanceof Character ) 373 { 374 this.writeChar(((Character ) value).charValue()); 375 } 376 else if (value instanceof Double ) 377 { 378 this.writeDouble(((Double ) value).doubleValue()); 379 } 380 else if (value instanceof Float ) 381 { 382 this.writeFloat(((Float ) value).floatValue()); 383 } 384 else if (value instanceof Integer ) 385 { 386 this.writeInt(((Integer ) value).intValue()); 387 } 388 else if (value instanceof Long ) 389 { 390 this.writeLong(((Long ) value).longValue()); 391 } 392 else if (value instanceof Short ) 393 { 394 this.writeShort(((Short ) value).shortValue()); 395 } 396 else if (value instanceof String ) 397 { 398 this.writeUTF((String ) value); 399 } 400 else 401 { 402 } 404 } 405 406 public void writeShort(short value) throws JMSException 407 { 408 try 409 { 410 this.getWritableBody().writeShort(value); 411 } 412 catch (IOException exception) 413 { 414 } 416 } 417 418 public void writeUTF(String value) throws JMSException 419 { 420 try 421 { 422 this.getWritableBody().writeUTF(value); 423 } 424 catch (IOException exception) 425 { 426 } 428 } 429 430 public void clearBody() 431 { 432 this.setReadOnly(false); 433 this.outputStream = new ByteArrayOutputStream (); 434 this.body = super.body = new DataOutputStream (this.outputStream); 435 } 436 437 } | Popular Tags |