1 21 package com.presumo.jms.message; 22 23 import java.io.IOException ; 24 import java.io.EOFException ; 25 26 import java.io.DataInput ; 27 import java.io.DataOutput ; 28 29 import java.io.ByteArrayOutputStream ; 30 import java.io.ByteArrayInputStream ; 31 import java.io.DataOutputStream ; 32 import java.io.DataInputStream ; 33 34 import javax.jms.BytesMessage ; 35 import javax.jms.JMSException ; 36 import javax.jms.MessageFormatException ; 37 import javax.jms.MessageNotReadableException ; 38 import javax.jms.MessageEOFException ; 39 import javax.jms.MessageNotWriteableException ; 40 41 47 public final class JmsBytesMessage extends JmsMessage 48 implements BytesMessage , java.io.Externalizable 49 { 50 51 private byte [] byteMsgArray; 52 private ByteArrayOutputStream byteOutStream; 53 private DataOutputStream dataOutStream; 54 private ByteArrayInputStream byteInStream; 55 private DataInputStream dataInStream; 56 57 58 62 public JmsBytesMessage() 63 { 64 super(); 65 } 66 67 public JmsBytesMessage(String name) 68 { 69 super(name); 70 } 71 72 public JmsBytesMessage(byte [] msg, int offset, int length) 73 throws IOException 74 { 75 super(msg, offset, length); 76 } 77 78 82 86 public byte readByte() throws JMSException { 87 88 if (!this.readOnly){ 89 JMSException jmse = new MessageNotReadableException ("This Message is open for writing only"); 90 throw jmse; 91 } 92 byte retval; 93 94 try { 95 retval = dataInStream.readByte(); 96 97 } catch (EOFException e) { 98 JMSException jmse = new MessageEOFException ("End of Message Reached"); 99 jmse.setLinkedException(e); 100 throw jmse; 101 } catch (IOException e) { 102 JMSException jmse = new MessageFormatException ("Unable to read from Stream"); 103 jmse.setLinkedException(e); 104 throw jmse; 105 } 106 return retval; 107 } 108 109 public int readUnsignedByte() throws JMSException 110 { 111 if (!this.readOnly){ 112 JMSException jmse = new MessageNotReadableException ("This Message is open for writing only"); 113 throw jmse; 114 } 115 int retval; 116 try { 117 118 retval = dataInStream.readUnsignedByte(); 119 120 } catch (EOFException e) { 121 JMSException jmse = new MessageEOFException ("End of Message Reached"); 122 jmse.setLinkedException(e); 123 throw jmse; 124 } catch (IOException e) { 125 JMSException jmse = new MessageFormatException ("Unable to read from Stream"); 126 jmse.setLinkedException(e); 127 throw jmse; 128 } 129 return retval; 130 131 } 132 133 public int readBytes(byte[] value) throws JMSException 134 { 135 136 if (!this.readOnly){ 137 JMSException jmse = new MessageNotReadableException ("This Message is open for writing only"); 138 throw jmse; 139 } 140 int retval; 141 try { 142 retval = dataInStream.read(value); 143 } catch (IOException e) { 144 JMSException jmse = new MessageFormatException ("Unable to read from Stream"); 145 jmse.setLinkedException(e); 146 throw jmse; 147 } 148 return retval; 149 } 150 151 public int readBytes(byte[] value, int length) throws JMSException 152 { 153 154 if (!this.readOnly){ 155 JMSException jmse = new MessageNotReadableException ("This Message is open for writing only"); 156 throw jmse; 157 } 158 int retval; 159 try { 160 retval = dataInStream.read(value, 0, length); 161 } catch (IOException e) { 162 JMSException jmse = new MessageFormatException ("Unable to read from Stream"); 163 jmse.setLinkedException(e); 164 throw jmse; 165 } 166 return retval; 167 } 168 169 public boolean readBoolean() throws JMSException { 170 171 if (!this.readOnly){ 172 JMSException jmse = new MessageNotReadableException ("This Message is open for writing only"); 173 throw jmse; 174 } 175 boolean retval; 176 try { 177 retval = dataInStream.readBoolean(); 178 } catch (EOFException e) { 179 JMSException jmse = new MessageEOFException ("End of Message Reached"); 180 jmse.setLinkedException(e); 181 throw jmse; 182 } catch (IOException e) { 183 JMSException jmse = new MessageFormatException ("Unable to read from Stream"); 184 jmse.setLinkedException(e); 185 throw jmse; 186 } 187 return retval; 188 } 189 190 public char readChar() throws JMSException { 191 192 if (!this.readOnly){ 193 JMSException jmse = new MessageNotReadableException ("This Message is open for writing only"); 194 throw jmse; 195 } 196 char retval; 197 try { 198 retval = dataInStream.readChar(); 199 } catch (EOFException e) { 200 JMSException jmse = new MessageEOFException ("End of Message Reached"); 201 jmse.setLinkedException(e); 202 throw jmse; 203 } catch (IOException e) { 204 JMSException jmse = new MessageFormatException ("Unable to read from Stream"); 205 jmse.setLinkedException(e); 206 throw jmse; 207 } 208 return retval; 209 } 210 211 public short readShort() throws JMSException { 212 213 if (!this.readOnly){ 214 JMSException jmse = new MessageNotReadableException ("This Message is open for writing only"); 215 throw jmse; 216 } 217 short retval; 218 try { 219 retval = dataInStream.readShort(); 220 } catch (EOFException e) { 221 JMSException jmse = new MessageEOFException ("End of Message Reached"); 222 jmse.setLinkedException(e); 223 throw jmse; 224 } catch (IOException e) { 225 JMSException jmse = new MessageFormatException ("Unable to read from Stream"); 226 jmse.setLinkedException(e); 227 throw jmse; 228 } 229 return retval; 230 } 231 232 public int readUnsignedShort() throws JMSException { 233 234 if (!this.readOnly){ 235 JMSException jmse = new MessageNotReadableException ("This Message is open for writing only"); 236 throw jmse; 237 } 238 int retval; 239 try { 240 retval = dataInStream.readUnsignedShort(); 241 } catch (EOFException e) { 242 JMSException jmse = new MessageEOFException ("End of Message Reached"); 243 jmse.setLinkedException(e); 244 throw jmse; 245 } catch (IOException e) { 246 JMSException jmse = new MessageFormatException ("Unable to read from Stream"); 247 jmse.setLinkedException(e); 248 throw jmse; 249 } 250 return retval; 251 } 252 253 public int readInt() throws JMSException 254 { 255 if (!this.readOnly){ 256 JMSException jmse = new MessageNotReadableException ("This Message is open for writing only"); 257 throw jmse; 258 } 259 int retval; 260 try { 261 retval = dataInStream.readInt(); 262 } catch (EOFException e) { 263 JMSException jmse = new MessageEOFException ("End of Message Reached"); 264 jmse.setLinkedException(e); 265 throw jmse; 266 } catch (IOException e) { 267 JMSException jmse = new MessageFormatException ("Unable to read from Stream"); 268 jmse.setLinkedException(e); 269 throw jmse; 270 } 271 return retval; 272 } 273 274 public long readLong() throws JMSException 275 { 276 if (!this.readOnly){ 277 JMSException jmse = new MessageNotReadableException ("This Message is open for writing only"); 278 throw jmse; 279 } 280 long retval; 281 try { 282 retval = dataInStream.readLong(); 283 } catch (EOFException e) { 284 JMSException jmse = new MessageEOFException ("End of Message Reached"); 285 jmse.setLinkedException(e); 286 throw jmse; 287 } catch (IOException e) { 288 JMSException jmse = new MessageFormatException ("Unable to read from Stream"); 289 jmse.setLinkedException(e); 290 throw jmse; 291 } 292 return retval; 293 } 294 295 public float readFloat() throws JMSException 296 { 297 if (!this.readOnly){ 298 JMSException jmse = new MessageNotReadableException ("This Message is open for writing only"); 299 throw jmse; 300 } 301 float retval; 302 try { 303 retval = dataInStream.readFloat(); 304 } catch (EOFException e) { 305 JMSException jmse = new MessageEOFException ("End of Message Reached"); 306 jmse.setLinkedException(e); 307 throw jmse; 308 } catch (IOException e) { 309 JMSException jmse = new MessageFormatException ("Unable to read from Stream"); 310 jmse.setLinkedException(e); 311 throw jmse; 312 } 313 return retval; 314 } 315 316 public double readDouble() throws JMSException 317 { 318 319 if (!this.readOnly){ 320 JMSException jmse = new MessageNotReadableException ("This Message is open for writing only"); 321 throw jmse; 322 } 323 double retval; 324 try { 325 retval = dataInStream.readDouble(); 326 } catch (EOFException e) { 327 JMSException jmse = new MessageEOFException ("End of Message Reached"); 328 jmse.setLinkedException(e); 329 throw jmse; 330 } catch (IOException e) { 331 JMSException jmse = new MessageFormatException ("Unable to read from Stream"); 332 jmse.setLinkedException(e); 333 throw jmse; 334 } 335 return retval; 336 } 337 338 public String readUTF() throws JMSException 339 { 340 if (!this.readOnly){ 341 JMSException jmse = new MessageNotReadableException ("This Message is open for writing only"); 342 throw jmse; 343 } 344 String retval; 345 try { 346 retval = dataInStream.readUTF(); 347 } catch (EOFException e) { 348 JMSException jmse = new MessageEOFException ("End of Message Reached"); 349 jmse.setLinkedException(e); 350 throw jmse; 351 } catch (IOException e) { 352 JMSException jmse = new MessageFormatException ("Unable to read from Stream"); 353 jmse.setLinkedException(e); 354 throw jmse; 355 } 356 return retval; 357 } 358 359 public void writeByte(byte value) throws JMSException 360 { 361 checkWrite(); 362 363 try { 364 dataOutStream.writeByte(value); 365 } catch (IOException e) { 366 JMSException jmse = new MessageFormatException ("Unable to write to Stream"); 367 jmse.setLinkedException(e); 368 throw jmse; 369 } 370 } 371 372 public void writeBytes(byte [] value) throws JMSException 373 { 374 checkWrite(); 375 376 try { 377 dataOutStream.write(value, 0, value.length); 378 } catch (IOException e) { 379 JMSException jmse = new MessageFormatException ("Unable to write to Stream"); 380 jmse.setLinkedException(e); 381 throw jmse; 382 } 383 } 384 385 public void writeBytes(byte [] value, int offset, int length) 386 throws JMSException 387 { 388 checkWrite(); 389 390 try { 391 dataOutStream.write(value, offset, length); 392 } catch (IOException e) { 393 JMSException jmse = new MessageFormatException ("Unable to write to Stream"); 394 jmse.setLinkedException(e); 395 throw jmse; 396 } 397 } 398 399 public void writeBoolean(boolean value) throws JMSException 400 { 401 checkWrite(); 402 403 try { 404 dataOutStream.writeBoolean(value); 405 } catch (IOException e) { 406 JMSException jmse = new MessageFormatException ("Unable to write to Stream"); 407 jmse.setLinkedException(e); 408 throw jmse; 409 } 410 } 411 412 public void writeChar(char value) throws JMSException 413 { 414 checkWrite(); 415 416 try { 417 dataOutStream.writeChar(value); 418 } catch (IOException e) { 419 JMSException jmse = new MessageFormatException ("Unable to write to Stream"); 420 jmse.setLinkedException(e); 421 throw jmse; 422 } 423 } 424 425 public void writeShort(short value) throws JMSException 426 { 427 checkWrite(); 428 429 try { 430 dataOutStream.writeShort(value); 431 } catch (IOException e) { 432 JMSException jmse = new MessageFormatException ("Unable to write to Stream"); 433 jmse.setLinkedException(e); 434 throw jmse; 435 } 436 } 437 438 public void writeInt(int value) throws JMSException 439 { 440 checkWrite(); 441 442 try { 443 dataOutStream.writeInt(value); 444 } catch (IOException e) { 445 JMSException jmse = new MessageFormatException ("Unable to write to Stream"); 446 jmse.setLinkedException(e); 447 throw jmse; 448 } 449 } 450 451 public void writeLong(long value) throws JMSException 452 { 453 checkWrite(); 454 455 try { 456 dataOutStream.writeLong(value); 457 } catch (IOException e) { 458 JMSException jmse = new MessageFormatException ("Unable to write to Stream"); 459 jmse.setLinkedException(e); 460 throw jmse; 461 } 462 } 463 464 public void writeFloat(float value) throws JMSException 465 { 466 checkWrite(); 467 468 try { 469 dataOutStream.writeFloat(value); 470 } catch (IOException e) { 471 JMSException jmse = new MessageFormatException ("Unable to write to Stream"); 472 jmse.setLinkedException(e); 473 throw jmse; 474 } 475 } 476 477 public void writeDouble(double value) throws JMSException 478 { 479 checkWrite(); 480 481 try { 482 dataOutStream.writeDouble(value); 483 } catch (IOException e) { 484 JMSException jmse = new MessageFormatException ("Unable to write to Stream"); 485 jmse.setLinkedException(e); 486 throw jmse; 487 } 488 } 489 490 public void writeUTF(String value) throws JMSException 491 { 492 checkWrite(); 493 494 try { 495 dataOutStream.writeUTF(value); 496 } catch (IOException e) { 497 JMSException jmse = new MessageFormatException ("Unable to write to Stream"); 498 jmse.setLinkedException(e); 499 throw jmse; 500 } 501 } 502 503 public void writeObject(Object value) throws JMSException 504 { 505 try { 506 if (value instanceof String ) { 507 writeUTF((String )value); 508 } else if (value instanceof Boolean ) { 509 writeBoolean(( (Boolean ) value).booleanValue()); 510 } else if (value instanceof Byte ) { 511 writeByte(( (Byte ) value).byteValue()); 512 } else if (value instanceof Short ) { 513 writeShort(( (Short ) value).shortValue()); 514 } else if (value instanceof Integer ) { 515 writeInt(( (Integer ) value).intValue()); 516 } else if (value instanceof Long ) { 517 writeLong(( (Long ) value).longValue()); 518 } else if (value instanceof Float ) { 519 writeFloat(( (Float ) value).floatValue()); 520 } else if (value instanceof Double ) { 521 writeDouble(( (Double ) value).doubleValue()); 522 } 523 else { 524 throw new MessageFormatException ("Object must be a primitive type"); 525 } 526 } catch (Exception e) { 527 JMSException jmse = new MessageFormatException ("Unable to write to Stream"); 528 jmse.setLinkedException(e); 529 throw jmse; 530 } 531 } 532 533 public void reset() throws JMSException { 534 535 this.setReadOnly(); 536 537 try { 538 byteMsgArray = this.convertDataToByteArray(); 539 } catch (IOException e) { 540 JMSException jmse = new MessageFormatException (""); 541 jmse.setLinkedException(e); 542 throw jmse; 543 } 544 } 545 546 547 550 public void unmarshal(DataInput in) throws IOException 551 { 552 super.unmarshal(in); 556 557 int arraySize = in.readInt(); 558 if (arraySize > 0) { 559 this.byteMsgArray = new byte[arraySize]; 560 in.readFully(byteMsgArray, 0, arraySize); 561 } 562 563 convertByteArrayToData(byteMsgArray); 564 this.setReadOnly(); 565 } 566 567 568 571 public void marshal(DataOutput out) throws IOException 572 { 573 super.marshal(out); 577 578 if (byteMsgArray == null || byteMsgArray.length == 0) 579 out.writeInt(0); 580 else { 581 out.writeInt(byteMsgArray.length); 582 out.write(byteMsgArray); 583 } 584 } 585 586 public void clearBody() throws JMSException 587 { 588 super.checkWrite(); 589 super.clearBody(); 590 this.byteMsgArray = null; 591 } 592 593 597 601 byte getMarshalingID() 602 { 603 return MessageEncoder.BYTE_MESSAGE; 604 } 605 606 607 611 612 protected void finalize() throws Throwable 613 { 614 try{ 615 if (dataInStream != null) 616 dataInStream.close(); 617 if (byteInStream != null) 618 byteInStream.close(); 619 if (byteOutStream != null) 620 byteOutStream.close(); 621 if (dataOutStream != null) 622 dataOutStream.close(); 623 }catch(IOException e){ 624 } 626 } 627 628 629 protected void checkWrite() throws JMSException 630 { 631 super.checkWrite(); 632 633 if (dataOutStream == null) { 634 byteOutStream = new ByteArrayOutputStream (); 635 dataOutStream = new DataOutputStream (byteOutStream); 636 } 637 } 638 639 643 645 private byte[] convertDataToByteArray() 646 throws IOException 647 { 648 dataOutStream.close(); 649 return byteOutStream.toByteArray(); 650 } 651 652 653 655 private void convertByteArrayToData(byte[] byteArray) 656 throws IOException 657 { 658 if (byteArray == null) 659 byteArray = new byte[0]; 660 661 byteInStream = new ByteArrayInputStream (byteArray); 662 dataInStream = new DataInputStream (byteInStream); 663 664 } 665 666 } 667 668 | Popular Tags |