View Javadoc
1 /* 2 * @(#)BytesMessageImpl.java 3 * 4 * JFoxMQ the open source JMS MOM. 5 * 6 * Corpyright 2002-2003 Huihoo Power, Inc. All Rights Reserved. This software 7 * is licensed under LGPL license. 8 * 9 * For more information, please visit: http://www.huihoo.org 10 */ 11 12 package org.huihoo.jfox.ms.jms.message; 13 14 import java.io.ByteArrayInputStream; 15 import java.io.ByteArrayOutputStream; 16 import java.io.DataInputStream; 17 import java.io.DataOutputStream; 18 import java.io.EOFException; 19 import java.io.IOException; 20 import java.io.Serializable; 21 22 import javax.jms.BytesMessage; 23 import javax.jms.JMSException; 24 import javax.jms.MessageEOFException; 25 import javax.jms.MessageFormatException; 26 import javax.jms.MessageNotReadableException; 27 import javax.jms.MessageNotWriteableException; 28 29 /*** 30 * <p> 31 * A <CODE>StreamMessage</CODE> object is used to send a stream of primitive 32 * types in the Java programming language. It is filled and read sequentially. 33 * It inherits from the <CODE>Message</CODE> interface and adds a stream 34 * message body. Its methods are based largely on those found in <CODE> 35 * java.io.DataInputStream</CODE> and <CODE>java.io.DataOutputStream</CODE>. 36 * 37 * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter.Cheng</a> 38 * @version Revision: 1.2 Date: 2002-12-08 19:22:15 39 */ 40 41 public class BytesMessageImpl 42 extends MessageImpl 43 implements BytesMessage, Serializable { 44 45 private DataOutputStream dos = null; 46 private DataInputStream dis = null; 47 private ByteArrayOutputStream baos = null; 48 private ByteArrayInputStream bais = null; 49 private byte[] buffer = null; 50 51 /*** 52 * Default constructor. 53 */ 54 public BytesMessageImpl() { 55 super(); 56 buffer = new byte[0]; 57 baos = new ByteArrayOutputStream(); 58 dos = new DataOutputStream(baos); 59 } 60 61 /*** 62 * Reads a <code>boolean</code> from the bytes message stream. 63 * 64 * @return the <code>boolean</code> value read 65 * 66 * @exception JMSException 67 * if the JMS provider fails to read the message due to some 68 * internal error. 69 * @exception MessageEOFException 70 * if unexpected end of bytes stream has been reached. 71 * @exception MessageNotReadableException 72 * if the message is in write-only mode. 73 * @see javax.jms.BytesMessage#readBoolean() 74 */ 75 public boolean readBoolean() throws JMSException { 76 if (isBodyModifiable()) { 77 throw new MessageNotReadableException("BytesMesage write_only"); 78 } 79 80 try { 81 return getInputStream().readBoolean(); 82 } catch (EOFException e) { 83 throw new MessageEOFException("BytesMessage end_of_message"); 84 } catch (IOException e) { 85 throw new JMSException(e.getMessage()); 86 } 87 } 88 89 /*** 90 * Reads a signed 8-bit value from the bytes message stream. 91 * 92 * @return the next byte from the bytes message stream as a signed 8-bit 93 * <code>byte</code> 94 * 95 * @exception JMSException 96 * if the JMS provider fails to read the message due to some 97 * internal error. 98 * @exception MessageEOFException 99 * if unexpected end of bytes stream has been reached. 100 * @exception MessageNotReadableException 101 * if the message is in write-only mode. 102 * 103 * @see javax.jms.BytesMessage#readByte() 104 */ 105 public byte readByte() throws JMSException { 106 if (isBodyModifiable()) { 107 throw new MessageNotReadableException("BytesMessage write_only"); 108 } 109 110 try { 111 return this.getInputStream().readByte(); 112 } catch (EOFException e) { 113 throw new MessageEOFException("BytesMessage end_of_message"); 114 } catch (IOException e) { 115 throw new JMSException(e.getMessage()); 116 } 117 } 118 119 /*** 120 * Reads an unsigned 8-bit number from the bytes message stream. 121 * 122 * @return the next byte from the bytes message stream, interpreted as an 123 * unsigned 8-bit number 124 * 125 * @exception JMSException 126 * if the JMS provider fails to read the message due to some 127 * internal error. 128 * @exception MessageEOFException 129 * if unexpected end of bytes stream has been reached. 130 * @exception MessageNotReadableException 131 * if the message is in write-only mode. 132 * 133 * @see javax.jms.BytesMessage#readUnsignedByte() 134 */ 135 public int readUnsignedByte() throws JMSException { 136 if (isBodyModifiable()) { 137 throw new MessageNotReadableException("BytesMessage write_only"); 138 } 139 140 try { 141 return getInputStream().readUnsignedByte(); 142 } catch (EOFException e) { 143 throw new MessageEOFException("BytesMessageimpl end_of_message"); 144 } catch (IOException e) { 145 throw new JMSException(e.getMessage()); 146 } 147 } 148 149 /*** 150 * Reads a signed 16-bit number from the bytes message stream. 151 * 152 * @return the next two bytes from the bytes message stream, interpreted as 153 * a signed 16-bit number 154 * 155 * @exception JMSException 156 * if the JMS provider fails to read the message due to some 157 * internal error. 158 * @exception MessageEOFException 159 * if unexpected end of bytes stream has been reached. 160 * @exception MessageNotReadableException 161 * if the message is in write-only mode. 162 * 163 * @see javax.jms.BytesMessage#readShort() 164 */ 165 public short readShort() throws JMSException { 166 if (isBodyModifiable()) { 167 throw new MessageNotReadableException("BytesMessage write_only"); 168 } 169 170 try { 171 return getInputStream().readShort(); 172 } catch (EOFException e) { 173 throw new MessageEOFException("Bytesmessage end_of_message"); 174 } catch (IOException e) { 175 throw new JMSException(e.getMessage()); 176 } 177 } 178 179 /*** 180 * Reads an unsigned 16-bit number from the bytes message stream. 181 * 182 * @return the next two bytes from the bytes message stream, interpreted as 183 * an unsigned 16-bit integer 184 * 185 * @exception JMSException 186 * if the JMS provider fails to read the message due to some 187 * internal error. 188 * @exception MessageEOFException 189 * if unexpected end of bytes stream has been reached. 190 * @exception MessageNotReadableException 191 * if the message is in write-only mode. 192 * 193 * @see javax.jms.BytesMessage#readUnsignedShort() 194 */ 195 public int readUnsignedShort() throws JMSException { 196 if (isBodyModifiable()) { 197 throw new MessageNotReadableException("BytesMessage write_only"); 198 } 199 200 try { 201 return getInputStream().readUnsignedShort(); 202 } catch (EOFException e) { 203 throw new MessageEOFException("BytesMessage end_of_message"); 204 } catch (IOException e) { 205 throw new JMSException(e.getMessage()); 206 } 207 } 208 209 /*** 210 * Reads a Unicode character value from the bytes message stream. 211 * 212 * @return the next two bytes from the bytes message stream as a Unicode 213 * character 214 * 215 * @exception JMSException 216 * if the JMS provider fails to read the message due to some 217 * internal error. 218 * @exception MessageEOFException 219 * if unexpected end of bytes stream has been reached. 220 * @exception MessageNotReadableException 221 * if the message is in write-only mode. 222 * 223 * @see javax.jms.BytesMessage#readChar() 224 */ 225 public char readChar() throws JMSException { 226 if (isBodyModifiable()) { 227 throw new MessageNotReadableException("BytesMessage write_only"); 228 } 229 230 try { 231 return getInputStream().readChar(); 232 } catch (EOFException e) { 233 throw new MessageEOFException("BytesMessage end_of_message"); 234 } catch (IOException e) { 235 throw new JMSException(e.getMessage()); 236 } 237 } 238 239 /*** 240 * Reads a signed 32-bit integer from the bytes message stream. 241 * 242 * @return the next four bytes from the bytes message stream, interpreted 243 * as an <code>int</code> 244 * 245 * @exception JMSException 246 * if the JMS provider fails to read the message due to some 247 * internal error. 248 * @exception MessageEOFException 249 * if unexpected end of bytes stream has been reached. 250 * @exception MessageNotReadableException 251 * if the message is in write-only mode. 252 * 253 * @see javax.jms.BytesMessage#readInt() 254 */ 255 public int readInt() throws JMSException { 256 if (isBodyModifiable()) { 257 throw new MessageNotReadableException("BytesMessage write_only"); 258 } 259 260 try { 261 return getInputStream().readInt(); 262 } catch (EOFException e1) { 263 throw new MessageEOFException("BytesMessage end_of_message"); 264 } catch (IOException e) { 265 throw new JMSException(e.getMessage()); 266 } 267 } 268 269 /*** 270 * Reads a signed 64-bit integer from the bytes message stream. 271 * 272 * @return the next eight bytes from the bytes message stream, interpreted 273 * as a <code>long</code> 274 * 275 * @exception JMSException 276 * if the JMS provider fails to read the message due to some 277 * internal error. 278 * @exception MessageEOFException 279 * if unexpected end of bytes stream has been reached. 280 * @exception MessageNotReadableException 281 * if the message is in write-only mode. 282 * 283 * @see javax.jms.BytesMessage#readLong() 284 */ 285 public long readLong() throws JMSException { 286 if (isBodyModifiable()) { 287 throw new MessageNotReadableException("BytesMessage write_only"); 288 } 289 290 try { 291 return this.getInputStream().readLong(); 292 } catch (EOFException e) { 293 throw new MessageEOFException("BytesMessage end_of_message"); 294 } catch (IOException e) { 295 throw new JMSException(e.getMessage()); 296 } 297 } 298 299 /*** 300 * Reads a <code>float</code> from the bytes message stream. 301 * 302 * @return the next four bytes from the bytes message stream, interpreted 303 * as a <code>float</code> 304 * 305 * @exception JMSException 306 * if the JMS provider fails to read the message due to some 307 * internal error. 308 * @exception MessageEOFException 309 * if unexpected end of bytes stream has been reached. 310 * @exception MessageNotReadableException 311 * if the message is in write-only mode. 312 * 313 * @see javax.jms.BytesMessage#readFloat() 314 */ 315 public float readFloat() throws JMSException { 316 if (isBodyModifiable()) { 317 throw new MessageNotReadableException("BytesMessage write_only"); 318 } 319 320 try { 321 return getInputStream().readFloat(); 322 } catch (EOFException e) { 323 throw new MessageEOFException("BytesMessage end_of_message"); 324 } catch (IOException e) { 325 throw new JMSException(e.getMessage()); 326 } 327 } 328 329 /*** 330 * Reads a <code>double</code> from the bytes message stream. 331 * 332 * @return the next eight bytes from the bytes message stream, interpreted 333 * as a <code>double</code> 334 * 335 * @exception JMSException 336 * if the JMS provider fails to read the message due to some 337 * internal error. 338 * @exception MessageEOFException 339 * if unexpected end of bytes stream has been reached. 340 * @exception MessageNotReadableException 341 * if the message is in write-only mode. 342 * 343 * @see javax.jms.BytesMessage#readDouble() 344 */ 345 public double readDouble() throws JMSException { 346 if (isBodyModifiable()) { 347 throw new MessageNotReadableException("BytesMessage write_only"); 348 } 349 350 try { 351 return getInputStream().readDouble(); 352 } catch (EOFException e) { 353 throw new MessageEOFException("BytesMessage end_of_message"); 354 } catch (IOException e) { 355 throw new JMSException(e.getMessage()); 356 } 357 } 358 359 /*** 360 * Reads a string that has been encoded using a modified UTF-8 format from 361 * the bytes message stream. 362 * 363 * <P> 364 * For more information on the UTF-8 format, see "File System Safe UCS 365 * Transformation Format (FSS_UTF)", X/Open Preliminary Specification, 366 * X/Open Company Ltd., Document Number: P316. This information also 367 * appears in ISO/IEC 10646, Annex P. 368 * 369 * @return a Unicode string from the bytes message stream 370 * 371 * @exception JMSException 372 * if the JMS provider fails to read the message due to some 373 * internal error. 374 * @exception MessageEOFException 375 * if unexpected end of bytes stream has been reached. 376 * @exception MessageNotReadableException 377 * if the message is in write-only mode. 378 * 379 * @see javax.jms.BytesMessage#readUTF() 380 */ 381 public String readUTF() throws JMSException { 382 if (isBodyModifiable()) { 383 throw new MessageNotReadableException("BytesMessage write_only"); 384 } 385 386 try { 387 return getInputStream().readUTF(); 388 } catch (EOFException e) { 389 throw new MessageEOFException("BytesMessage end_of_message"); 390 } catch (IOException e) { 391 throw new JMSException(e.getMessage()); 392 } 393 } 394 395 /*** 396 * Reads a byte array from the bytes message stream. 397 * 398 * <P> 399 * If the length of array <code>value</code> is less than the number of 400 * bytes remaining to be read from the stream, the array should be filled. 401 * A subsequent call reads the next increment, and so on. 402 * 403 * <P> 404 * If the number of bytes remaining in the stream is less than the length 405 * of array <code>value</code>, the bytes should be read into the array. 406 * The return value of the total number of bytes read will be less than the 407 * length of the array, indicating that there are no more bytes left to be 408 * read from the stream. The next read of the stream returns -1. 409 * 410 * @param value 411 * the buffer into which the data is read 412 * 413 * @return the total number of bytes read into the buffer, or -1 if there 414 * is no more data because the end of the stream has been reached 415 * 416 * @exception JMSException 417 * if the JMS provider fails to read the message due to some 418 * internal error. 419 * @exception MessageNotReadableException 420 * if the message is in write-only mode. 421 * 422 * @see javax.jms.BytesMessage#readBytes(byte[]) 423 */ 424 public int readBytes(byte[] value) throws JMSException { 425 if (isBodyModifiable()) { 426 throw new MessageNotReadableException("BytesMessage write_only"); 427 } 428 429 try { 430 return this.getInputStream().read(value); 431 } catch (IOException e) { 432 throw new JMSException(e.getMessage()); 433 } 434 } 435 436 /*** 437 * Reads a portion of the bytes message stream. 438 * 439 * <P> 440 * If the length of array <code>value</code> is less than the number of 441 * bytes remaining to be read from the stream, the array should be filled. 442 * A subsequent call reads the next increment, and so on. 443 * 444 * <P> 445 * If the number of bytes remaining in the stream is less than the length 446 * of array <code>value</code>, the bytes should be read into the array. 447 * The return value of the total number of bytes read will be less than the 448 * length of the array, indicating that there are no more bytes left to be 449 * read from the stream. The next read of the stream returns -1. 450 * 451 * <p> 452 * If <code>length</code> is negative, or <code>length</code> is 453 * greater than the length of the array <code>value</code>, then an 454 * <code>IndexOutOfBoundsException</code> is thrown. No bytes will be 455 * read from the stream for this exception case. 456 * 457 * @param value 458 * the buffer into which the data is read 459 * @param length 460 * the number of bytes to read; must be less than or equal to 461 * <code>value.length</code> 462 * 463 * @return the total number of bytes read into the buffer, or -1 if there 464 * is no more data because the end of the stream has been reached 465 * 466 * @exception JMSException 467 * if the JMS provider fails to read the message due to some 468 * internal error. 469 * @exception MessageNotReadableException 470 * if the message is in write-only mode. 471 * 472 * @see javax.jms.BytesMessage#readBytes(byte[], int) 473 */ 474 public int readBytes(byte[] value, int length) throws JMSException { 475 if (isBodyModifiable()) { 476 throw new MessageNotReadableException("BytesMessage write_only"); 477 } 478 479 if ((length < 0) || (length > value.length)) { 480 throw new IndexOutOfBoundsException(); 481 } 482 483 try { 484 return getInputStream().read(value, 0, length); 485 } catch (IOException e) { 486 throw new JMSException(e.getMessage()); 487 } 488 } 489 490 /*** 491 * Writes a <code>boolean</code> to the bytes message stream as a 1-byte 492 * value. The value <code>true</code> is written as the value <code>(byte)1</code>; 493 * the value <code>false</code> is written as the value <code>(byte)0</code>. 494 * 495 * @param value 496 * the <code>boolean</code> value to be written 497 * 498 * @exception JMSException 499 * if the JMS provider fails to write the message due to 500 * some internal error. 501 * @exception MessageNotWriteableException 502 * if the message is in read-only mode. 503 * 504 * @see javax.jms.BytesMessage#writeBoolean(boolean) 505 */ 506 public void writeBoolean(boolean value) throws JMSException { 507 if (!isBodyModifiable()) { 508 throw new MessageNotWriteableException("BytesMessage read_only"); 509 } 510 511 try { 512 getOutputStream().writeBoolean(value); 513 } catch (IOException e) { 514 throw new JMSException(e.getMessage()); 515 } 516 } 517 518 /*** 519 * Writes a <code>byte</code> to the bytes message stream as a 1-byte 520 * value. 521 * 522 * @param value 523 * the <code>byte</code> value to be written 524 * 525 * @exception JMSException 526 * if the JMS provider fails to write the message due to 527 * some internal error. 528 * @exception MessageNotWriteableException 529 * if the message is in read-only mode. 530 * 531 * @see javax.jms.BytesMessage#writeByte(byte) 532 */ 533 public void writeByte(byte value) throws JMSException { 534 if (!isBodyModifiable()) { 535 throw new MessageNotWriteableException("BytesMessage read_only"); 536 } 537 538 try { 539 getOutputStream().writeByte((int)value); 540 } catch (IOException e) { 541 throw new JMSException(e.getMessage()); 542 } 543 } 544 545 /*** 546 * Writes a <code>short</code> to the bytes message stream as two bytes, 547 * high byte first. 548 * 549 * @param value 550 * the <code>short</code> to be written 551 * 552 * @exception JMSException 553 * if the JMS provider fails to write the message due to 554 * some internal error. 555 * @exception MessageNotWriteableException 556 * if the message is in read-only mode. 557 * 558 * @see javax.jms.BytesMessage#writeShort(short) 559 */ 560 public void writeShort(short value) throws JMSException { 561 if (!isBodyModifiable()) { 562 throw new MessageNotWriteableException("BytesMessage read_only"); 563 } 564 565 try { 566 getOutputStream().writeShort((int)value); 567 } catch (IOException e) { 568 throw new JMSException(e.getMessage()); 569 } 570 } 571 572 /*** 573 * Writes a <code>char</code> to the bytes message stream as a 2-byte 574 * value, high byte first. 575 * 576 * @param value 577 * the <code>char</code> value to be written 578 * 579 * @exception JMSException 580 * if the JMS provider fails to write the message due to 581 * some internal error. 582 * @exception MessageNotWriteableException 583 * if the message is in read-only mode. 584 * 585 * @see javax.jms.BytesMessage#writeChar(char) 586 */ 587 public void writeChar(char value) throws JMSException { 588 if (!isBodyModifiable()) { 589 throw new MessageNotWriteableException("BytesMessage read_only"); 590 } 591 592 try { 593 this.getOutputStream().writeChar((int)value); 594 } catch (IOException e) { 595 throw new JMSException(e.getMessage()); 596 } 597 } 598 599 /*** 600 * Writes an <code>int</code> to the bytes message stream as four bytes, 601 * high byte first. 602 * 603 * @param value 604 * the <code>int</code> to be written 605 * 606 * @exception JMSException 607 * if the JMS provider fails to write the message due to 608 * some internal error. 609 * @exception MessageNotWriteableException 610 * if the message is in read-only mode. 611 * 612 * @see javax.jms.BytesMessage#writeInt(int) 613 */ 614 public void writeInt(int value) throws JMSException { 615 if (!isBodyModifiable()) { 616 throw new MessageNotWriteableException("BytesMessage read_only"); 617 } 618 619 try { 620 getOutputStream().writeInt(value); 621 } catch (IOException e) { 622 throw new JMSException(e.getMessage()); 623 } 624 } 625 626 /*** 627 * Writes a <code>long</code> to the bytes message stream as eight bytes, 628 * high byte first. 629 * 630 * @param value 631 * the <code>long</code> to be written 632 * 633 * @exception JMSException 634 * if the JMS provider fails to write the message due to 635 * some internal error. 636 * @exception MessageNotWriteableException 637 * if the message is in read-only mode. 638 * 639 * @see javax.jms.BytesMessage#writeLong(long) 640 */ 641 public void writeLong(long value) throws JMSException { 642 if (!isBodyModifiable()) { 643 throw new MessageNotWriteableException("BytesMessage read_only"); 644 } 645 646 try { 647 this.getOutputStream().writeLong(value); 648 } catch (IOException e) { 649 throw new JMSException(e.getMessage()); 650 } 651 } 652 653 /*** 654 * Converts the <code>float</code> argument to an <code>int</code> 655 * using the <code>floatToIntBits</code> method in class <code>Float</code>, 656 * and then writes that <code>int</code> value to the bytes message 657 * stream as a 4-byte quantity, high byte first. 658 * 659 * @param value 660 * the <code>float</code> value to be written 661 * 662 * @exception JMSException 663 * if the JMS provider fails to write the message due to 664 * some internal error. 665 * @exception MessageNotWriteableException 666 * if the message is in read-only mode. 667 * 668 * @see javax.jms.BytesMessage#writeFloat(float) 669 */ 670 public void writeFloat(float value) throws JMSException { 671 if (!isBodyModifiable()) { 672 throw new MessageNotWriteableException("BytesMessage read_only"); 673 } 674 675 try { 676 getOutputStream().writeFloat(value); 677 } catch (IOException e) { 678 throw new JMSException(e.getMessage()); 679 } 680 } 681 682 /*** 683 * Converts the <code>double</code> argument to a <code>long</code> 684 * using the <code>doubleToLongBits</code> method in class <code>Double</code>, 685 * and then writes that <code>long</code> value to the bytes message 686 * stream as an 8-byte quantity, high byte first. 687 * 688 * @param value 689 * the <code>double</code> value to be written 690 * 691 * @exception JMSException 692 * if the JMS provider fails to write the message due to 693 * some internal error. 694 * @exception MessageNotWriteableException 695 * if the message is in read-only mode. 696 * 697 * @see javax.jms.BytesMessage#writeDouble(double) 698 */ 699 public void writeDouble(double value) throws JMSException { 700 if (!isBodyModifiable()) { 701 throw new MessageNotWriteableException("BytesMessage read_only"); 702 } 703 704 try { 705 getOutputStream().writeDouble(value); 706 } catch (IOException e) { 707 throw new JMSException(e.getMessage()); 708 } 709 } 710 711 /*** 712 * Writes a string to the bytes message stream using UTF-8 encoding in a 713 * machine-independent manner. 714 * 715 * <P> 716 * For more information on the UTF-8 format, see "File System Safe UCS 717 * Transformation Format (FSS_UTF)", X/Open Preliminary Specification, 718 * X/Open Company Ltd., Document Number: P316. This information also 719 * appears in ISO/IEC 10646, Annex P. 720 * 721 * @param value 722 * the <code>String</code> value to be written 723 * 724 * @exception JMSException 725 * if the JMS provider fails to write the message due to 726 * some internal error. 727 * @exception MessageNotWriteableException 728 * if the message is in read-only mode. 729 * 730 * @see javax.jms.BytesMessage#writeUTF(String) 731 */ 732 public void writeUTF(String value) throws JMSException { 733 if (!isBodyModifiable()) { 734 throw new MessageNotWriteableException("BytesMessage read_only"); 735 } 736 737 try { 738 getOutputStream().writeUTF(value); 739 } catch (IOException e) { 740 throw new JMSException(e.getMessage()); 741 } 742 } 743 744 /*** 745 * Writes a byte array to the bytes message stream. 746 * 747 * @param value 748 * the byte array to be written 749 * 750 * @exception JMSException 751 * if the JMS provider fails to write the message due to 752 * some internal error. 753 * @exception MessageNotWriteableException 754 * if the message is in read-only mode. 755 * 756 * @see javax.jms.BytesMessage#writeBytes(byte[]) 757 */ 758 public void writeBytes(byte[] value) throws JMSException { 759 if (!isBodyModifiable()) { 760 throw new MessageNotWriteableException("BytesMessage read_only"); 761 } 762 763 try { 764 this.getOutputStream().write(value); 765 } catch (IOException e) { 766 throw new JMSException(e.getMessage()); 767 } 768 } 769 770 /*** 771 * Writes a portion of a byte array to the bytes message stream. 772 * 773 * @param value 774 * the byte array value to be written 775 * @param offset 776 * the initial offset within the byte array 777 * @param length 778 * the number of bytes to use 779 * 780 * @exception JMSException 781 * if the JMS provider fails to write the message due to 782 * some internal error. 783 * @exception MessageNotWriteableException 784 * if the message is in read-only mode. 785 * 786 * @see javax.jms.BytesMessage#writeBytes(byte[], int, int) 787 */ 788 public void writeBytes(byte[] value, int offset, int length) 789 throws JMSException { 790 if (!isBodyModifiable()) { 791 throw new MessageNotWriteableException("BytesMessage read_only"); 792 } 793 794 try { 795 getOutputStream().write(value, offset, length); 796 } catch (IOException e) { 797 throw new JMSException(e.getMessage()); 798 } 799 } 800 801 /*** 802 * Writes an object to the bytes message stream. 803 * 804 * <P> 805 * This method works only for the objectified primitive object types ( 806 * <code>Integer</code>,<code>Double</code>,<code>Long</code> 807 *  ...), <code>String</code> objects, and byte arrays. 808 * 809 * @param value 810 * the object in the Java programming language ("Java object") 811 * to be written; it must not be null 812 * 813 * @exception JMSException 814 * if the JMS provider fails to write the message due to 815 * some internal error. 816 * @exception MessageFormatException 817 * if the object is of an invalid type. 818 * @exception MessageNotWriteableException 819 * if the message is in read-only mode. 820 * @exception java.lang.NullPointerException 821 * if the parameter <code>value</code> is null. 822 * 823 * @see javax.jms.BytesMessage#writeObject(Object) 824 */ 825 public void writeObject(Object value) throws JMSException { 826 if (!isBodyModifiable()) { 827 throw new MessageNotWriteableException("BytesMessage read_only"); 828 } 829 830 if (value instanceof Boolean) { 831 writeBoolean(((Boolean)value).booleanValue()); 832 } else if (value instanceof Byte) { 833 writeByte(((Byte)value).byteValue()); 834 } else if (value instanceof Character) { 835 writeChar(((Character)value).charValue()); 836 } else if (value instanceof Double) { 837 writeDouble(((Double)value).doubleValue()); 838 } else if (value instanceof Float) { 839 writeFloat(((Float)value).floatValue()); 840 } else if (value instanceof Integer) { 841 writeInt(((Integer)value).intValue()); 842 } else if (value instanceof Long) { 843 writeLong(((Long)value).longValue()); 844 } else if (value instanceof Short) { 845 writeShort(((Short)value).shortValue()); 846 } else if (value instanceof String) { 847 writeUTF((String)value); 848 } else if (value instanceof byte[]) { 849 writeBytes((byte[])value); 850 } else { 851 throw new MessageFormatException("ByteMessage invalid_type"); 852 } 853 } 854 855 /*** 856 * Puts the message body in read-only mode and repositions the stream of 857 * bytes to the beginning. 858 * 859 * @exception JMSException 860 * if the JMS provider fails to reset the message due to 861 * some internal error. 862 * @exception MessageFormatException 863 * if the message has an invalid format. 864 * 865 * @see javax.jms.BytesMessage#reset() 866 */ 867 public void reset() throws JMSException { 868 try { 869 if (dos != null) { 870 dos.flush(); 871 dis = 872 new DataInputStream( 873 new ByteArrayInputStream(baos.toByteArray())); 874 setBodyModifiable(false); 875 } 876 } catch (IOException e) { 877 throw new JMSException("Can't reset message " + e.getMessage()); 878 } 879 } 880 881 /*** 882 * Sets the object reference in the message to null and sets the message to 883 * modifiable. 884 * 885 * @exception javax.jms.JMSException 886 */ 887 public void clearBody() throws JMSException { 888 super.clearBody(); 889 buffer = new byte[0]; 890 baos = new ByteArrayOutputStream(); 891 dos = new DataOutputStream(baos); 892 bais = null; 893 dis = null; 894 } 895 896 /*** 897 * Initialise the input stream if it hasn't been intialised 898 * 899 * @return the input stream 900 */ 901 private DataInputStream getInputStream() { 902 if (dis == null) { 903 bais = new ByteArrayInputStream(buffer); 904 dis = new DataInputStream(bais); 905 } 906 return dis; 907 } 908 909 /*** 910 * Initialise the output stream if it hasn't been intialised 911 * 912 * @return the output stream 913 * @throws IOException 914 * if the output stream can't be created 915 */ 916 private DataOutputStream getOutputStream() throws IOException { 917 if (dos == null) { 918 baos = new ByteArrayOutputStream(); 919 dos = new DataOutputStream(baos); 920 dos.write(buffer); 921 } 922 return dos; 923 } 924 }

This page was automatically generated by Maven