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.BytesMessage ; 17 import javax.jms.JMSException ; 18 import javax.jms.MessageEOFException ; 19 import javax.jms.MessageFormatException ; 20 import javax.jms.MessageNotReadableException ; 21 import javax.jms.MessageNotWriteableException ; 22 23 34 35 public class BytesMessageImpl 36 extends JMSMessage 37 implements BytesMessage , Serializable { 38 39 private transient DataOutputStream dos = null; 41 private transient DataInputStream dis = null; 42 private transient ByteArrayOutputStream baos = null; 43 private transient ByteArrayInputStream bais = null; 44 private byte[] buffer = new byte[0]; 45 46 49 public BytesMessageImpl() { 50 super(); 51 baos = new ByteArrayOutputStream (); 52 dos = new DataOutputStream (baos); 53 } 54 55 66 public boolean readBoolean() throws JMSException { 67 if (isBodyModifiable()) { 68 throw new MessageNotReadableException ("BytesMesage write_only"); 69 } 70 71 try { 72 return getInputStream().readBoolean(); 73 } catch (EOFException e) { 74 throw new MessageEOFException ("BytesMessage end_of_message"); 75 } catch (IOException e) { 76 throw new JMSException (e.getMessage()); 77 } 78 } 79 80 92 public byte readByte() throws JMSException { 93 if (isBodyModifiable()) { 94 throw new MessageNotReadableException ("BytesMessage write_only"); 95 } 96 97 try { 98 return this.getInputStream().readByte(); 99 } catch (EOFException e) { 100 throw new MessageEOFException ("BytesMessage end_of_message"); 101 } catch (IOException e) { 102 throw new JMSException (e.getMessage()); 103 } 104 } 105 106 118 public int readUnsignedByte() throws JMSException { 119 if (isBodyModifiable()) { 120 throw new MessageNotReadableException ("BytesMessage write_only"); 121 } 122 123 try { 124 return getInputStream().readUnsignedByte(); 125 } catch (EOFException e) { 126 throw new MessageEOFException ("BytesMessageimpl end_of_message"); 127 } catch (IOException e) { 128 throw new JMSException (e.getMessage()); 129 } 130 } 131 132 144 public short readShort() throws JMSException { 145 if (isBodyModifiable()) { 146 throw new MessageNotReadableException ("BytesMessage write_only"); 147 } 148 149 try { 150 return getInputStream().readShort(); 151 } catch (EOFException e) { 152 throw new MessageEOFException ("Bytesmessage end_of_message"); 153 } catch (IOException e) { 154 throw new JMSException (e.getMessage()); 155 } 156 } 157 158 170 public int readUnsignedShort() throws JMSException { 171 if (isBodyModifiable()) { 172 throw new MessageNotReadableException ("BytesMessage write_only"); 173 } 174 175 try { 176 return getInputStream().readUnsignedShort(); 177 } catch (EOFException e) { 178 throw new MessageEOFException ("BytesMessage end_of_message"); 179 } catch (IOException e) { 180 throw new JMSException (e.getMessage()); 181 } 182 } 183 184 196 public char readChar() throws JMSException { 197 if (isBodyModifiable()) { 198 throw new MessageNotReadableException ("BytesMessage write_only"); 199 } 200 201 try { 202 return getInputStream().readChar(); 203 } catch (EOFException e) { 204 throw new MessageEOFException ("BytesMessage end_of_message"); 205 } catch (IOException e) { 206 throw new JMSException (e.getMessage()); 207 } 208 } 209 210 222 public int readInt() throws JMSException { 223 if (isBodyModifiable()) { 224 throw new MessageNotReadableException ("BytesMessage write_only"); 225 } 226 227 try { 228 return getInputStream().readInt(); 229 } catch (EOFException e1) { 230 throw new MessageEOFException ("BytesMessage end_of_message"); 231 } catch (IOException e) { 232 throw new JMSException (e.getMessage()); 233 } 234 } 235 236 248 public long readLong() throws JMSException { 249 if (isBodyModifiable()) { 250 throw new MessageNotReadableException ("BytesMessage write_only"); 251 } 252 253 try { 254 return this.getInputStream().readLong(); 255 } catch (EOFException e) { 256 throw new MessageEOFException ("BytesMessage end_of_message"); 257 } catch (IOException e) { 258 throw new JMSException (e.getMessage()); 259 } 260 } 261 262 274 public float readFloat() throws JMSException { 275 if (isBodyModifiable()) { 276 throw new MessageNotReadableException ("BytesMessage write_only"); 277 } 278 279 try { 280 return getInputStream().readFloat(); 281 } catch (EOFException e) { 282 throw new MessageEOFException ("BytesMessage end_of_message"); 283 } catch (IOException e) { 284 throw new JMSException (e.getMessage()); 285 } 286 } 287 288 300 public double readDouble() throws JMSException { 301 if (isBodyModifiable()) { 302 throw new MessageNotReadableException ("BytesMessage write_only"); 303 } 304 305 try { 306 return getInputStream().readDouble(); 307 } catch (EOFException e) { 308 throw new MessageEOFException ("BytesMessage end_of_message"); 309 } catch (IOException e) { 310 throw new JMSException (e.getMessage()); 311 } 312 } 313 314 332 public String readUTF() throws JMSException { 333 if (isBodyModifiable()) { 334 throw new MessageNotReadableException ("BytesMessage write_only"); 335 } 336 337 try { 338 return getInputStream().readUTF(); 339 } catch (EOFException e) { 340 throw new MessageEOFException ("BytesMessage end_of_message"); 341 } catch (IOException e) { 342 throw new JMSException (e.getMessage()); 343 } 344 } 345 346 370 public int readBytes(byte[] value) throws JMSException { 371 if (isBodyModifiable()) { 372 throw new MessageNotReadableException ("BytesMessage write_only"); 373 } 374 375 try { 376 return this.getInputStream().read(value); 377 } catch (IOException e) { 378 throw new JMSException (e.getMessage()); 379 } 380 } 381 382 414 public int readBytes(byte[] value, int length) throws JMSException { 415 if (isBodyModifiable()) { 416 throw new MessageNotReadableException ("BytesMessage write_only"); 417 } 418 419 if ((length < 0) || (length > value.length)) { 420 throw new IndexOutOfBoundsException (); 421 } 422 423 try { 424 return getInputStream().read(value, 0, length); 425 } catch (IOException e) { 426 throw new JMSException (e.getMessage()); 427 } 428 } 429 430 442 public void writeBoolean(boolean value) throws JMSException { 443 if (!isBodyModifiable()) { 444 throw new MessageNotWriteableException ("BytesMessage read_only"); 445 } 446 447 try { 448 getOutputStream().writeBoolean(value); 449 } catch (IOException e) { 450 throw new JMSException (e.getMessage()); 451 } 452 } 453 454 465 public void writeByte(byte value) throws JMSException { 466 if (!isBodyModifiable()) { 467 throw new MessageNotWriteableException ("BytesMessage read_only"); 468 } 469 470 try { 471 getOutputStream().writeByte((int) value); 472 } catch (IOException e) { 473 throw new JMSException (e.getMessage()); 474 } 475 } 476 477 488 public void writeShort(short value) throws JMSException { 489 if (!isBodyModifiable()) { 490 throw new MessageNotWriteableException ("BytesMessage read_only"); 491 } 492 493 try { 494 getOutputStream().writeShort((int) value); 495 } catch (IOException e) { 496 throw new JMSException (e.getMessage()); 497 } 498 } 499 500 511 public void writeChar(char value) throws JMSException { 512 if (!isBodyModifiable()) { 513 throw new MessageNotWriteableException ("BytesMessage read_only"); 514 } 515 516 try { 517 this.getOutputStream().writeChar((int) value); 518 } catch (IOException e) { 519 throw new JMSException (e.getMessage()); 520 } 521 } 522 523 534 public void writeInt(int value) throws JMSException { 535 if (!isBodyModifiable()) { 536 throw new MessageNotWriteableException ("BytesMessage read_only"); 537 } 538 539 try { 540 getOutputStream().writeInt(value); 541 } catch (IOException e) { 542 throw new JMSException (e.getMessage()); 543 } 544 } 545 546 557 public void writeLong(long value) throws JMSException { 558 if (!isBodyModifiable()) { 559 throw new MessageNotWriteableException ("BytesMessage read_only"); 560 } 561 562 try { 563 this.getOutputStream().writeLong(value); 564 } catch (IOException e) { 565 throw new JMSException (e.getMessage()); 566 } 567 } 568 569 582 public void writeFloat(float value) throws JMSException { 583 if (!isBodyModifiable()) { 584 throw new MessageNotWriteableException ("BytesMessage read_only"); 585 } 586 587 try { 588 getOutputStream().writeFloat(value); 589 } catch (IOException e) { 590 throw new JMSException (e.getMessage()); 591 } 592 } 593 594 607 public void writeDouble(double value) throws JMSException { 608 if (!isBodyModifiable()) { 609 throw new MessageNotWriteableException ("BytesMessage read_only"); 610 } 611 612 try { 613 getOutputStream().writeDouble(value); 614 } catch (IOException e) { 615 throw new JMSException (e.getMessage()); 616 } 617 } 618 619 636 public void writeUTF(String value) throws JMSException { 637 if (!isBodyModifiable()) { 638 throw new MessageNotWriteableException ("BytesMessage read_only"); 639 } 640 641 try { 642 getOutputStream().writeUTF(value); 643 } catch (IOException e) { 644 throw new JMSException (e.getMessage()); 645 } 646 } 647 648 658 public void writeBytes(byte[] value) throws JMSException { 659 if (!isBodyModifiable()) { 660 throw new MessageNotWriteableException ("BytesMessage read_only"); 661 } 662 663 try { 664 this.getOutputStream().write(value); 665 } catch (IOException e) { 666 throw new JMSException (e.getMessage()); 667 } 668 } 669 670 682 public void writeBytes(byte[] value, int offset, int length) 683 throws JMSException { 684 if (!isBodyModifiable()) { 685 throw new MessageNotWriteableException ("BytesMessage read_only"); 686 } 687 688 try { 689 getOutputStream().write(value, offset, length); 690 } catch (IOException e) { 691 throw new JMSException (e.getMessage()); 692 } 693 } 694 695 714 public void writeObject(Object value) throws JMSException { 715 if (!isBodyModifiable()) { 716 throw new MessageNotWriteableException ("BytesMessage read_only"); 717 } 718 719 if (value instanceof Boolean ) { 720 writeBoolean(((Boolean ) value).booleanValue()); 721 } else if (value instanceof Byte ) { 722 writeByte(((Byte ) value).byteValue()); 723 } else if (value instanceof Character ) { 724 writeChar(((Character ) value).charValue()); 725 } else if (value instanceof Double ) { 726 writeDouble(((Double ) value).doubleValue()); 727 } else if (value instanceof Float ) { 728 writeFloat(((Float ) value).floatValue()); 729 } else if (value instanceof Integer ) { 730 writeInt(((Integer ) value).intValue()); 731 } else if (value instanceof Long ) { 732 writeLong(((Long ) value).longValue()); 733 } else if (value instanceof Short ) { 734 writeShort(((Short ) value).shortValue()); 735 } else if (value instanceof String ) { 736 writeUTF((String ) value); 737 } else if (value instanceof byte[]) { 738 writeBytes((byte[]) value); 739 } else { 740 throw new MessageFormatException ("ByteMessage invalid_type"); 741 } 742 } 743 744 754 public void reset() throws JMSException { 755 try { 756 if (dos != null) { 757 dos.flush(); 758 dis = 759 new DataInputStream (new ByteArrayInputStream (baos.toByteArray())); 760 setBodyModifiable(false); 761 } 762 } catch (IOException e) { 763 throw new JMSException ("Can't reset message " + e.getMessage()); 764 } 765 } 766 767 773 public void clearBody() throws JMSException { 774 super.clearBody(); 775 buffer = new byte[0]; 776 baos = new ByteArrayOutputStream (); 777 dos = new DataOutputStream (baos); 778 bais = null; 779 dis = null; 780 } 781 782 787 private DataInputStream getInputStream() { 788 if (dis == null) { 789 bais = new ByteArrayInputStream (buffer); 790 dis = new DataInputStream (bais); 791 } 792 return dis; 793 } 794 795 801 private DataOutputStream getOutputStream() throws IOException { 802 if (dos == null) { 803 baos = new ByteArrayOutputStream (); 804 dos = new DataOutputStream (baos); 805 dos.write(buffer); 806 } 807 return dos; 808 } 809 810 public long getBodyLength() throws JMSException { 811 return buffer.length; 812 } 813 } 814 | Popular Tags |