1 21 package com.presumo.jms.message; 22 23 import java.io.Serializable ; 24 import java.io.IOException ; 25 import java.io.EOFException ; 26 27 import java.io.DataInput ; 28 import java.io.DataOutput ; 29 30 import java.io.ByteArrayOutputStream ; 31 import java.io.ByteArrayInputStream ; 32 import java.io.DataOutputStream ; 33 import java.io.DataInputStream ; 34 35 import javax.jms.StreamMessage ; 36 import javax.jms.JMSException ; 37 import javax.jms.MessageFormatException ; 38 import javax.jms.MessageNotReadableException ; 39 import javax.jms.MessageNotWriteableException ; 40 import javax.jms.MessageEOFException ; 41 42 48 public final class JmsStreamMessage extends JmsMessage 49 implements StreamMessage , java.io.Externalizable 50 { 51 52 private byte [] streamMsgArray; 53 private ByteArrayOutputStream byteOutStream; 54 private DataOutputStream dataOutStream; 55 private ByteArrayInputStream byteInStream; 56 private DataInputStream dataInStream; 57 58 59 60 protected static final byte BOOL_TYPE = 0; 61 protected static final byte BYTE_TYPE = 1; 62 protected static final byte SHORT_TYPE = 2; 63 protected static final byte INT_TYPE = 3; 64 protected static final byte LONG_TYPE = 4; 65 protected static final byte FLOAT_TYPE = 5; 66 protected static final byte DOUBLE_TYPE = 6; 67 protected static final byte CHAR_TYPE = 7; 68 protected static final byte BYTES_TYPE = 8; 69 protected static final byte UTF_TYPE = 9; 70 protected static final byte UNBOUND_TYPE = 20; 71 72 protected byte dataType = UNBOUND_TYPE; 73 74 75 79 public JmsStreamMessage() 80 { 81 super(); 82 } 83 84 public JmsStreamMessage(String name) 85 { 86 super(name); 87 } 88 89 public JmsStreamMessage(byte [] msg, int offset, int length) 90 throws IOException 91 { 92 super(msg, offset, length); 93 } 94 98 102 public boolean readBoolean() throws JMSException 103 { 104 105 boolean retval; 106 if (!this.readOnly){ 107 JMSException jmse = new MessageNotReadableException ("This Message is open for writing only"); 108 throw jmse; 109 } 110 111 try { 112 switch(this.nextType()) 113 { 114 case(BOOL_TYPE): 115 retval = dataInStream.readBoolean(); 116 this.popType(); 117 return retval; 118 case(UTF_TYPE): 119 String s = dataInStream.readUTF(); 120 retval = Boolean.valueOf(s).booleanValue(); 121 this.popType(); 122 return retval; 123 default: 124 throw new JMSException ("Incorrect Type Called"); 125 } 126 127 128 129 } catch (EOFException e) { 130 JMSException jmse = new MessageEOFException ("End of Message Reached"); 131 jmse.setLinkedException(e); 132 throw jmse; 133 } catch (IOException e) { 134 JMSException jmse = new MessageFormatException ("Unable to read from Stream"); 135 jmse.setLinkedException(e); 136 throw jmse; 137 } 138 139 140 } 141 142 public byte readByte() throws JMSException 143 { 144 145 byte retval; 146 if (!this.readOnly){ 147 JMSException jmse = new MessageNotReadableException ("This Message is open for writing only"); 148 throw jmse; 149 } 150 151 try { 152 153 switch(this.nextType()) 154 { 155 case(BYTE_TYPE): 156 retval = dataInStream.readByte(); 157 this.popType(); 158 return retval; 159 case(UTF_TYPE): 160 String s = dataInStream.readUTF(); 161 retval = Byte.valueOf(s).byteValue(); 162 this.popType(); 163 return retval; 164 default: 165 throw new JMSException ("Incorrect Type Called"); 166 } 167 168 169 } catch (EOFException e) { 170 JMSException jmse = new MessageEOFException ("End of Message Reached"); 171 jmse.setLinkedException(e); 172 throw jmse; 173 } catch (IOException e) { 174 JMSException jmse = new MessageFormatException ("Unable to read from Stream"); 175 jmse.setLinkedException(e); 176 throw jmse; 177 } 178 179 } 180 181 public int readBytes(byte [] value) throws JMSException 182 { 183 int retval; 184 if (!this.readOnly){ 185 JMSException jmse = new MessageNotReadableException ("This Message is open for writing only"); 186 throw jmse; 187 } 188 189 try { 190 if(this.nextType() != BYTES_TYPE) 191 throw new JMSException ("Incorrect Type Called"); 192 else { 193 retval = dataInStream.read(value); 194 this.popType(); 195 } 196 return retval; 197 } catch (IOException e) { 198 JMSException jmse = new MessageFormatException ("Unable to read from Stream"); 199 jmse.setLinkedException(e); 200 throw jmse; 201 } 202 203 } 204 205 public short readShort() throws JMSException 206 { 207 short retval; 208 209 if (!this.readOnly){ 210 JMSException jmse = new MessageNotReadableException ("This Message is open for writing only"); 211 throw jmse; 212 } 213 214 try { 215 switch(this.nextType()) 216 { 217 case(BYTE_TYPE): 218 retval = dataInStream.readByte(); 219 this.popType(); 220 return retval; 221 case(SHORT_TYPE): 222 retval = dataInStream.readShort(); 223 this.popType(); 224 return retval; 225 case(UTF_TYPE): 226 String s = dataInStream.readUTF(); 227 retval = Short.valueOf(s).shortValue(); 228 this.popType(); 229 return retval; 230 default: 231 throw new JMSException ("Incorrect Type Called"); 232 } 233 234 235 } catch (EOFException e) { 236 JMSException jmse = new MessageEOFException ("End of Message Reached"); 237 jmse.setLinkedException(e); 238 throw jmse; 239 } catch (IOException e) { 240 JMSException jmse = new MessageFormatException ("Unable to read from Stream"); 241 jmse.setLinkedException(e); 242 throw jmse; 243 } 244 245 246 } 247 248 public char readChar() throws JMSException 249 { 250 char retval; 251 252 if (!this.readOnly){ 253 JMSException jmse = new MessageNotReadableException ("This Message is open for writing only"); 254 throw jmse; 255 } 256 257 try { 258 if(this.nextType() != CHAR_TYPE) 259 throw new JMSException ("Incorrect Type Called"); 260 else { 261 retval = dataInStream.readChar(); 262 this.popType(); 263 } 264 return retval; 265 266 } catch (EOFException e) { 267 JMSException jmse = new MessageEOFException ("End of Message Reached"); 268 jmse.setLinkedException(e); 269 throw jmse; 270 } catch (IOException e) { 271 JMSException jmse = new MessageFormatException ("Unable to read from Stream"); 272 jmse.setLinkedException(e); 273 throw jmse; 274 } 275 276 } 277 278 public int readInt() throws JMSException 279 { 280 int retval; 281 282 if (!this.readOnly){ 283 JMSException jmse = new MessageNotReadableException ("This Message is open for writing only"); 284 throw jmse; 285 } 286 287 try { 288 switch(this.nextType()) 289 { 290 case(BYTE_TYPE): 291 retval = dataInStream.readByte(); 292 this.popType(); 293 return retval; 294 case(SHORT_TYPE): 295 retval = dataInStream.readShort(); 296 this.popType(); 297 return retval; 298 case(INT_TYPE): 299 retval = dataInStream.readInt(); 300 this.popType(); 301 return retval; 302 case(UTF_TYPE): 303 String s = dataInStream.readUTF(); 304 retval = Integer.valueOf(s).intValue(); 305 this.popType(); 306 return retval; 307 default: 308 throw new JMSException ("Incorrect Type Called"); 309 } 310 311 312 } catch (EOFException e) { 313 JMSException jmse = new MessageEOFException ("End of Message Reached"); 314 jmse.setLinkedException(e); 315 throw jmse; 316 } catch (IOException e) { 317 JMSException jmse = new MessageFormatException ("Unable to read from Stream"); 318 jmse.setLinkedException(e); 319 throw jmse; 320 } 321 322 323 } 324 325 public long readLong() throws JMSException 326 { 327 long retval; 328 if (!this.readOnly){ 329 JMSException jmse = new MessageNotReadableException ("This Message is open for writing only"); 330 throw jmse; 331 } 332 333 try { 334 switch(this.nextType()) 335 { 336 case(BYTE_TYPE): 337 retval = dataInStream.readByte(); 338 this.popType(); 339 return retval; 340 case(SHORT_TYPE): 341 retval = dataInStream.readShort(); 342 this.popType(); 343 return retval; 344 case(INT_TYPE): 345 retval = dataInStream.readInt(); 346 this.popType(); 347 return retval; 348 case(LONG_TYPE): 349 retval = dataInStream.readLong(); 350 this.popType(); 351 return retval; 352 case(UTF_TYPE): 353 String s = dataInStream.readUTF(); 354 retval = Long.valueOf(s).longValue(); 355 this.popType(); 356 return retval; 357 default: 358 throw new JMSException ("Incorrect Type Called"); 359 } 360 361 362 } catch (EOFException e) { 363 JMSException jmse = new MessageEOFException ("End of Message Reached"); 364 jmse.setLinkedException(e); 365 throw jmse; 366 } catch (IOException e) { 367 JMSException jmse = new MessageFormatException ("Unable to read from Stream"); 368 jmse.setLinkedException(e); 369 throw jmse; 370 } 371 372 373 } 374 375 public float readFloat() throws JMSException 376 { 377 float retval; 378 if (!this.readOnly){ 379 JMSException jmse = new MessageNotReadableException ("This Message is open for writing only"); 380 throw jmse; 381 } 382 383 try { 384 switch(this.nextType()) 385 { 386 case(FLOAT_TYPE): 387 retval = dataInStream.readFloat(); 388 this.popType(); 389 return retval; 390 case(UTF_TYPE): 391 String s = dataInStream.readUTF(); 392 retval = Float.valueOf(s).floatValue(); 393 this.popType(); 394 return retval; 395 default: 396 throw new JMSException ("Incorrect Type Called"); 397 } 398 399 400 } catch (EOFException e) { 401 JMSException jmse = new MessageEOFException ("End of Message Reached"); 402 jmse.setLinkedException(e); 403 throw jmse; 404 } catch (IOException e) { 405 JMSException jmse = new MessageFormatException ("Unable to read from Stream"); 406 jmse.setLinkedException(e); 407 throw jmse; 408 } 409 410 411 } 412 413 public double readDouble() throws JMSException 414 { 415 double retval; 416 if (!this.readOnly){ 417 JMSException jmse = new MessageNotReadableException ("This Message is open for writing only"); 418 throw jmse; 419 } 420 421 try { 422 switch(this.nextType()) 423 { 424 case(FLOAT_TYPE): 425 retval = dataInStream.readFloat(); 426 this.popType(); 427 return retval; 428 case(DOUBLE_TYPE): 429 retval = dataInStream.readDouble(); 430 this.popType(); 431 return retval; 432 case(UTF_TYPE): 433 String s = dataInStream.readUTF(); 434 retval = Double.valueOf(s).doubleValue(); 435 this.popType(); 436 return retval; 437 default: 438 throw new JMSException ("Incorrect Type Called"); 439 } 440 441 442 } catch (EOFException e) { 443 JMSException jmse = new MessageEOFException ("End of Message Reached"); 444 jmse.setLinkedException(e); 445 throw jmse; 446 } catch (IOException e) { 447 JMSException jmse = new MessageFormatException ("Unable to read from Stream"); 448 jmse.setLinkedException(e); 449 throw jmse; 450 } 451 452 453 } 454 455 public String readString() throws JMSException 456 { 457 String retval; 458 if (!this.readOnly){ 459 JMSException jmse = new MessageNotReadableException ("This Message is open for writing only"); 460 throw jmse; 461 } 462 463 try { 464 switch(this.nextType()) 465 { 466 case(BYTE_TYPE): 467 retval = new Byte (dataInStream.readByte()).toString(); 468 this.popType(); 469 return retval; 470 case(SHORT_TYPE): 471 retval = new Short (dataInStream.readShort()).toString(); 472 this.popType(); 473 return retval; 474 case(INT_TYPE): 475 retval = new Integer (dataInStream.readInt()).toString(); 476 this.popType(); 477 return retval; 478 case(LONG_TYPE): 479 retval = new Long (dataInStream.readLong()).toString(); 480 this.popType(); 481 return retval; 482 case(BOOL_TYPE): 483 retval = new Boolean (dataInStream.readBoolean()).toString(); 484 this.popType(); 485 return retval; 486 case(FLOAT_TYPE): 487 retval = new Float (dataInStream.readFloat()).toString(); 488 this.popType(); 489 return retval; 490 case(DOUBLE_TYPE): 491 retval = new Double (dataInStream.readDouble()).toString(); 492 this.popType(); 493 return retval; 494 case(UTF_TYPE): 495 retval = dataInStream.readUTF(); 496 this.popType(); 497 return retval; 498 default: 499 throw new JMSException ("Incorrect Type Called"); 500 } 501 502 503 } catch (EOFException e) { 504 JMSException jmse = new MessageEOFException ("End of Message Reached"); 505 jmse.setLinkedException(e); 506 throw jmse; 507 } catch (IOException e) { 508 JMSException jmse = new MessageFormatException ("Unable to read from Stream"); 509 jmse.setLinkedException(e); 510 throw jmse; 511 } 512 513 514 } 515 516 public Object readObject() throws JMSException 517 { 518 Object retval; 519 if (!this.readOnly){ 520 JMSException jmse = new MessageNotReadableException ("This Message is open for writing only"); 521 throw jmse; 522 } 523 524 try { 525 switch(this.nextType()) 526 { 527 case(BYTE_TYPE): 528 retval = new Byte (dataInStream.readByte()); 529 this.popType(); 530 return retval; 531 case(SHORT_TYPE): 532 retval = new Short (dataInStream.readShort()); 533 this.popType(); 534 return retval; 535 case(INT_TYPE): 536 retval = new Integer (dataInStream.readInt()); 537 this.popType(); 538 return retval; 539 case(LONG_TYPE): 540 retval = new Long (dataInStream.readLong()); 541 this.popType(); 542 return retval; 543 case(BOOL_TYPE): 544 retval = new Boolean (dataInStream.readBoolean()); 545 this.popType(); 546 return retval; 547 case(FLOAT_TYPE): 548 retval = new Float (dataInStream.readFloat()); 549 this.popType(); 550 return retval; 551 case(DOUBLE_TYPE): 552 retval = new Double (dataInStream.readDouble()); 553 this.popType(); 554 return retval; 555 case(UTF_TYPE): 556 retval = dataInStream.readUTF(); 557 this.popType(); 558 return retval; 559 default: 560 throw new JMSException ("Incorrect Type Called"); 561 } 562 563 564 } catch (EOFException e) { 565 JMSException jmse = new MessageEOFException ("End of Message Reached"); 566 jmse.setLinkedException(e); 567 throw jmse; 568 } catch (IOException e) { 569 JMSException jmse = new MessageFormatException ("Unable to read from Stream"); 570 jmse.setLinkedException(e); 571 throw jmse; 572 } 573 574 } 575 576 public void writeBoolean(boolean value) throws JMSException 577 { 578 checkWrite(); 579 580 try { 581 dataOutStream.writeByte(BOOL_TYPE); 582 dataOutStream.writeBoolean(value); 583 } catch (IOException e) { 584 JMSException jmse = new MessageFormatException ("Unable to write to Stream"); 585 jmse.setLinkedException(e); 586 throw jmse; 587 } 588 } 589 590 public void writeByte(byte value) throws JMSException 591 { 592 checkWrite(); 593 594 try { 595 dataOutStream.writeByte(BYTE_TYPE); 596 dataOutStream.writeByte(value); 597 } catch (IOException e) { 598 JMSException jmse = new MessageFormatException ("Unable to write to Stream"); 599 jmse.setLinkedException(e); 600 throw jmse; 601 } 602 } 603 604 public void writeBytes(byte[] value) throws JMSException 605 { 606 checkWrite(); 607 608 try { 609 dataOutStream.writeByte(BYTES_TYPE); 610 dataOutStream.write(value, 0, value.length); 611 } catch (IOException e) { 612 JMSException jmse = new MessageFormatException ("Unable to write to Stream"); 613 jmse.setLinkedException(e); 614 throw jmse; 615 } 616 } 617 618 public void writeBytes(byte[] value, int offset, int length) 619 throws JMSException 620 { 621 checkWrite(); 622 623 if (this.readOnly){ 624 JMSException jmse = new MessageNotWriteableException ("This Message is readOnly"); 625 throw jmse; 626 } 627 628 try { 629 dataOutStream.writeByte(BYTES_TYPE); 630 dataOutStream.write(value, offset, length); 631 } catch (IOException e) { 632 JMSException jmse = new MessageFormatException ("Unable to write to Stream"); 633 jmse.setLinkedException(e); 634 throw jmse; 635 } 636 } 637 638 public void writeShort(short value) throws JMSException 639 { 640 checkWrite(); 641 642 try { 643 dataOutStream.writeByte(SHORT_TYPE); 644 dataOutStream.writeShort(value); 645 } catch (IOException e) { 646 JMSException jmse = new MessageFormatException ("Unable to write to Stream"); 647 jmse.setLinkedException(e); 648 throw jmse; 649 } 650 } 651 652 public void writeChar(char value) throws JMSException 653 { 654 checkWrite(); 655 656 try { 657 dataOutStream.writeByte(CHAR_TYPE); 658 dataOutStream.writeChar(value); 659 } catch (IOException e) { 660 JMSException jmse = new MessageFormatException ("Unable to write to Stream"); 661 jmse.setLinkedException(e); 662 throw jmse; 663 } 664 } 665 666 public void writeInt(int value) throws JMSException 667 { 668 checkWrite(); 669 670 try { 671 dataOutStream.writeByte(INT_TYPE); 672 dataOutStream.writeInt(value); 673 } catch (IOException e) { 674 JMSException jmse = new MessageFormatException ("Unable to write to Stream"); 675 jmse.setLinkedException(e); 676 throw jmse; 677 } 678 } 679 680 public void writeLong(long value) throws JMSException 681 { 682 checkWrite(); 683 684 try { 685 dataOutStream.writeByte(LONG_TYPE); 686 dataOutStream.writeLong(value); 687 } catch (IOException e) { 688 JMSException jmse = new MessageFormatException ("Unable to write to Stream"); 689 jmse.setLinkedException(e); 690 throw jmse; 691 } 692 } 693 694 public void writeFloat(float value) throws JMSException 695 { 696 checkWrite(); 697 698 try { 699 dataOutStream.writeByte(FLOAT_TYPE); 700 dataOutStream.writeFloat(value); 701 } catch (IOException e) { 702 JMSException jmse = new MessageFormatException ("Unable to write to Stream"); 703 jmse.setLinkedException(e); 704 throw jmse; 705 } 706 } 707 708 public void writeDouble(double value) throws JMSException 709 { 710 checkWrite(); 711 712 try { 713 dataOutStream.writeByte(DOUBLE_TYPE); 714 dataOutStream.writeDouble(value); 715 } catch (IOException e) { 716 JMSException jmse = new MessageFormatException ("Unable to write to Stream"); 717 jmse.setLinkedException(e); 718 throw jmse; 719 } 720 } 721 722 public void writeString(String value) throws JMSException 723 { 724 checkWrite(); 725 726 try { 727 dataOutStream.writeByte(UTF_TYPE); 728 dataOutStream.writeUTF(value); 729 } catch (IOException e) { 730 JMSException jmse = new MessageFormatException ("Unable to write to Stream"); 731 jmse.setLinkedException(e); 732 throw jmse; 733 } 734 } 735 736 public void writeObject(Object value) throws JMSException 737 { 738 checkWrite(); 739 740 try { 741 if (value instanceof String ) { 742 writeString((String )value); 743 } else if (value instanceof Boolean ) { 744 writeBoolean(( (Boolean ) value).booleanValue()); 745 } else if (value instanceof Byte ) { 746 writeByte(( (Byte ) value).byteValue()); 747 } else if (value instanceof Short ) { 748 writeShort(( (Short ) value).shortValue()); 749 } else if (value instanceof Integer ) { 750 writeInt(( (Integer ) value).intValue()); 751 } else if (value instanceof Long ) { 752 writeLong(( (Long ) value).longValue()); 753 } else if (value instanceof Float ) { 754 writeFloat(( (Float ) value).floatValue()); 755 } else if (value instanceof Double ) { 756 writeDouble(( (Double ) value).doubleValue()); 757 } 758 else { 759 throw new MessageFormatException ("Object must be a primitive type"); 760 } 761 } catch (MessageFormatException e) { 762 JMSException jmse = new MessageFormatException ("Unable to write to Stream"); 763 jmse.setLinkedException(e); 764 throw jmse; 765 } 766 } 767 768 769 public void reset() throws JMSException 770 { 771 this.setReadOnly(); 772 } 773 774 775 778 public void unmarshal(DataInput in) throws IOException 779 { 780 super.unmarshal(in); 784 785 int arraySize = in.readInt(); 786 if (arraySize > 0) { 787 this.streamMsgArray = new byte[arraySize]; 788 in.readFully(streamMsgArray, 0, arraySize); 789 convertByteArrayToData(streamMsgArray); 790 } 791 792 this.setReadOnly(); 793 } 794 795 798 public void marshal(DataOutput out) throws IOException 799 { 800 super.marshal(out); 804 805 this.streamMsgArray = this.convertDataToByteArray(); 806 807 if (streamMsgArray == null || streamMsgArray.length == 0) 808 out.writeInt(0); 809 else { 810 out.writeInt(streamMsgArray.length); 811 out.write(streamMsgArray); 812 } 813 } 814 815 public void clearBody() throws JMSException 816 { 817 checkWrite(); 818 super.clearBody(); 819 this.streamMsgArray = null; 820 } 821 822 823 827 828 832 byte getMarshalingID() 833 { 834 return MessageEncoder.STREAM_MESSAGE; 835 } 836 837 838 842 protected byte nextType() throws JMSException , IOException 843 { 844 byte retval; 845 846 if(dataType == UNBOUND_TYPE) { 847 if (dataInStream == null) 848 throw new MessageEOFException ("End of Message Reached"); 849 retval = dataInStream.readByte(); 850 } 851 else 852 retval = dataType; 853 854 return retval; 855 856 } 857 858 protected void popType() throws IOException 859 { 860 dataType = dataInStream.readByte(); 861 } 862 863 protected void finalize() throws Throwable 864 { 865 try{ 866 dataInStream.close(); 867 byteInStream.close(); 868 byteOutStream.close(); 869 dataOutStream.close(); 870 }catch(IOException e){ 871 } 873 } 874 878 protected void checkWrite() throws JMSException 879 { 880 super.checkWrite(); 881 882 if (dataOutStream == null) { 883 byteOutStream = new ByteArrayOutputStream (); 884 dataOutStream = new DataOutputStream (byteOutStream); 885 } 886 } 887 888 889 private byte[] convertDataToByteArray() 890 throws IOException 891 { 892 if (dataOutStream == null) 893 return null; 894 895 dataOutStream.close(); 896 return byteOutStream.toByteArray(); 897 } 898 899 900 904 private void convertByteArrayToData(byte[] byteArray) 905 throws IOException 906 { 907 if (byteArray.length != 0){ 908 byteInStream = new ByteArrayInputStream (byteArray); 909 dataInStream = new DataInputStream (byteInStream); 910 } 911 } 912 913 } 914 915 | Popular Tags |