1 46 47 package org.mr.api.jms; 48 86 87 88 import java.io.ByteArrayOutputStream ; 89 import java.io.DataOutputStream ; 90 import java.io.EOFException ; 91 import java.io.IOException ; 92 import java.io.UTFDataFormatException ; 93 94 import javax.jms.BytesMessage ; 95 import javax.jms.JMSException ; 96 import javax.jms.MessageEOFException ; 97 import javax.jms.MessageFormatException ; 98 import javax.jms.MessageNotReadableException ; 99 import javax.jms.MessageNotWriteableException ; 100 101 import org.mr.core.util.byteable.Byteable; 102 import org.mr.core.util.byteable.ByteableInputStream; 103 import org.mr.core.util.byteable.ByteableOutputStream; 104 import org.mr.core.util.byteable.ByteableRegistry; 105 106 107 public class MantaBytesMessage extends MantaMessage implements BytesMessage { 108 109 112 private static final long serialVersionUID = 3088174266194469819L; 113 114 protected static final byte[] EMPTY = new byte[]{}; protected byte[] message = EMPTY; 116 protected DataOutputStream outputStream = null; 118 protected ByteArrayOutputStream byteOutputStream = null; 119 protected int internalOffset = 0; 120 121 122 126 public MantaBytesMessage(){ 127 128 } 129 130 136 public MantaBytesMessage(MantaSession sess) throws JMSException { 137 flags = (flags&ONLY_MSG_TYPES) | BYTES_MESSAGE; 138 creatingSession = sess; 139 } 141 150 public final boolean readBoolean() throws JMSException { 151 int result = 0; 152 changeInput(); 154 try { 155 result = read(); 156 } catch (IOException exception) { 158 rollback(exception); 159 } return (result != 0); 161 } 163 164 173 public final byte readByte() throws JMSException { 174 int result = 0; 175 changeInput(); 177 try { 178 result = read(); 179 } catch (IOException exception) { 181 rollback(exception); 182 } return (byte)(result); 184 } 186 187 196 public final int readUnsignedByte() throws JMSException { 197 int result = 0; 198 changeInput(); 200 try { 201 result = read(); 202 } catch (IOException exception) { 204 rollback(exception); 205 } return result; 207 } 209 210 219 public final short readShort() throws JMSException { 220 short result = 0; 221 changeInput(); 223 try { 224 int ch1 = read(); 225 int ch2 = read(); 226 227 result= (short)((ch1 << 8) + (ch2 << 0)); 228 } catch (IOException exception) { 230 rollback(exception); 231 } return result; 233 } 235 236 245 public final int readUnsignedShort() throws JMSException { 246 int result = 0; 247 changeInput(); 249 try { 250 int ch1 = read(); 251 int ch2 = read(); 252 result = (ch1 << 8) + (ch2 << 0); 253 } catch (IOException exception) { 255 rollback(exception); 256 } return result; 258 } 260 261 270 public final char readChar() throws JMSException { 271 char result = 0; 272 changeInput(); 274 try { 275 int ch1 = read(); 276 int ch2 = read(); 277 result= (char)((ch1 << 8) + (ch2 << 0)); 278 } catch (IOException exception) { 280 rollback(exception); 281 } return result; 283 } 285 286 295 public final int readInt() throws JMSException { 296 int result = 0; 297 changeInput(); 299 try { 300 int ch1 = read(); 301 int ch2 = read(); 302 int ch3 = read(); 303 int ch4 = read(); 304 305 result= ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0)); 306 } catch (IOException exception) { 308 rollback(exception); 309 } return result; 311 } 313 314 323 public final long readLong() throws JMSException { 324 long result = 0; 325 changeInput(); 327 328 byte b1= readByte(); 329 byte b2= readByte(); 330 byte b3= readByte(); 331 byte b4= readByte(); 332 byte b5= readByte(); 333 byte b6= readByte(); 334 byte b7= readByte(); 335 byte b8= readByte(); 336 337 result= (((long)b1 << 56) + 338 ((long)(b2 & 255) << 48) + 339 ((long)(b3 & 255) << 40) + 340 ((long)(b4 & 255) << 32) + 341 ((long)(b5 & 255) << 24) + 342 ((b6 & 255) << 16) + 343 ((b7 & 255) << 8) + 344 ((b8 & 255) << 0)); 345 346 347 return result; 348 } 350 351 359 public final float readFloat() throws JMSException { 360 return Float.intBitsToFloat(readInt()); 361 } 363 364 373 public final double readDouble() throws JMSException { 374 375 return Double.longBitsToDouble(readLong()); 376 } 378 379 391 public final String readUTF() throws JMSException { 392 String result = null; 393 changeInput(); 395 try { 396 int utflen = readShort(); 397 if(utflen ==ByteableOutputStream.NULL_PLACE_HOLDER){ 398 return null; 399 } 400 401 StringBuffer str = new StringBuffer (utflen); 402 byte bytearr [] = new byte[utflen]; 403 int c, char2, char3; 404 int count = 0; 405 406 readFully(bytearr, 0, utflen); 407 408 while (count < utflen) { 409 c = bytearr[count] & 0xff; 410 switch (c >> 4) { 411 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: 412 413 count++; 414 str.append((char)c); 415 break; 416 case 12: case 13: 417 418 count += 2; 419 if (count > utflen) 420 throw new UTFDataFormatException (); 421 char2 = bytearr[count-1]; 422 if ((char2 & 0xC0) != 0x80) 423 throw new UTFDataFormatException ( 424 "MNJMS00014 : CORRUPTED UTF FORMAT. FAILED IN METHOD readUTF()."); 425 str.append((char)(((c & 0x1F) << 6) | (char2 & 0x3F))); 426 break; 427 case 14: 428 429 count += 3; 430 if (count > utflen) 431 throw new UTFDataFormatException ("MNJMS00014 : FAILED TO READ UTF. METHOD readUTF()."); 432 char2 = bytearr[count-2]; 433 char3 = bytearr[count-1]; 434 if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)) 435 throw new UTFDataFormatException ("MNJMS00014 : FAILED TO READ UTF. METHOD readUTF()."); 436 str.append((char)(((c & 0x0F) << 12) | 437 ((char2 & 0x3F) << 6) | 438 ((char3 & 0x3F) << 0))); 439 break; 440 default: 441 442 throw new UTFDataFormatException ("MNJMS00014 : FAILED TO READ UTF. METHOD readUTF()."); 443 } 444 result = new String (str); 446 } 447 448 } catch (IOException exception) { 450 rollback(exception); 451 } return result; 453 } 455 456 477 public final int readBytes(byte[] value) throws JMSException { 478 return readBytes(value, value.length); 479 } 481 482 510 public final int readBytes(byte[] value, int length) throws JMSException { 511 int read = -1; 512 changeInput(); 514 515 if (length < 0 || length > value.length) { 517 throw new IndexOutOfBoundsException ("MNJMS00001 : INVALID LENGTH SUPPLIED FOR READ OPERATION."); 518 } 519 try { 521 522 if (internalOffset < message.length) { 523 524 if(length <= message.length-internalOffset){ 525 read = length; 526 }else{ 527 read = length-internalOffset; 528 } 529 readFully(value, 0, read); 530 531 } } catch (EOFException doNothing) { 534 } catch (IOException exception) { 536 rollback(exception); 537 } return read; 539 } 541 542 552 public final void writeBoolean(boolean value) throws JMSException { 553 checkWriteableState(false); 554 try { 555 getOutputStream().writeBoolean(value); 556 } catch (IOException exception) { 558 throw new JMSException ("MNJMS00005 : AN IO EXCEPTION OCCURED IN writeBoolean() METHOD. EXCEPTION TEXT : "+ exception.getMessage()); 559 } } 562 563 571 public final void writeByte(byte value) throws JMSException { 572 checkWriteableState(false); 573 try { 574 getOutputStream().writeByte(value); 575 } catch (IOException exception) { 577 throw new JMSException ("MNJMS00006 : AN IO EXCEPTION OCCURED IN writeByte() METHOD. EXCEPTION TEXT : "+ exception.getMessage()); 578 } } 581 582 590 public final void writeShort(short value) throws JMSException { 591 checkWriteableState(false); 592 try { 593 getOutputStream().writeShort(value); 594 } catch (IOException exception) { 596 throw new JMSException ("MNJMS00007 : AN IO EXCEPTION OCCURED IN writeShort() METHOD. EXCEPTION TEXT : "+ exception.getMessage()); 597 } } 600 601 609 public final void writeChar(char value) throws JMSException { 610 checkWriteableState(false); 611 try { 612 getOutputStream().writeChar(value); 613 } catch (IOException exception) { 615 throw new JMSException ("MNJMS00008 : AN IO EXCEPTION OCCURED IN writeChar() METHOD. EXCEPTION TEXT : "+ exception.getMessage()); 616 } } 619 620 628 public final void writeInt(int value) throws JMSException { 629 checkWriteableState(false); 630 try { 631 getOutputStream().writeInt(value); 632 } catch (IOException exception) { 634 throw new JMSException ("MNJMS00009 : AN IO EXCEPTION OCCURED IN writeInt() METHOD. EXCEPTION TEXT : "+ exception.getMessage()); 635 } } 638 639 646 public final void writeLong(long value) throws JMSException { 647 checkWriteableState(false); 648 try { 649 getOutputStream().writeLong(value); 650 } catch (IOException exception) { 652 throw new JMSException ("MNJMS0000A : AN IO EXCEPTION OCCURED IN writeLong() METHOD. EXCEPTION TEXT : "+ exception.getMessage()); 653 } } 656 657 667 public final void writeFloat(float value) throws JMSException { 668 checkWriteableState(false); 669 try { 670 getOutputStream().writeFloat(value); 671 } catch (IOException exception) { 673 throw new JMSException ("MNJMS0000B : AN IO EXCEPTION OCCURED IN writeFloat() METHOD. EXCEPTION TEXT : "+ exception.getMessage()); 674 } } 677 678 689 public final void writeDouble(double value) throws JMSException { 690 checkWriteableState(false); 691 try { 692 getOutputStream().writeDouble(value); 693 } catch (IOException exception) { 695 throw new JMSException ("MNJMS0000C : AN IO EXCEPTION OCCURED IN writeDouble() METHOD. EXCEPTION TEXT : "+ exception.getMessage()); 696 } } 699 700 709 public final void writeUTF(String value) throws JMSException { 710 checkWriteableState(false); 711 try { 712 getOutputStream().writeUTF(value); 713 } catch (IOException exception) { 715 throw new JMSException ("MNJMS0000D : AN IO EXCEPTION OCCURED IN writeUTF() METHOD. EXCEPTION TEXT : "+ exception.getMessage()); 716 } } 719 720 727 public final void writeBytes(byte[] value) throws JMSException { 728 checkWriteableState(false); 729 try { 730 getOutputStream().write(value); 731 } catch (IOException exception) { 733 throw new JMSException ("MNJMS0000E : AN IO EXCEPTION OCCURED IN writeBytes() METHOD. EXCEPTION TEXT : "+ exception.getMessage()); 734 } } 737 738 748 public final void writeBytes(byte[] value, int offset, int length) throws JMSException { 749 checkWriteableState(false); 750 try { 751 getOutputStream().write(value, offset, length); 752 } catch (IOException exception) { 754 throw new JMSException ("MNJMS0000E : AN IO EXCEPTION OCCURED IN writeBytes() METHOD. EXCEPTION TEXT : "+ exception.getMessage()); 755 } } 758 759 772 public final void writeObject(Object value) throws JMSException { 773 checkWriteableState(false); 774 if (value == null) { 776 throw new NullPointerException ("MNJMS00002 : BYTES MESSAGE DOES NOT SUPPORT NULL VALUES."); 777 } 778 else if (value instanceof Boolean ) { 779 writeBoolean(((Boolean ) value).booleanValue()); 780 } else if (value instanceof Byte ) { 782 writeByte(((Byte ) value).byteValue()); 783 } else if (value instanceof Short ) { 785 writeShort(((Short ) value).shortValue()); 786 } else if (value instanceof Character ) { 788 writeChar(((Character ) value).charValue()); 789 } else if (value instanceof Integer ) { 791 writeInt(((Integer ) value).intValue()); 792 } else if (value instanceof Long ) { 794 writeLong(((Long ) value).longValue()); 795 } else if (value instanceof Float ) { 797 writeFloat(((Float ) value).floatValue()); 798 } else if (value instanceof Double ) { 800 writeDouble(((Double ) value).doubleValue()); 801 } else if (value instanceof String ) { 803 writeUTF((String ) value); 804 } else if (value instanceof byte[]) { 806 writeBytes((byte[]) value); 807 } else { 809 throw new MessageFormatException ("MNJMS00003 : OBJECT OF TYPE "+value.getClass().getName()+ 810 " IS UNALLOWED IN BYTES MESSAGE."); 811 812 } } 815 816 822 public final void reset() throws JMSException { 823 824 try { 825 826 if (this.isWriteable()) 827 this.setWriteableState(false); 828 829 if (outputStream != null) { 830 outputStream.flush(); 831 this.message = this.byteOutputStream.toByteArray(); 832 this.byteOutputStream = null; 833 this.outputStream.close(); 834 this.outputStream=null; 835 } 836 internalOffset=0; 837 } catch (IOException exception) { 839 throw new JMSException ("MNJMS0000F : AN IO EXCEPTION OCCURED IN reset() METHOD. EXCEPTION TEXT : "+ exception.getMessage()); 840 } } 843 844 857 public final void clearBody() throws JMSException { 858 try { 859 860 setWriteableState(true); 861 862 if (outputStream != null) { 863 this.byteOutputStream = null; 865 this.outputStream.close(); 866 this.outputStream = null; 867 } this.message = EMPTY; 874 this.internalOffset = 0; 875 this.byteOutputStream = null; 876 this.outputStream = null; 877 } catch (IOException exception) { 879 throw new JMSException ("MNJMS00010 : AN IO EXCEPTION OCCURED IN clearBody() METHOD. EXCEPTION TEXT : "+ exception.getMessage()); 880 } } 883 884 893 private final void rollback(IOException exception) throws JMSException { 894 895 internalOffset = 0; 896 897 JMSException error = null; 898 String throwText = "MNJMS000BB : AN EXCEPTION OCCURED IN METHOD rollback(). EXCEPTION TEXT : "+exception.getMessage(); 899 if (exception instanceof EOFException ) { 900 error = new MessageEOFException (throwText); 901 } else if (exception instanceof UTFDataFormatException ) { 903 error = new MessageFormatException (throwText); 904 } else { 906 error = new JMSException (throwText); 907 } error.setLinkedException(exception); 909 throw error; 910 } 912 913 917 private DataOutputStream getOutputStream() throws IOException , JMSException { 918 919 if (outputStream==null) { 920 byteOutputStream = new ByteArrayOutputStream (); 921 outputStream = new DataOutputStream (this.byteOutputStream); 922 outputStream.write(this.message); 923 } 924 925 return outputStream; 926 } 927 928 930 933 public void toBytes(ByteableOutputStream out) throws IOException { 934 super.toBytes(out); 935 936 if (this.byteOutputStream!=null || message==null) { 937 outputStream.flush(); 938 message =byteOutputStream.toByteArray(); 939 internalOffset=0; 940 byteOutputStream = null; 941 } 942 out.writeInt(message.length); 943 out.write(message); 944 out.writeInt(internalOffset); 946 947 } 948 949 954 955 public Byteable createInstance(ByteableInputStream in) throws IOException { 956 MantaBytesMessage cbm; 957 try { 958 cbm = new MantaBytesMessage(null); 959 960 createHeadersAndProperties(cbm,in); 961 962 byte[] msg = new byte[in.readInt()]; 963 in.read(msg); 964 cbm.message = msg; 965 cbm.internalOffset = in.readInt(); 966 } 967 catch (Exception ee) { 968 throw new IOException ("MNJMS00FFF : METHOD createInstance() FAILED ON MantaBytesMessage. EXCEPTION TEXT : "+ee.getMessage()); 969 } 970 971 return cbm; 972 } 973 974 977 public void registerToByteableRegistry() { 978 ByteableRegistry.registerByteableFactory(getByteableName() , this); 979 } 980 981 982 985 public final static String BYTEABLENAME = "MantaBytesMsg"; 986 public String getByteableName() { 987 988 return BYTEABLENAME; 989 } 990 991 994 public static void register() throws JMSException { 995 MantaBytesMessage instance = new MantaBytesMessage(null); 996 instance.registerToByteableRegistry(); 997 } 998 999 1001 1002 1005 1006 private void changeInput() throws JMSException { 1007 if ((flags&IS_WRITEABLE)>0) 1008 throw new MessageNotReadableException ("MNJMS00004 : INVALID OPERATION. MESSAGE IS MARKED AS WRITE-ONLY."); 1009 1010 if(message == null && outputStream != null){ 1011 try { 1012 outputStream.flush(); 1013 message = byteOutputStream.toByteArray(); 1014 } catch (IOException e) { 1015 throw new JMSException ("MNJMS00011 : IO ERROR OCCURED ON METHOD changeInput(). ERROR TEXT : "+e.getMessage()); 1016 1017 } 1018 1019 } 1020 } 1022 1032 private synchronized int read() throws EOFException { 1033 if(internalOffset < message.length) 1034 return (message[internalOffset++] & 0xff); 1035 else 1036 throw new EOFException ("MNJMS00012 : END OF FILE ERROR IN METHOD read()"); 1037 } 1038 1039 1040 1056 private synchronized final void readFully(byte b[], int off, int len) throws IOException { 1057 int count =0; 1058 if(off + len > b.length ||len+ internalOffset >message.length){ 1059 throw new IOException ("MNJMS00013 : BUFFER TOO SHORT FOR READING "+ 1060 b.length+" BYTES IN METHOD readFully()."); 1061 1062 } 1063 while(count != len){ 1064 b[off+count]= message[internalOffset++]; 1065 count++; 1066 } 1067 } 1068 1071 public long getBodyLength() throws JMSException { 1072 if ((flags & MantaMessage.IS_WRITEABLE) > 0) 1073 throw new MessageNotReadableException ("MNJMS00004 : INVALID OPERATION. MESSAGE IS MARKED AS WRITE-ONLY."); 1074 1075 return message.length; 1076 } 1077 1078 1095 public MantaMessage makeCopy() throws JMSException { 1096 MantaBytesMessage copy = new MantaBytesMessage(); 1097 initCopy(copy); 1098 if(message == null ||message == EMPTY ){ 1100 if (byteOutputStream==null) 1101 copy.message = EMPTY; 1102 else 1103 copy.message=byteOutputStream.toByteArray(); 1104 1105 }else{ 1106 copy.message = message; 1107 } 1108 copy.internalOffset = internalOffset; 1110 return copy; 1111 } 1112 1113} 1114 | Popular Tags |