1 6 7 package org.jfox.jms.message; 8 9 import java.io.ByteArrayInputStream ; 10 import java.io.ByteArrayOutputStream ; 11 import java.io.DataInputStream ; 12 import java.io.DataOutputStream ; 13 import java.io.EOFException ; 14 import java.io.IOException ; 15 import java.io.Serializable ; 16 import javax.jms.JMSException ; 17 import javax.jms.MessageEOFException ; 18 import javax.jms.MessageFormatException ; 19 import javax.jms.MessageNotReadableException ; 20 import javax.jms.MessageNotWriteableException ; 21 import javax.jms.StreamMessage ; 22 23 24 35 36 public class StreamMessageImpl 37 extends JMSMessage 38 implements StreamMessage , Serializable { 39 40 public static final byte TYPE_NULL = 0; 41 public static final byte TYPE_BOOLEAN = 1; 42 public static final byte TYPE_BYTE = 2; 43 public static final byte TYPE_BYTE_ARRAY = 3; 44 public static final byte TYPE_SHORT = 4; 45 public static final byte TYPE_CHAR = 5; 46 public static final byte TYPE_INT = 6; 47 public static final byte TYPE_LONG = 7; 48 public static final byte TYPE_FLOAT = 8; 49 public static final byte TYPE_DOUBLE = 9; 50 public static final byte TYPE_STRING = 10; 51 52 private transient DataOutputStream dos = null; 53 private transient DataInputStream dis = null; 54 private transient ByteArrayOutputStream baos = null; 55 private transient ByteArrayInputStream bais = null; 56 private byte[] buffer = null; 57 private int readBytes = 0; 58 private int byteArrayLength = 0; 59 60 private static final String [] TYPE_NAMES = 61 { 62 "null", 63 "boolean", 64 "byte", 65 "byte[]", 66 "short", 67 "char", 68 "int", 69 "long", 70 "float", 71 "double", 72 "String"}; 73 74 77 public StreamMessageImpl() { 78 super(); 79 buffer = new byte[0]; 80 baos = new ByteArrayOutputStream (); 81 dos = new DataOutputStream (baos); 82 } 83 84 97 public boolean readBoolean() throws JMSException { 98 prepareRead(); 99 try { 100 return Conversions.getBoolean(readNext()); 101 } catch (MessageFormatException e) { 102 try { 103 dis.reset(); 104 } catch (IOException ex) { 105 throw new JMSException (ex.getMessage()); 106 } 107 throw new MessageFormatException (e.getMessage()); 108 } 109 } 110 111 124 public byte readByte() throws JMSException { 125 prepareRead(); 126 try { 127 return Conversions.getByte(readNext()); 128 } catch (MessageFormatException e) { 129 try { 130 dis.reset(); 131 } catch (IOException ex) { 132 throw new JMSException (ex.getMessage()); 133 } 134 throw new MessageFormatException (e.getMessage()); 135 136 } catch (NumberFormatException e) { 137 try { 138 dis.reset(); 139 } catch (IOException ex) { 140 throw new JMSException (ex.getMessage()); 141 } 142 throw new JMSException (e.getMessage()); 143 } 144 } 145 146 159 public short readShort() throws JMSException { 160 prepareRead(); 161 try { 162 return Conversions.getShort(readNext()); 163 } catch (MessageFormatException e) { 164 try { 165 dis.reset(); 166 } catch (IOException ex) { 167 throw new JMSException (ex.getMessage()); 168 } 169 throw new MessageFormatException (e.getMessage()); 170 } catch (NumberFormatException e) { 171 try { 172 dis.reset(); 173 } catch (IOException ex) { 174 throw new JMSException (ex.getMessage()); 175 } 176 throw new JMSException (e.getMessage()); 177 } 178 } 179 180 193 public char readChar() throws JMSException { 194 prepareRead(); 195 try { 196 return Conversions.getChar(readNext()); 197 } catch (MessageFormatException e) { 198 try { 199 dis.reset(); 200 } catch (IOException ex) { 201 throw new JMSException (ex.getMessage()); 202 } 203 throw new MessageFormatException (e.getMessage()); 204 } catch (NullPointerException e) { 205 try { 206 dis.reset(); 207 } catch (IOException ex) { 208 throw new JMSException (ex.getMessage()); 209 } 210 throw new JMSException (e.getMessage()); 211 } 212 } 213 214 228 public int readInt() throws JMSException { 229 prepareRead(); 230 try { 231 return Conversions.getInt(readNext()); 232 } catch (MessageFormatException e) { 233 try { 234 dis.reset(); 235 } catch (IOException ex) { 236 throw new JMSException (ex.getMessage()); 237 } 238 throw new MessageFormatException (e.getMessage()); 239 } catch (NumberFormatException e) { 240 try { 241 dis.reset(); 242 } catch (IOException ex) { 243 throw new JMSException (ex.getMessage()); 244 } 245 throw new JMSException (e.getMessage()); 246 } 247 } 248 249 263 public long readLong() throws JMSException { 264 prepareRead(); 265 try { 266 return Conversions.getLong(readNext()); 267 } catch (MessageFormatException e) { 268 try { 269 dis.reset(); 270 } catch (IOException ex) { 271 throw new JMSException (ex.getMessage()); 272 } 273 throw new MessageFormatException (e.getMessage()); 274 } catch (NumberFormatException e) { 275 try { 276 dis.reset(); 277 } catch (IOException ex) { 278 throw new JMSException (ex.getMessage()); 279 } 280 throw new JMSException (e.getMessage()); 281 } 282 } 283 284 297 public float readFloat() throws JMSException { 298 prepareRead(); 299 try { 300 return Conversions.getFloat(readNext()); 301 } catch (MessageFormatException e) { 302 try { 303 dis.reset(); 304 } catch (IOException ex) { 305 throw new JMSException (ex.getMessage()); 306 } 307 throw new MessageFormatException (e.getMessage()); 308 } catch (NullPointerException e) { 309 try { 310 dis.reset(); 311 } catch (IOException ex) { 312 throw new JMSException (ex.getMessage()); 313 } 314 throw new JMSException (e.getMessage()); 315 } catch (NumberFormatException e) { 316 try { 317 dis.reset(); 318 } catch (IOException ex) { 319 throw new JMSException (ex.getMessage()); 320 } 321 throw new JMSException (e.getMessage()); 322 } 323 } 324 325 338 public double readDouble() throws JMSException { 339 prepareRead(); 340 try { 341 return Conversions.getDouble(readNext()); 342 } catch (MessageFormatException e) { 343 try { 344 dis.reset(); 345 } catch (IOException ex) { 346 throw new JMSException (ex.getMessage()); 347 } 348 throw new MessageFormatException (e.getMessage()); 349 } catch (NullPointerException e) { 350 try { 351 dis.reset(); 352 } catch (IOException ex) { 353 throw new JMSException (ex.getMessage()); 354 } 355 throw new JMSException (e.getMessage()); 356 } catch (NumberFormatException e) { 357 try { 358 dis.reset(); 359 } catch (IOException ex) { 360 throw new JMSException (ex.getMessage()); 361 } 362 throw new JMSException (e.getMessage()); 363 } 364 } 365 366 379 public String readString() throws JMSException { 380 prepareRead(); 381 try { 382 return Conversions.getString(readNext()); 383 } catch (MessageFormatException e) { 384 try { 385 dis.reset(); 386 } catch (IOException ex) { 387 throw new JMSException (ex.getMessage()); 388 } 389 throw new MessageFormatException (e.getMessage()); 390 } 391 } 392 393 440 public int readBytes(byte[] value) throws JMSException { 441 if (isBodyModifiable()) { 442 throw new MessageNotReadableException ("StreamMessage write_only"); 443 } 444 getInputStream(); 445 int read = 0; if (readBytes == 0) { 447 try { 448 dis.mark(buffer.length - dis.available()); 449 byte type = (byte) (dis.readByte() & 0x0F); 450 if (type == TYPE_NULL) { 451 return -1; 452 } else if (type != TYPE_BYTE_ARRAY) { 453 dis.reset(); 454 if (type < TYPE_NAMES.length) { 455 throw new MessageFormatException ("Expected type=" 456 + TYPE_NAMES[TYPE_BYTE_ARRAY] 457 + ", but got type=" 458 + TYPE_NAMES[type]); 459 } else { 460 throw new MessageFormatException ("StreamMessage corrupted"); 461 } 462 } 463 } catch (IOException e) { 464 throw new JMSException (e.getMessage()); 465 } 466 try { 467 byteArrayLength = dis.readInt(); 468 } catch (IOException e) { 469 throw new JMSException (e.getMessage()); 470 } 471 } 472 if (byteArrayLength == 0) { 473 if (readBytes != 0) { read = -1; 475 } 476 readBytes = 0; } else { 478 ++readBytes; 479 try { 480 if (value.length <= byteArrayLength) { 481 read = value.length; 482 dis.readFully(value); 483 byteArrayLength -= value.length; 484 } else { 485 read = byteArrayLength; 486 dis.readFully(value, 0, byteArrayLength); 487 byteArrayLength = 0; 488 } 489 } catch (IOException e) { 490 throw new JMSException (e.getMessage()); 491 } 492 } 493 return read; 494 } 495 496 528 public Object readObject() throws JMSException { 529 prepareRead(); 530 try { 531 return readNext(); 532 } catch (MessageFormatException e) { 533 try { 534 dis.reset(); 535 } catch (IOException ex) { 536 throw new JMSException (ex.getMessage()); 537 } 538 throw new MessageFormatException (e.getMessage()); 539 } 540 } 541 542 554 public void writeBoolean(boolean value) throws JMSException { 555 if (!isBodyModifiable()) { 556 throw new MessageNotWriteableException ("StreamMessage read_only"); 557 } 558 559 try { 560 dos.writeByte((int) TYPE_BOOLEAN); 561 dos.writeBoolean(value); 562 } catch (IOException e) { 563 throw new JMSException (e.getMessage()); 564 } 565 } 566 567 577 public void writeByte(byte value) throws JMSException { 578 if (!isBodyModifiable()) { 579 throw new MessageNotWriteableException ("StreamMessage read_only"); 580 } 581 582 try { 583 dos.writeByte((int) TYPE_BYTE); 584 dos.writeByte((int) value); 585 } catch (IOException e) { 586 throw new JMSException (e.getMessage()); 587 } 588 } 589 590 600 public void writeShort(short value) throws JMSException { 601 if (!isBodyModifiable()) { 602 throw new MessageNotWriteableException ("StreamMessage read_only"); 603 } 604 605 try { 606 dos.writeByte((int) TYPE_SHORT); 607 dos.writeShort((int) value); 608 } catch (IOException e) { 609 throw new JMSException (e.getMessage()); 610 } 611 } 612 613 623 public void writeChar(char value) throws JMSException { 624 if (!isBodyModifiable()) { 625 throw new MessageNotWriteableException ("StreamMessage read_only"); 626 } 627 628 try { 629 dos.writeByte((int) TYPE_CHAR); 630 dos.writeChar((int) value); 631 } catch (IOException e) { 632 throw new JMSException (e.getMessage()); 633 } 634 } 635 636 646 public void writeInt(int value) throws JMSException { 647 if (!isBodyModifiable()) { 648 throw new MessageNotWriteableException ("StreamMessage read_only"); 649 } 650 651 try { 652 dos.writeByte((int) TYPE_INT); 653 dos.writeInt(value); 654 } catch (IOException e) { 655 throw new JMSException (e.getMessage()); 656 } 657 } 658 659 669 public void writeLong(long value) throws JMSException { 670 if (!isBodyModifiable()) { 671 throw new MessageNotWriteableException ("StreamMessage read_only"); 672 } 673 674 try { 675 dos.writeByte((int) TYPE_LONG); 676 dos.writeLong(value); 677 } catch (IOException e) { 678 throw new JMSException (e.getMessage()); 679 } 680 } 681 682 692 public void writeFloat(float value) throws JMSException { 693 if (!isBodyModifiable()) { 694 throw new MessageNotWriteableException ("StreamMessage read_only"); 695 } 696 697 try { 698 dos.writeByte((int) TYPE_FLOAT); 699 dos.writeFloat(value); 700 } catch (IOException e) { 701 throw new JMSException (e.getMessage()); 702 } 703 } 704 705 715 public void writeDouble(double value) throws JMSException { 716 if (!isBodyModifiable()) { 717 throw new MessageNotWriteableException ("StreamMessage read_only"); 718 } 719 720 try { 721 dos.writeByte((int) TYPE_DOUBLE); 722 dos.writeDouble(value); 723 } catch (IOException e) { 724 throw new JMSException (e.getMessage()); 725 } 726 } 727 728 738 public void writeString(String value) throws JMSException { 739 if (!isBodyModifiable()) { 740 throw new MessageNotWriteableException ("StreamMessage read_only"); 741 } 742 743 try { 744 if (value != null) { 745 dos.writeByte((int) TYPE_STRING); 746 dos.writeUTF(value); 747 } else { 748 throw new JMSException ("Value is null"); 749 } 750 } catch (IOException e) { 751 throw new JMSException (e.getMessage()); 752 } 753 } 754 755 770 public void writeBytes(byte[] value) throws JMSException { 771 writeBytes(value, 0, value.length); 772 } 773 774 792 public void writeBytes(byte[] value, int offset, int length) 793 throws JMSException { 794 if (!isBodyModifiable()) { 795 throw new MessageNotWriteableException ("StreamMessage read_only"); 796 } 797 798 try { 799 if (value != null) { 800 dos.writeByte((int) TYPE_BYTE_ARRAY); 801 dos.writeInt(length); 802 dos.write(value, offset, length); 803 } else { 804 throw new JMSException ("Value is null"); 805 } 806 } catch (IOException e) { 807 throw new JMSException (e.getMessage()); 808 } 809 } 810 811 828 public void writeObject(Object value) throws JMSException { 829 if (value == null) { 830 try { 831 if (!isBodyModifiable()) { 832 throw new MessageNotWriteableException ("StreamMessage read_only"); 833 } 834 getOutputStream(); 835 dos.writeByte(TYPE_NULL); 836 } catch (IOException e) { 837 throw new JMSException (e.getMessage()); 838 } 839 } else if (value instanceof Boolean ) { 840 writeBoolean(((Boolean ) value).booleanValue()); 841 } else if (value instanceof Byte ) { 842 writeByte(((Byte ) value).byteValue()); 843 } else if (value instanceof byte[]) { 844 writeBytes((byte[]) value); 845 } else if (value instanceof Short ) { 846 writeShort(((Short ) value).shortValue()); 847 } else if (value instanceof Character ) { 848 writeChar(((Character ) value).charValue()); 849 } else if (value instanceof Integer ) { 850 writeInt(((Integer ) value).intValue()); 851 } else if (value instanceof Long ) { 852 writeLong(((Long ) value).longValue()); 853 } else if (value instanceof Float ) { 854 writeFloat(((Float ) value).floatValue()); 855 } else if (value instanceof Double ) { 856 writeDouble(((Double ) value).doubleValue()); 857 } else if (value instanceof String ) { 858 writeString((String ) value); 859 } else { 860 throw new MessageFormatException ("StreamMessag invalid_type"); 861 } 862 } 863 864 867 public void reset() throws JMSException { 868 try { 869 if (!isBodyModifiable()) { 870 setBodyModifiable(true); 871 if (dos != null) { 872 dos.flush(); 873 buffer = baos.toByteArray(); 874 baos = null; 875 dos.close(); 876 dos = null; 877 } 878 } else { 879 if (dis != null) { 880 bais = null; 881 dis.close(); 882 dis = null; 883 } 884 } 885 readBytes = 0; 886 } catch (IOException e) { 887 if (e instanceof EOFException ) { 888 throw new MessageEOFException (e.getMessage()); 889 } else { 890 throw new JMSException (e.getMessage()); 891 } 892 } 893 } 894 895 901 public void clearBody() throws JMSException { 902 super.clearBody(); 903 buffer = new byte[0]; 904 baos = new ByteArrayOutputStream (); 905 dos = new DataOutputStream (baos); 906 bais = null; 907 dis = null; 908 } 909 910 924 private Object readNext() throws JMSException { 925 if (readBytes != 0) { 926 throw new MessageFormatException ("Must finish reading byte array before reading next field"); 927 } 928 929 byte type = 0; 930 try { 931 type = dis.readByte(); 932 } catch (IOException e) { 933 throw new JMSException (e.getMessage()); 934 } 935 if ((type & 0x0F) > TYPE_NAMES.length) { 936 throw new JMSException ("StreamMessage corrupted"); 937 } 938 Object result = null; 939 940 try { 941 switch (type & 0x0F) { 942 case TYPE_BOOLEAN: 943 boolean value = ((type & 0xF0) != 0) ? true : false; 944 result = new Boolean (value); 945 break; 946 case TYPE_BYTE: 947 result = new Byte (dis.readByte()); 948 break; 949 case TYPE_BYTE_ARRAY: 950 int length = dis.readInt(); 951 byte[] bytes = new byte[length]; 952 dis.readFully(bytes); 953 result = bytes; 954 break; 955 case TYPE_SHORT: 956 result = new Short (dis.readShort()); 957 break; 958 case TYPE_CHAR: 959 result = new Character (dis.readChar()); 960 break; 961 case TYPE_INT: 962 result = new Integer (dis.readInt()); 963 break; 964 case TYPE_LONG: 965 result = new Long (dis.readLong()); 966 break; 967 case TYPE_FLOAT: 968 result = new Float (dis.readFloat()); 969 break; 970 case TYPE_DOUBLE: 971 result = new Double (dis.readDouble()); 972 break; 973 case TYPE_STRING: 974 result = dis.readUTF(); 975 break; 976 } 977 } catch (IOException e) { 978 throw new JMSException (e.getMessage()); 979 } 980 return result; 981 } 982 983 private void prepareRead() throws JMSException { 984 if (isBodyModifiable()) { 985 throw new MessageNotReadableException ("StreamMessage write_only"); 986 } 987 getInputStream(); 988 try { 989 dis.mark(buffer.length - dis.available()); 990 } catch (IOException e) { 991 throw new JMSException (e.getMessage()); 992 } 993 } 994 995 1000 private DataInputStream getInputStream() { 1001 if (dis == null) { 1002 bais = new ByteArrayInputStream (buffer); 1003 dis = new DataInputStream (bais); 1004 } 1005 return dis; 1006 } 1007 1008 1014 private DataOutputStream getOutputStream() throws IOException { 1015 if (dos == null) { 1016 baos = new ByteArrayOutputStream (); 1017 dos = new DataOutputStream (baos); 1018 dos.write(buffer); 1019 } 1020 return dos; 1021 } 1022 1023} 1024 | Popular Tags |