1 45 package org.exolab.jms.message; 46 47 import java.io.ByteArrayInputStream ; 48 import java.io.ByteArrayOutputStream ; 49 import java.io.DataInputStream ; 50 import java.io.DataOutputStream ; 51 import java.io.EOFException ; 52 import java.io.IOException ; 53 import java.io.ObjectInput ; 54 import java.io.ObjectOutput ; 55 import java.io.UTFDataFormatException ; 56 57 import javax.jms.BytesMessage ; 58 import javax.jms.JMSException ; 59 import javax.jms.MessageEOFException ; 60 import javax.jms.MessageFormatException ; 61 import javax.jms.MessageNotReadableException ; 62 import javax.jms.MessageNotWriteableException ; 63 64 65 73 public final class BytesMessageImpl extends MessageImpl 74 implements BytesMessage { 75 76 79 static final long serialVersionUID = 1; 80 81 84 private static final byte[] EMPTY = new byte[]{}; 85 86 89 private byte[] _bytes = EMPTY; 90 91 94 private DataOutputStream _out = null; 95 96 99 private ByteArrayOutputStream _byteOut = null; 100 101 104 private DataInputStream _in = null; 105 106 109 private ByteArrayInputStream _byteIn = null; 110 111 116 private int _offset = 0; 117 118 124 public BytesMessageImpl() throws JMSException { 125 setJMSType("BytesMessage"); 126 } 127 128 135 public final Object clone() throws CloneNotSupportedException { 136 BytesMessageImpl result = (BytesMessageImpl) super.clone(); 137 if (_bodyReadOnly) { 138 result._bytes = new byte[_bytes.length]; 139 System.arraycopy(_bytes, 0, result._bytes, 0, _bytes.length); 140 if (_byteIn != null) { 141 _offset = _bytes.length - _byteIn.available(); 144 } 145 result._byteIn = null; 146 result._in = null; 147 } else { 148 if (_out != null) { 149 try { 150 _out.flush(); 151 } catch (IOException exception) { 152 throw new CloneNotSupportedException ( 153 exception.getMessage()); 154 } 155 result._bytes = _byteOut.toByteArray(); 156 result._byteOut = null; 157 result._out = null; 158 } else { 159 result._bytes = new byte[_bytes.length]; 160 System.arraycopy(_bytes, 0, result._bytes, 0, _bytes.length); 161 } 162 } 163 164 return result; 165 } 166 167 173 public final void writeExternal(ObjectOutput out) throws IOException { 174 if (!_bodyReadOnly && _out != null) { 176 _out.flush(); 177 _bytes = _byteOut.toByteArray(); 178 } 179 180 super.writeExternal(out); 181 out.writeLong(serialVersionUID); 182 out.writeInt(_bytes.length); 183 out.write(_bytes); 184 out.flush(); 185 } 186 187 195 public final void readExternal(ObjectInput in) 196 throws ClassNotFoundException , IOException { 197 super.readExternal(in); 198 long version = in.readLong(); 199 if (version == serialVersionUID) { 200 int length = in.readInt(); 201 _bytes = new byte[length]; 202 in.readFully(_bytes); 203 } else { 204 throw new IOException ("Incorrect version enountered: " + version + 205 ". This version = " + serialVersionUID); 206 } 207 } 208 209 221 222 public long getBodyLength() throws JMSException { 223 checkRead(); 224 return _bytes.length; 225 } 226 227 236 public final boolean readBoolean() throws JMSException { 237 boolean result = false; 238 prepare(); 239 try { 240 result = _in.readBoolean(); 241 } catch (IOException exception) { 242 revert(exception); 243 } 244 return result; 245 } 246 247 257 public final byte readByte() throws JMSException { 258 byte result = 0; 259 prepare(); 260 try { 261 result = _in.readByte(); 262 } catch (IOException exception) { 263 revert(exception); 264 } 265 return result; 266 } 267 268 278 public final int readUnsignedByte() throws JMSException { 279 int result = 0; 280 prepare(); 281 try { 282 result = _in.readUnsignedByte(); 283 } catch (IOException exception) { 284 revert(exception); 285 } 286 return result; 287 } 288 289 299 public final short readShort() throws JMSException { 300 short result = 0; 301 prepare(); 302 try { 303 result = _in.readShort(); 304 } catch (IOException exception) { 305 revert(exception); 306 } 307 return result; 308 } 309 310 321 public final int readUnsignedShort() throws JMSException { 322 int result = 0; 323 prepare(); 324 try { 325 result = _in.readUnsignedShort(); 326 } catch (IOException exception) { 327 revert(exception); 328 } 329 return result; 330 } 331 332 342 public final char readChar() throws JMSException { 343 char result = 0; 344 prepare(); 345 try { 346 result = _in.readChar(); 347 } catch (IOException exception) { 348 revert(exception); 349 } 350 return result; 351 } 352 353 363 public final int readInt() throws JMSException { 364 int result = 0; 365 prepare(); 366 try { 367 result = _in.readInt(); 368 } catch (IOException exception) { 369 revert(exception); 370 } 371 return result; 372 } 373 374 384 public final long readLong() throws JMSException { 385 long result = 0; 386 prepare(); 387 try { 388 result = _in.readLong(); 389 } catch (IOException exception) { 390 revert(exception); 391 } 392 return result; 393 } 394 395 405 public final float readFloat() throws JMSException { 406 float result = 0; 407 prepare(); 408 try { 409 result = _in.readFloat(); 410 } catch (IOException exception) { 411 revert(exception); 412 } 413 return result; 414 } 415 416 426 public final double readDouble() throws JMSException { 427 double result = 0; 428 prepare(); 429 try { 430 result = _in.readDouble(); 431 } catch (IOException exception) { 432 revert(exception); 433 } 434 return result; 435 } 436 437 453 public final String readUTF() throws JMSException { 454 String result = null; 455 prepare(); 456 try { 457 result = _in.readUTF(); 458 } catch (IOException exception) { 459 revert(exception); 460 } 461 return result; 462 } 463 464 484 public final int readBytes(byte[] value) throws JMSException { 485 return readBytes(value, value.length); 486 } 487 488 516 public final int readBytes(byte[] value, int length) throws JMSException { 517 int read = -1; 518 prepare(); 519 520 if (length < 0 || length > value.length) { 521 throw new IndexOutOfBoundsException ( 522 "Length must be > 0 and less than array size"); 523 } 524 try { 525 _in.mark(length); 526 int remain = _in.available(); 527 if (remain == 0) { 528 read = -1; 529 } else if (length <= remain) { 530 read = length; 531 _in.read(value, 0, length); 532 } else { 533 _in.readFully(value, 0, remain); 534 read = remain; 535 } 536 } catch (EOFException ignore) { 537 } catch (IOException exception) { 538 revert(exception); 539 } 540 return read; 541 } 542 543 555 public final void writeBoolean(boolean value) throws JMSException { 556 checkWrite(); 557 try { 558 getOutputStream().writeBoolean(value); 559 } catch (IOException exception) { 560 raise(exception); 561 } 562 } 563 564 573 public final void writeByte(byte value) throws JMSException { 574 checkWrite(); 575 try { 576 getOutputStream().writeByte(value); 577 } catch (IOException exception) { 578 raise(exception); 579 } 580 } 581 582 591 public final void writeShort(short value) throws JMSException { 592 checkWrite(); 593 try { 594 getOutputStream().writeShort(value); 595 } catch (IOException exception) { 596 raise(exception); 597 } 598 } 599 600 609 public final void writeChar(char value) throws JMSException { 610 checkWrite(); 611 try { 612 getOutputStream().writeChar(value); 613 } catch (IOException exception) { 614 raise(exception); 615 } 616 } 617 618 627 public final void writeInt(int value) throws JMSException { 628 checkWrite(); 629 try { 630 getOutputStream().writeInt(value); 631 } catch (IOException exception) { 632 raise(exception); 633 } 634 } 635 636 645 public final void writeLong(long value) throws JMSException { 646 checkWrite(); 647 try { 648 getOutputStream().writeLong(value); 649 } catch (IOException exception) { 650 raise(exception); 651 } 652 } 653 654 665 public final void writeFloat(float value) throws JMSException { 666 checkWrite(); 667 try { 668 getOutputStream().writeFloat(value); 669 } catch (IOException exception) { 670 raise(exception); 671 } 672 } 673 674 675 686 public final void writeDouble(double value) throws JMSException { 687 checkWrite(); 688 try { 689 getOutputStream().writeDouble(value); 690 } catch (IOException exception) { 691 raise(exception); 692 } 693 } 694 695 709 public final void writeUTF(String value) throws JMSException { 710 checkWrite(); 711 try { 712 getOutputStream().writeUTF(value); 713 } catch (IOException exception) { 714 raise(exception); 715 } 716 } 717 718 726 public final void writeBytes(byte[] value) throws JMSException { 727 checkWrite(); 728 try { 729 getOutputStream().write(value); 730 } catch (IOException exception) { 731 raise(exception); 732 } 733 } 734 735 745 public final void writeBytes(byte[] value, int offset, int length) 746 throws JMSException { 747 checkWrite(); 748 try { 749 getOutputStream().write(value, offset, length); 750 } catch (IOException exception) { 751 raise(exception); 752 } 753 } 754 755 769 public final void writeObject(Object value) throws JMSException { 770 if (value instanceof Boolean ) { 771 writeBoolean(((Boolean ) value).booleanValue()); 772 } else if (value instanceof Byte ) { 773 writeByte(((Byte ) value).byteValue()); 774 } else if (value instanceof Short ) { 775 writeShort(((Short ) value).shortValue()); 776 } else if (value instanceof Character ) { 777 writeChar(((Character ) value).charValue()); 778 } else if (value instanceof Integer ) { 779 writeInt(((Integer ) value).intValue()); 780 } else if (value instanceof Long ) { 781 writeLong(((Long ) value).longValue()); 782 } else if (value instanceof Float ) { 783 writeFloat(((Float ) value).floatValue()); 784 } else if (value instanceof Double ) { 785 writeDouble(((Double ) value).doubleValue()); 786 } else if (value instanceof String ) { 787 writeUTF((String ) value); 788 } else if (value instanceof byte[]) { 789 writeBytes((byte[]) value); 790 } else if (value == null) { 791 throw new NullPointerException ( 792 "BytesMessage does not support null"); 793 } else { 794 throw new MessageFormatException ("Cannot write objects of type=" + 795 value.getClass().getName()); 796 } 797 } 798 799 806 public final void reset() throws JMSException { 807 try { 808 if (!_bodyReadOnly) { 809 _bodyReadOnly = true; 810 if (_out != null) { 811 _out.flush(); 812 _bytes = _byteOut.toByteArray(); 813 _byteOut = null; 814 _out.close(); 815 _out = null; 816 } 817 } else { 818 if (_in != null) { 819 _byteIn = null; 820 _in.close(); 821 _in = null; 822 } 823 } 824 } catch (IOException exception) { 825 raise(exception); 826 } 827 } 828 829 845 public final void clearBody() throws JMSException { 846 try { 847 if (_bodyReadOnly) { 848 _bodyReadOnly = false; 850 if (_in != null) { 851 _byteIn = null; 852 _in.close(); 853 _in = null; 854 _offset = 0; 855 } 856 } else if (_out != null) { 857 _byteOut = null; 859 _out.close(); 860 _out = null; 861 } 862 _bytes = EMPTY; 863 _byteOut = null; 864 _out = null; 865 } catch (IOException exception) { 866 raise(exception); 867 } 868 } 869 870 877 public final void setReadOnly(boolean readOnly) throws JMSException { 878 if (readOnly) { 879 reset(); 880 } 881 super.setReadOnly(readOnly); 882 } 883 884 891 private final void prepare() throws JMSException { 892 checkRead(); 893 getInputStream(); 894 try { 895 _in.mark(_bytes.length - _in.available()); 896 } catch (IOException exception) { 897 raise(exception); 898 } 899 } 900 901 909 private final void revert(IOException exception) throws JMSException { 910 try { 911 _in.reset(); 912 } catch (IOException ignore) { 913 } 914 JMSException error = null; 915 if (exception instanceof EOFException ) { 916 error = new MessageEOFException (exception.getMessage()); 917 } else if (exception instanceof UTFDataFormatException ) { 918 error = new MessageFormatException (exception.getMessage()); 919 } else { 920 error = new JMSException (exception.getMessage()); 921 } 922 error.setLinkedException(exception); 923 throw error; 924 } 925 926 931 private DataInputStream getInputStream() { 932 if (_in == null) { 933 _byteIn = new ByteArrayInputStream (_bytes, _offset, 934 _bytes.length - _offset); 935 _in = new DataInputStream (_byteIn); 936 } 937 return _in; 938 } 939 940 946 private final DataOutputStream getOutputStream() throws IOException { 947 if (_out == null) { 948 _byteOut = new ByteArrayOutputStream (); 949 _out = new DataOutputStream (_byteOut); 950 _out.write(_bytes); 951 } 952 return _out; 953 } 954 955 961 private final void raise(IOException exception) throws JMSException { 962 JMSException error = new JMSException (exception.getMessage()); 963 error.setLinkedException(exception); 964 throw error; 965 } 966 967 } 968 | Popular Tags |