1 22 package org.jboss.mq; 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.ByteArrayOutputStream ; 26 import java.io.DataInputStream ; 27 import java.io.DataOutputStream ; 28 import java.io.EOFException ; 29 import java.io.IOException ; 30 import java.io.Externalizable ; 31 import java.io.ObjectInput ; 32 import java.io.ObjectOutput ; 33 import java.security.PrivilegedAction ; 34 import java.security.AccessController ; 35 import java.util.ArrayList ; 36 37 import javax.jms.BytesMessage ; 38 import javax.jms.JMSException ; 39 import javax.jms.MessageEOFException ; 40 import javax.jms.MessageFormatException ; 41 import javax.jms.MessageNotReadableException ; 42 import javax.jms.MessageNotWriteableException ; 43 44 51 public class SpyBytesMessage extends SpyMessage 52 implements Cloneable , BytesMessage , Externalizable 53 { 54 59 private final static String USE_WRITE_UTF = "org.jboss.mq.useWriteUTF"; 60 61 private static boolean useWriteUTF = true; 62 63 68 private final static String CHUNK_UTF = "org.jboss.mq.chunkUTF"; 69 70 private static boolean chunkUTF = true; 71 72 73 private final static int chunkSize = 16384; 74 75 76 private final static long serialVersionUID = -6572727147964701014L; 77 78 static 79 { 80 PrivilegedAction action = new PrivilegedAction () 81 { 82 public Object run() 83 { 84 return System.getProperty(USE_WRITE_UTF, "true"); 85 } 86 }; 87 try 88 { 89 String flag = (String ) AccessController.doPrivileged(action); 90 useWriteUTF = Boolean.valueOf(flag).booleanValue(); 91 } 92 catch(Throwable ignore) 93 { 94 } 95 action = new PrivilegedAction () 96 { 97 public Object run() 98 { 99 return System.getProperty(CHUNK_UTF, "true"); 100 } 101 }; 102 try 103 { 104 String flag = (String ) AccessController.doPrivileged(action); 105 chunkUTF = Boolean.valueOf(flag).booleanValue(); 106 } 107 catch(Throwable ignore) 108 { 109 } 110 } 111 112 113 byte[] InternalArray = null; 114 115 private transient ByteArrayOutputStream ostream = null; 116 private transient DataOutputStream p = null; 117 private transient ByteArrayInputStream istream = null; 118 private transient DataInputStream m = null; 119 120 123 public SpyBytesMessage() 124 { 125 header.msgReadOnly = false; 126 ostream = new ByteArrayOutputStream (); 127 p = new DataOutputStream (ostream); 128 } 129 130 public boolean readBoolean() throws JMSException 131 { 132 checkRead(); 133 try 134 { 135 return m.readBoolean(); 136 } 137 catch (EOFException e) 138 { 139 throw new MessageEOFException (""); 140 } 141 catch (IOException e) 142 { 143 throw new JMSException ("IOException"); 144 } 145 } 146 147 public byte readByte() throws JMSException 148 { 149 checkRead(); 150 try 151 { 152 return m.readByte(); 153 } 154 catch (EOFException e) 155 { 156 throw new MessageEOFException (""); 157 } 158 catch (IOException e) 159 { 160 throw new JMSException ("IOException"); 161 } 162 } 163 164 public int readUnsignedByte() throws JMSException 165 { 166 checkRead(); 167 try 168 { 169 return m.readUnsignedByte(); 170 } 171 catch (EOFException e) 172 { 173 throw new MessageEOFException (""); 174 } 175 catch (IOException e) 176 { 177 throw new JMSException ("IOException"); 178 } 179 } 180 181 public short readShort() throws JMSException 182 { 183 checkRead(); 184 try 185 { 186 return m.readShort(); 187 } 188 catch (EOFException e) 189 { 190 throw new MessageEOFException (""); 191 } 192 catch (IOException e) 193 { 194 throw new JMSException ("IOException"); 195 } 196 } 197 198 public int readUnsignedShort() throws JMSException 199 { 200 checkRead(); 201 try 202 { 203 return m.readUnsignedShort(); 204 } 205 catch (EOFException e) 206 { 207 throw new MessageEOFException (""); 208 } 209 catch (IOException e) 210 { 211 throw new JMSException ("IOException"); 212 } 213 } 214 215 public char readChar() throws JMSException 216 { 217 checkRead(); 218 try 219 { 220 return m.readChar(); 221 } 222 catch (EOFException e) 223 { 224 throw new MessageEOFException (""); 225 } 226 catch (IOException e) 227 { 228 throw new JMSException ("IOException"); 229 } 230 } 231 232 public int readInt() throws JMSException 233 { 234 checkRead(); 235 try 236 { 237 return m.readInt(); 238 } 239 catch (EOFException e) 240 { 241 throw new MessageEOFException (""); 242 } 243 catch (IOException e) 244 { 245 throw new JMSException ("IOException"); 246 } 247 } 248 249 public long readLong() throws JMSException 250 { 251 checkRead(); 252 try 253 { 254 return m.readLong(); 255 } 256 catch (EOFException e) 257 { 258 throw new MessageEOFException (""); 259 } 260 catch (IOException e) 261 { 262 throw new JMSException ("IOException"); 263 } 264 } 265 266 public float readFloat() throws JMSException 267 { 268 checkRead(); 269 try 270 { 271 return m.readFloat(); 272 } 273 catch (EOFException e) 274 { 275 throw new MessageEOFException (""); 276 } 277 catch (IOException e) 278 { 279 throw new JMSException ("IOException"); 280 } 281 } 282 283 public double readDouble() throws JMSException 284 { 285 checkRead(); 286 try 287 { 288 return m.readDouble(); 289 } 290 catch (EOFException e) 291 { 292 throw new MessageEOFException (""); 293 } 294 catch (IOException e) 295 { 296 throw new JMSException ("IOException"); 297 } 298 } 299 300 public String readUTF() throws JMSException 301 { 302 checkRead(); 303 try 304 { 305 if (chunkUTF == false) 306 return m.readUTF(); 307 308 byte type = m.readByte(); 309 if (type == NULL) 310 return null; 311 312 314 int chunksToRead = m.readInt(); 318 int bufferSize = chunkSize * chunksToRead; 319 320 if (chunksToRead == 1) 322 { 323 329 int inSize = m.available(); 330 if (inSize <= 0) 331 { 332 inSize = 256; 333 } 334 335 bufferSize = Math.min(inSize, bufferSize); 336 } 337 338 StringBuffer sb = new StringBuffer (bufferSize); 340 341 for (int i = 0; i < chunksToRead; i++) 342 { 343 sb.append(m.readUTF()); 344 } 345 346 return sb.toString(); 347 } 348 catch (EOFException e) 349 { 350 throw new MessageEOFException (""); 351 } 352 catch (IOException e) 353 { 354 throw new JMSException ("IOException"); 355 } 356 } 357 358 public int readBytes(byte[] value) throws JMSException 359 { 360 checkRead(); 361 try 362 { 363 return m.read(value); 364 } 365 catch (IOException e) 366 { 367 throw new JMSException ("IOException"); 368 } 369 } 370 371 public int readBytes(byte[] value, int length) throws JMSException 372 { 373 checkRead(); 374 try 375 { 376 return m.read(value, 0, length); 377 } 378 catch (IOException e) 379 { 380 throw new JMSException ("IOException"); 381 } 382 } 383 384 public void writeBoolean(boolean value) throws JMSException 385 { 386 if (header.msgReadOnly) 387 { 388 throw new MessageNotWriteableException ("the message body is read-only"); 389 } 390 try 391 { 392 p.writeBoolean(value); 393 } 394 catch (IOException e) 395 { 396 throw new JMSException ("IOException"); 397 } 398 } 399 400 public void writeByte(byte value) throws JMSException 401 { 402 if (header.msgReadOnly) 403 { 404 throw new MessageNotWriteableException ("the message body is read-only"); 405 } 406 try 407 { 408 p.writeByte(value); 409 } 410 catch (IOException e) 411 { 412 throw new JMSException ("IOException"); 413 } 414 } 415 416 public void writeShort(short value) throws JMSException 417 { 418 if (header.msgReadOnly) 419 { 420 throw new MessageNotWriteableException ("the message body is read-only"); 421 } 422 try 423 { 424 p.writeShort(value); 425 } 426 catch (IOException e) 427 { 428 throw new JMSException ("IOException"); 429 } 430 } 431 432 public void writeChar(char value) throws JMSException 433 { 434 if (header.msgReadOnly) 435 { 436 throw new MessageNotWriteableException ("the message body is read-only"); 437 } 438 try 439 { 440 p.writeChar(value); 441 } 442 catch (IOException e) 443 { 444 throw new JMSException ("IOException"); 445 } 446 } 447 448 public void writeInt(int value) throws JMSException 449 { 450 if (header.msgReadOnly) 451 { 452 throw new MessageNotWriteableException ("the message body is read-only"); 453 } 454 try 455 { 456 p.writeInt(value); 457 } 458 catch (IOException e) 459 { 460 throw new JMSException ("IOException"); 461 } 462 } 463 464 public void writeLong(long value) throws JMSException 465 { 466 if (header.msgReadOnly) 467 { 468 throw new MessageNotWriteableException ("the message body is read-only"); 469 } 470 try 471 { 472 p.writeLong(value); 473 } 474 catch (IOException e) 475 { 476 throw new JMSException ("IOException"); 477 } 478 } 479 480 public void writeFloat(float value) throws JMSException 481 { 482 if (header.msgReadOnly) 483 { 484 throw new MessageNotWriteableException ("the message body is read-only"); 485 } 486 try 487 { 488 p.writeFloat(value); 489 } 490 catch (IOException e) 491 { 492 throw new JMSException ("IOException"); 493 } 494 } 495 496 public void writeDouble(double value) throws JMSException 497 { 498 if (header.msgReadOnly) 499 { 500 throw new MessageNotWriteableException ("the message body is read-only"); 501 } 502 try 503 { 504 p.writeDouble(value); 505 } 506 catch (IOException e) 507 { 508 throw new JMSException ("IOException"); 509 } 510 } 511 512 public void writeUTF(String value) throws JMSException 513 { 514 if (header.msgReadOnly) 515 { 516 throw new MessageNotWriteableException ("the message body is read-only"); 517 } 518 try 519 { 520 if (chunkUTF == false) 521 p.writeUTF(value); 522 else 523 { 524 if (value == null) 525 p.writeByte(NULL); 526 else 527 { 528 530 533 536 ArrayList v = new ArrayList (); 537 int contentLength = value.length(); 538 539 while (contentLength > 0) 540 { 541 int beginCopy = (v.size()) * chunkSize; 542 int endCopy = contentLength <= chunkSize ? beginCopy + contentLength : beginCopy + chunkSize; 543 544 String theChunk = value.substring(beginCopy, endCopy); 545 v.add(theChunk); 546 547 contentLength -= chunkSize; 548 } 549 550 p.writeByte(OBJECT); 553 p.writeInt(v.size()); 554 555 for (int i = 0; i < v.size(); i++) 556 { 557 p.writeUTF((String ) v.get(i)); 558 } 559 } 560 } 561 } 562 catch (IOException e) 563 { 564 throw new JMSException ("IOException"); 565 } 566 } 567 568 public void writeBytes(byte[] value) throws JMSException 569 { 570 if (header.msgReadOnly) 571 { 572 throw new MessageNotWriteableException ("the message body is read-only"); 573 } 574 try 575 { 576 p.write(value, 0, value.length); 577 } 578 catch (IOException e) 579 { 580 throw new JMSException ("IOException"); 581 } 582 } 583 584 public void writeBytes(byte[] value, int offset, int length) throws JMSException 585 { 586 if (header.msgReadOnly) 587 { 588 throw new MessageNotWriteableException ("the message body is read-only"); 589 } 590 try 591 { 592 p.write(value, offset, length); 593 } 594 catch (IOException e) 595 { 596 throw new JMSException ("IOException"); 597 } 598 } 599 600 public void writeObject(Object value) throws JMSException 601 { 602 if (header.msgReadOnly) 603 { 604 throw new MessageNotWriteableException ("the message body is read-only"); 605 } 606 try 607 { 608 if (value == null) 609 { 610 throw new NullPointerException ("Attempt to write a new value"); 611 } 612 if (value instanceof String ) 613 { 614 String s = (String ) value; 615 if( useWriteUTF == true ) 616 writeUTF(s); 617 else 618 p.writeChars(s); 619 } 620 else if (value instanceof Boolean ) 621 { 622 p.writeBoolean(((Boolean ) value).booleanValue()); 623 } 624 else if (value instanceof Byte ) 625 { 626 p.writeByte(((Byte ) value).byteValue()); 627 } 628 else if (value instanceof Short ) 629 { 630 p.writeShort(((Short ) value).shortValue()); 631 } 632 else if (value instanceof Integer ) 633 { 634 p.writeInt(((Integer ) value).intValue()); 635 } 636 else if (value instanceof Long ) 637 { 638 p.writeLong(((Long ) value).longValue()); 639 } 640 else if (value instanceof Float ) 641 { 642 p.writeFloat(((Float ) value).floatValue()); 643 } 644 else if (value instanceof Double ) 645 { 646 p.writeDouble(((Double ) value).doubleValue()); 647 } 648 else if (value instanceof byte[]) 649 { 650 p.write((byte[]) value, 0, ((byte[]) value).length); 651 } 652 else 653 { 654 throw new MessageFormatException ("Invalid object for properties"); 655 } 656 } 657 catch (IOException e) 658 { 659 throw new JMSException ("IOException"); 660 } 661 662 } 663 664 public void reset() throws JMSException 665 { 666 try 667 { 668 if (!header.msgReadOnly) 669 { 670 p.flush(); 671 InternalArray = ostream.toByteArray(); 672 ostream.close(); 673 } 674 ostream = null; 675 istream = null; 676 m = null; 677 p = null; 678 header.msgReadOnly = true; 679 } 680 catch (IOException e) 681 { 682 throw new JMSException ("IOException"); 683 } 684 } 685 686 public void clearBody() throws JMSException 687 { 688 try 689 { 690 if (!header.msgReadOnly) 691 { 692 ostream.close(); 693 } 694 else 695 { 696 if (istream != null) 701 istream.close(); 702 } 703 } 704 catch (IOException e) 705 { 706 } 708 709 ostream = new ByteArrayOutputStream (); 710 p = new DataOutputStream (ostream); 711 InternalArray = null; 712 istream = null; 713 m = null; 714 715 super.clearBody(); 716 } 717 718 public SpyMessage myClone() throws JMSException 719 { 720 SpyBytesMessage result = MessagePool.getBytesMessage(); 721 this.reset(); 722 result.copyProps(this); 723 if (this.InternalArray != null) 724 { 725 result.InternalArray = new byte[this.InternalArray.length]; 726 System.arraycopy(this.InternalArray, 0, result.InternalArray, 0, this.InternalArray.length); 727 } 728 return result; 729 } 730 731 public long getBodyLength() throws JMSException 732 { 733 checkRead(); 734 return InternalArray.length; 735 } 736 737 public void writeExternal(ObjectOutput out) throws IOException 738 { 739 byte[] arrayToSend = null; 740 if (!header.msgReadOnly) 741 { 742 p.flush(); 743 arrayToSend = ostream.toByteArray(); 744 } 745 else 746 { 747 arrayToSend = InternalArray; 748 } 749 super.writeExternal(out); 750 if (arrayToSend == null) 751 { 752 out.writeInt(0); } 754 else 755 { 756 out.writeInt(arrayToSend.length); 757 out.write(arrayToSend); 758 } 759 } 760 761 public void readExternal(ObjectInput in) throws IOException , ClassNotFoundException 762 { 763 super.readExternal(in); 764 int length = in.readInt(); 765 if (length < 0) 766 { 767 InternalArray = null; 768 } 769 else 770 { 771 InternalArray = new byte[length]; 772 in.readFully(InternalArray); 773 } 774 } 775 776 781 private void checkRead() throws JMSException 782 { 783 if (!header.msgReadOnly) 784 { 785 throw new MessageNotReadableException ("readByte while the buffer is writeonly"); 786 } 787 788 if (istream == null || m == null) 791 { 792 istream = new ByteArrayInputStream (InternalArray); 793 m = new DataInputStream (istream); 794 } 795 } 796 } 797 | Popular Tags |