1 22 package org.jboss.mq; 23 24 import java.io.Externalizable ; 25 import java.io.IOException ; 26 import java.io.ObjectInput ; 27 import java.io.ObjectOutput ; 28 import java.util.Vector ; 29 30 import javax.jms.JMSException ; 31 import javax.jms.MessageEOFException ; 32 import javax.jms.MessageFormatException ; 33 import javax.jms.MessageNotReadableException ; 34 import javax.jms.MessageNotWriteableException ; 35 import javax.jms.StreamMessage ; 36 37 import org.jboss.util.Primitives; 38 39 46 public class SpyStreamMessage extends SpyMessage implements StreamMessage , Cloneable , Externalizable 47 { 48 50 51 private final static long serialVersionUID = 2490910971426786841L; 52 53 55 56 Vector content; 57 58 int position; 59 60 int offset; 61 62 int size; 63 64 66 68 71 public SpyStreamMessage() 72 { 73 content = new Vector (); 74 position = 0; 75 size = 0; 76 offset = 0; 77 } 78 79 81 83 public boolean readBoolean() throws JMSException 84 { 85 if (!header.msgReadOnly) 86 throw new MessageNotReadableException ("The message body is writeonly"); 87 88 try 89 { 90 Object value = content.get(position); 91 offset = 0; 92 93 if (value == null) 94 throw new NullPointerException ("Value is null"); 95 else if (value instanceof Boolean ) 96 { 97 position++; 98 return ((Boolean ) value).booleanValue(); 99 } 100 else if (value instanceof String ) 101 { 102 boolean result = Boolean.valueOf((String ) value).booleanValue(); 103 position++; 104 return result; 105 } 106 else 107 throw new MessageFormatException ("Invalid conversion"); 108 } 109 catch (ArrayIndexOutOfBoundsException e) 110 { 111 throw new MessageEOFException (""); 112 } 113 114 } 115 116 public byte readByte() throws JMSException 117 { 118 if (!header.msgReadOnly) 119 throw new MessageNotReadableException ("The message body is writeonly"); 120 121 try 122 { 123 Object value = content.get(position); 124 offset = 0; 125 if (value == null) 126 throw new NullPointerException ("Value is null"); 127 else if (value instanceof Byte ) 128 { 129 position++; 130 return ((Byte ) value).byteValue(); 131 } 132 else if (value instanceof String ) 133 { 134 byte result = Byte.parseByte((String ) value); 135 position++; 136 return result; 137 } 138 else 139 throw new MessageFormatException ("Invalid conversion"); 140 } 141 catch (ArrayIndexOutOfBoundsException e) 142 { 143 throw new MessageEOFException (""); 144 } 145 } 146 147 public short readShort() throws JMSException 148 { 149 if (!header.msgReadOnly) 150 throw new MessageNotReadableException ("The message body is writeonly"); 151 try 152 { 153 Object value = content.get(position); 154 offset = 0; 155 156 if (value == null) 157 throw new NullPointerException ("Value is null"); 158 else if (value instanceof Byte ) 159 { 160 position++; 161 return ((Byte ) value).shortValue(); 162 } 163 else if (value instanceof Short ) 164 { 165 position++; 166 return ((Short ) value).shortValue(); 167 } 168 else if (value instanceof String ) 169 { 170 short result = Short.parseShort((String ) value); 171 position++; 172 return result; 173 } 174 else 175 throw new MessageFormatException ("Invalid conversion"); 176 } 177 catch (ArrayIndexOutOfBoundsException e) 178 { 179 throw new MessageEOFException (""); 180 } 181 } 182 183 public char readChar() throws JMSException 184 { 185 if (!header.msgReadOnly) 186 throw new MessageNotReadableException ("The message body is writeonly"); 187 try 188 { 189 Object value = content.get(position); 190 offset = 0; 191 192 if (value == null) 193 throw new NullPointerException ("Value is null"); 194 else if (value instanceof Character ) 195 { 196 position++; 197 return ((Character ) value).charValue(); 198 } 199 else 200 throw new MessageFormatException ("Invalid conversion"); 201 } 202 catch (ArrayIndexOutOfBoundsException e) 203 { 204 throw new MessageEOFException (""); 205 } 206 } 207 208 public int readInt() throws JMSException 209 { 210 if (!header.msgReadOnly) 211 throw new MessageNotReadableException ("The message body is writeonly"); 212 try 213 { 214 Object value = content.get(position); 215 offset = 0; 216 217 if (value == null) 218 throw new NullPointerException ("Value is null"); 219 else if (value instanceof Byte ) 220 { 221 position++; 222 return ((Byte ) value).intValue(); 223 } 224 else if (value instanceof Short ) 225 { 226 position++; 227 return ((Short ) value).intValue(); 228 } 229 else if (value instanceof Integer ) 230 { 231 position++; 232 return ((Integer ) value).intValue(); 233 } 234 else if (value instanceof String ) 235 { 236 int result = Integer.parseInt((String ) value); 237 position++; 238 return result; 239 } 240 else 241 throw new MessageFormatException ("Invalid conversion"); 242 } 243 catch (ArrayIndexOutOfBoundsException e) 244 { 245 throw new MessageEOFException (""); 246 } 247 } 248 249 public long readLong() throws JMSException 250 { 251 if (!header.msgReadOnly) 252 throw new MessageNotReadableException ("The message body is writeonly"); 253 try 254 { 255 Object value = content.get(position); 256 offset = 0; 257 258 if (value == null) 259 throw new NullPointerException ("Value is null"); 260 else if (value instanceof Byte ) 261 { 262 position++; 263 return ((Byte ) value).longValue(); 264 } 265 else if (value instanceof Short ) 266 { 267 position++; 268 return ((Short ) value).longValue(); 269 } 270 else if (value instanceof Integer ) 271 { 272 position++; 273 return ((Integer ) value).longValue(); 274 } 275 else if (value instanceof Long ) 276 { 277 position++; 278 return ((Long ) value).longValue(); 279 } 280 else if (value instanceof String ) 281 { 282 long result = Long.parseLong((String ) value); 283 position++; 284 return result; 285 } 286 else 287 throw new MessageFormatException ("Invalid conversion"); 288 } 289 catch (ArrayIndexOutOfBoundsException e) 290 { 291 throw new MessageEOFException (""); 292 } 293 } 294 295 public float readFloat() throws JMSException 296 { 297 if (!header.msgReadOnly) 298 throw new MessageNotReadableException ("The message body is writeonly"); 299 try 300 { 301 Object value = content.get(position); 302 offset = 0; 303 304 if (value == null) 305 throw new NullPointerException ("Value is null"); 306 else if (value instanceof Float ) 307 { 308 position++; 309 return ((Float ) value).floatValue(); 310 } 311 else if (value instanceof String ) 312 { 313 float result = Float.parseFloat((String ) value); 314 position++; 315 return result; 316 } 317 else 318 throw new MessageFormatException ("Invalid conversion"); 319 } 320 catch (ArrayIndexOutOfBoundsException e) 321 { 322 throw new MessageEOFException (""); 323 } 324 } 325 326 public double readDouble() throws JMSException 327 { 328 if (!header.msgReadOnly) 329 throw new MessageNotReadableException ("The message body is writeonly"); 330 try 331 { 332 Object value = content.get(position); 333 offset = 0; 334 335 if (value == null) 336 throw new NullPointerException ("Value is null"); 337 else if (value instanceof Float ) 338 { 339 position++; 340 return ((Float ) value).doubleValue(); 341 } 342 else if (value instanceof Double ) 343 { 344 position++; 345 return ((Double ) value).doubleValue(); 346 } 347 else if (value instanceof String ) 348 { 349 double result = Double.parseDouble((String ) value); 350 position++; 351 return result; 352 } 353 else 354 throw new MessageFormatException ("Invalid conversion"); 355 } 356 catch (ArrayIndexOutOfBoundsException e) 357 { 358 throw new MessageEOFException (""); 359 } 360 } 361 362 public String readString() throws JMSException 363 { 364 if (!header.msgReadOnly) 365 throw new MessageNotReadableException ("The message body is writeonly"); 366 try 367 { 368 Object value = content.get(position); 369 offset = 0; 370 371 if (value == null) 372 { 373 position++; 374 return null; 375 } 376 else if (value instanceof Boolean ) 377 { 378 position++; 379 return ((Boolean ) value).toString(); 380 } 381 else if (value instanceof Byte ) 382 { 383 position++; 384 return ((Byte ) value).toString(); 385 } 386 else if (value instanceof Short ) 387 { 388 position++; 389 return ((Short ) value).toString(); 390 } 391 else if (value instanceof Character ) 392 { 393 position++; 394 return ((Character ) value).toString(); 395 } 396 else if (value instanceof Integer ) 397 { 398 position++; 399 return ((Integer ) value).toString(); 400 } 401 else if (value instanceof Long ) 402 { 403 position++; 404 return ((Long ) value).toString(); 405 } 406 else if (value instanceof Float ) 407 { 408 position++; 409 return ((Float ) value).toString(); 410 } 411 else if (value instanceof Double ) 412 { 413 position++; 414 return ((Double ) value).toString(); 415 } 416 else if (value instanceof String ) 417 { 418 position++; 419 return (String ) value; 420 } 421 else 422 throw new MessageFormatException ("Invalid conversion"); 423 } 424 catch (ArrayIndexOutOfBoundsException e) 425 { 426 throw new MessageEOFException (""); 427 } 428 } 429 430 public int readBytes(byte[] value) throws JMSException 431 { 432 if (!header.msgReadOnly) 433 throw new MessageNotReadableException ("The message body is writeonly"); 434 try 435 { 436 Object myObj = content.get(position); 437 if (myObj == null) 438 throw new NullPointerException ("Value is null"); 439 else if (!(myObj instanceof byte[])) 440 throw new MessageFormatException ("Invalid conversion"); 441 byte[] obj = (byte[]) myObj; 442 443 if (obj.length == 0) 444 { 445 position++; 446 offset = 0; 447 return 0; 448 } 449 450 if (offset >= obj.length) 451 { 452 position++; 453 offset = 0; 454 return -1; 455 } 456 457 if (obj.length - offset < value.length) 458 { 459 for (int i = 0; i < obj.length; i++) 460 value[i] = obj[i + offset]; 461 462 position++; 463 offset = 0; 464 465 return obj.length - offset; 466 } 467 else 468 { 469 for (int i = 0; i < value.length; i++) 470 value[i] = obj[i + offset]; 471 offset += value.length; 472 473 return value.length; 474 } 475 476 } 477 catch (ArrayIndexOutOfBoundsException e) 478 { 479 throw new MessageEOFException (""); 480 } 481 } 482 483 public Object readObject() throws JMSException 484 { 485 if (!header.msgReadOnly) 486 throw new MessageNotReadableException ("The message body is writeonly"); 487 try 488 { 489 Object value = content.get(position); 490 position++; 491 offset = 0; 492 493 return value; 494 } 495 catch (ArrayIndexOutOfBoundsException e) 496 { 497 throw new MessageEOFException (""); 498 } 499 } 500 501 public void writeBoolean(boolean value) throws JMSException 502 { 503 if (header.msgReadOnly) 504 throw new MessageNotWriteableException ("The message body is readonly"); 505 content.add(Primitives.valueOf(value)); 506 } 507 508 public void writeByte(byte value) throws JMSException 509 { 510 if (header.msgReadOnly) 511 throw new MessageNotWriteableException ("The message body is readonly"); 512 content.add(new Byte (value)); 513 } 514 515 public void writeShort(short value) throws JMSException 516 { 517 if (header.msgReadOnly) 518 throw new MessageNotWriteableException ("The message body is readonly"); 519 content.add(new Short (value)); 520 } 521 522 public void writeChar(char value) throws JMSException 523 { 524 if (header.msgReadOnly) 525 throw new MessageNotWriteableException ("The message body is readonly"); 526 content.add(new Character (value)); 527 } 528 529 public void writeInt(int value) throws JMSException 530 { 531 if (header.msgReadOnly) 532 throw new MessageNotWriteableException ("The message body is readonly"); 533 content.add(new Integer (value)); 534 } 535 536 public void writeLong(long value) throws JMSException 537 { 538 if (header.msgReadOnly) 539 throw new MessageNotWriteableException ("The message body is readonly"); 540 content.add(new Long (value)); 541 } 542 543 public void writeFloat(float value) throws JMSException 544 { 545 if (header.msgReadOnly) 546 throw new MessageNotWriteableException ("The message body is readonly"); 547 content.add(new Float (value)); 548 } 549 550 public void writeDouble(double value) throws JMSException 551 { 552 if (header.msgReadOnly) 553 throw new MessageNotWriteableException ("The message body is readonly"); 554 content.add(new Double (value)); 555 } 556 557 public void writeString(String value) throws JMSException 558 { 559 if (header.msgReadOnly) 560 throw new MessageNotWriteableException ("The message body is readonly"); 561 if (value == null) 562 content.add(null); 563 else 564 content.add(value); 565 } 566 567 public void writeBytes(byte[] value) throws JMSException 568 { 569 if (header.msgReadOnly) 570 throw new MessageNotWriteableException ("The message body is readonly"); 571 content.add(value.clone()); 572 } 573 574 public void writeBytes(byte[] value, int offset, int length) throws JMSException 575 { 576 if (header.msgReadOnly) 577 throw new MessageNotWriteableException ("The message body is readonly"); 578 579 if (offset + length > value.length) 580 throw new JMSException ("Array is too small"); 581 byte[] temp = new byte[length]; 582 for (int i = 0; i < length; i++) 583 temp[i] = value[i + offset]; 584 585 content.add(temp); 586 } 587 588 public void writeObject(Object value) throws JMSException 589 { 590 if (header.msgReadOnly) 591 throw new MessageNotWriteableException ("The message body is readonly"); 592 if (value == null) 593 content.add(null); 594 else if (value instanceof Boolean ) 595 content.add(value); 596 else if (value instanceof Byte ) 597 content.add(value); 598 else if (value instanceof Short ) 599 content.add(value); 600 else if (value instanceof Character ) 601 content.add(value); 602 else if (value instanceof Integer ) 603 content.add(value); 604 else if (value instanceof Long ) 605 content.add(value); 606 else if (value instanceof Float ) 607 content.add(value); 608 else if (value instanceof Double ) 609 content.add(value); 610 else if (value instanceof String ) 611 content.add(value); 612 else if (value instanceof byte[]) 613 content.add(((byte[]) value).clone()); 614 else 615 throw new MessageFormatException ("Invalid object type"); 616 } 617 618 public void reset() throws JMSException 619 { 620 header.msgReadOnly = true; 621 position = 0; 622 size = content.size(); 623 offset = 0; 624 } 625 626 628 public void clearBody() throws JMSException 629 { 630 content = new Vector (); 631 position = 0; 632 offset = 0; 633 size = 0; 634 635 super.clearBody(); 636 } 637 638 public SpyMessage myClone() throws JMSException 639 { 640 SpyStreamMessage result = MessagePool.getStreamMessage(); 641 result.copyProps(this); 642 result.content = (Vector ) this.content.clone(); 643 result.position = this.position; 644 result.offset = this.offset; 645 result.size = this.size; 646 return result; 647 } 648 649 651 public void readExternal(ObjectInput in) throws IOException , ClassNotFoundException 652 { 653 super.readExternal(in); 654 content = (Vector ) in.readObject(); 655 position = in.readInt(); 656 offset = in.readInt(); 657 size = in.readInt(); 658 } 659 660 public void writeExternal(ObjectOutput out) throws IOException 661 { 662 super.writeExternal(out); 663 out.writeObject(content); 664 out.writeInt(position); 665 out.writeInt(offset); 666 out.writeInt(size); 667 } 668 669 671 673 675 } 677 | Popular Tags |