1 24 package com.scalagent.kjoram; 25 26 import java.io.*; 27 28 import com.scalagent.kjoram.excepts.JMSException; 29 import com.scalagent.kjoram.excepts.MessageFormatException; 30 import com.scalagent.kjoram.excepts.MessageNotWriteableException; 31 import com.scalagent.kjoram.excepts.MessageNotReadableException; 32 import com.scalagent.kjoram.excepts.MessageEOFException; 33 34 35 public class BytesMessage extends Message 36 { 37 38 private ByteArrayOutputStream outputBuffer = null; 39 40 private DataOutputStream outputStream = null; 41 42 private DataInputStream inputStream = null; 43 44 45 private boolean RObody = false; 46 47 private boolean WObody = true; 48 49 50 private byte[] bytes = null; 51 52 private boolean prepared = false; 53 54 55 58 BytesMessage() 59 { 60 super(); 61 outputBuffer = new ByteArrayOutputStream(); 62 outputStream = new DataOutputStream(outputBuffer); 63 } 64 65 72 BytesMessage(Session sess, com.scalagent.kjoram.messages.Message momMsg) 73 { 74 super(sess, momMsg); 75 bytes = momMsg.getBytes(); 76 77 inputStream = new DataInputStream(new ByteArrayInputStream(bytes)); 78 79 RObody = true; 80 WObody = false; 81 } 82 83 84 89 public long getBodyLength() throws JMSException 90 { 91 if (WObody) 92 throw new MessageNotReadableException("Can't get not readable message's" 93 + " size."); 94 return bytes.length; 95 } 96 97 103 public void clearBody() throws JMSException 104 { 105 super.clearBody(); 106 107 try { 108 if (WObody) { 109 outputStream.close(); 110 outputBuffer.close(); 111 } 112 else 113 inputStream.close(); 114 115 outputBuffer = new ByteArrayOutputStream(); 116 outputStream = new DataOutputStream(outputBuffer); 117 bytes = null; 118 RObody = false; 119 WObody = true; 120 prepared = false; 121 } 122 catch (IOException ioE) { 123 JMSException jE = new JMSException("Error while closing the stream" 124 + " facilities."); 125 jE.setLinkedException(ioE); 126 throw jE; 127 } 128 } 129 130 131 137 public void writeBoolean(boolean value) throws JMSException 138 { 139 writeObject(new Boolean (value)); 140 } 141 142 148 public void writeByte(byte value) throws JMSException 149 { 150 writeObject(new Byte (value)); 151 } 152 153 159 public void writeBytes(byte[] value) throws JMSException 160 { 161 writeObject(value); 162 } 163 164 170 public void writeBytes(byte[] value, int offset, int length) 171 throws JMSException 172 { 173 if (RObody) 174 throw new MessageNotWriteableException("Can't write a value as the" 175 + " message body is read-only."); 176 177 if (prepared) { 178 prepared = false; 179 outputBuffer = new ByteArrayOutputStream(); 180 outputStream = new DataOutputStream(outputBuffer); 181 } 182 183 try { 184 outputStream.write(value, offset, length); 185 } 186 catch (IOException ioE) { 187 JMSException jE = new JMSException("Error while writing the value."); 188 jE.setLinkedException(ioE); 189 throw jE; 190 } 191 } 192 193 199 public void writeChar(char value) throws JMSException 200 { 201 writeObject(new Character (value)); 202 } 203 204 210 215 226 232 public void writeInt(int value) throws JMSException 233 { 234 writeObject(new Integer (value)); 235 } 236 237 243 public void writeLong(long value) throws JMSException 244 { 245 writeObject(new Long (value)); 246 } 247 248 254 public void writeShort(short value) throws JMSException 255 { 256 writeObject(new Short (value)); 257 } 258 259 265 public void writeUTF(String value) throws JMSException 266 { 267 writeObject(value); 268 } 269 270 277 public void writeObject(Object value) throws JMSException 278 { 279 if (RObody) 280 throw new MessageNotWriteableException("Can't write a value as the" 281 + " message body is read-only."); 282 283 if (value == null) 284 throw new NullPointerException ("Forbidden null value."); 285 286 if (prepared) { 287 prepared = false; 288 outputBuffer = new ByteArrayOutputStream(); 289 outputStream = new DataOutputStream(outputBuffer); 290 } 291 292 try { 293 if (value instanceof Boolean ) 294 outputStream.writeBoolean(((Boolean ) value).booleanValue()); 295 else if (value instanceof Character ) 296 outputStream.writeChar(((Character ) value).charValue()); 297 else if (value instanceof Integer ) 298 outputStream.writeInt(((Integer ) value).intValue()); 299 else if (value instanceof Long ) 300 outputStream.writeLong(((Long ) value).longValue()); 301 else if (value instanceof String ) 306 outputStream.writeUTF((String ) value); 307 else if (value instanceof byte[]) 308 outputStream.write((byte[]) value); 309 else 310 throw new MessageFormatException("Can't write non Java primitive type" 311 + " as a bytes array."); 312 } 313 catch (IOException ioE) { 314 JMSException jE = new JMSException("Error while writing the value."); 315 jE.setLinkedException(ioE); 316 throw jE; 317 } 318 } 319 320 321 327 public boolean readBoolean() throws JMSException 328 { 329 if (WObody) 330 throw new MessageNotReadableException("Can't read the message body as" 331 + " it is write-only."); 332 try { 333 return inputStream.readBoolean(); 334 } 335 catch (Exception e) { 336 JMSException jE = null; 337 if (e instanceof EOFException) 338 jE = new MessageEOFException("Unexpected end of bytes array."); 339 else if (e instanceof IOException) 340 jE = new JMSException("Could not read the bytes array."); 341 jE.setLinkedException(e); 342 throw jE; 343 } 344 } 345 346 352 public byte readByte() throws JMSException 353 { 354 if (WObody) 355 throw new MessageNotReadableException("Can't read the message body as" 356 + " it is write-only."); 357 try { 358 return inputStream.readByte(); 359 } 360 catch (Exception e) { 361 JMSException jE = null; 362 if (e instanceof EOFException) 363 jE = new MessageEOFException("Unexpected end of bytes array."); 364 else if (e instanceof IOException) 365 jE = new JMSException("Could not read the bytes array."); 366 jE.setLinkedException(e); 367 throw jE; 368 } 369 } 370 371 377 public int readUnsignedByte() throws JMSException 378 { 379 if (WObody) 380 throw new MessageNotReadableException("Can't read the message body as" 381 + " it is write-only."); 382 try { 383 return inputStream.readUnsignedByte(); 384 } 385 catch (Exception e) { 386 JMSException jE = null; 387 if (e instanceof EOFException) 388 jE = new MessageEOFException("Unexpected end of bytes array."); 389 else if (e instanceof IOException) 390 jE = new JMSException("Could not read the bytes array."); 391 jE.setLinkedException(e); 392 throw jE; 393 } 394 } 395 396 402 public short readShort() throws JMSException 403 { 404 if (WObody) 405 throw new MessageNotReadableException("Can't read the message body as" 406 + " it is write-only."); 407 try { 408 return inputStream.readShort(); 409 } 410 catch (Exception e) { 411 JMSException jE = null; 412 if (e instanceof EOFException) 413 jE = new MessageEOFException("Unexpected end of bytes array."); 414 else if (e instanceof IOException) 415 jE = new JMSException("Could not read the bytes array."); 416 jE.setLinkedException(e); 417 throw jE; 418 } 419 } 420 421 427 public int readUnsignedShort() throws JMSException 428 { 429 if (WObody) 430 throw new MessageNotReadableException("Can't read the message body as" 431 + " it is write-only."); 432 try { 433 return inputStream.readUnsignedShort(); 434 } 435 catch (Exception e) { 436 JMSException jE = null; 437 if (e instanceof EOFException) 438 jE = new MessageEOFException("Unexpected end of bytes array."); 439 else if (e instanceof IOException) 440 jE = new JMSException("Could not read the bytes array."); 441 jE.setLinkedException(e); 442 throw jE; 443 } 444 } 445 446 452 public char readChar() throws JMSException 453 { 454 if (WObody) 455 throw new MessageNotReadableException("Can't read the message body as" 456 + " it is write-only."); 457 try { 458 return inputStream.readChar(); 459 } 460 catch (Exception e) { 461 JMSException jE = null; 462 if (e instanceof EOFException) 463 jE = new MessageEOFException("Unexpected end of bytes array."); 464 else if (e instanceof IOException) 465 jE = new JMSException("Could not read the bytes array."); 466 jE.setLinkedException(e); 467 throw jE; 468 } 469 } 470 471 477 public int readInt() throws JMSException 478 { 479 if (WObody) 480 throw new MessageNotReadableException("Can't read the message body as" 481 + " it is write-only."); 482 try { 483 return inputStream.readInt(); 484 } 485 catch (Exception e) { 486 JMSException jE = null; 487 if (e instanceof EOFException) 488 jE = new MessageEOFException("Unexpected end of bytes array."); 489 else if (e instanceof IOException) 490 jE = new JMSException("Could not read the bytes array."); 491 jE.setLinkedException(e); 492 throw jE; 493 } 494 } 495 496 502 public long readLong() throws JMSException 503 { 504 if (WObody) 505 throw new MessageNotReadableException("Can't read the message body as" 506 + " it is write-only."); 507 try { 508 return inputStream.readLong(); 509 } 510 catch (Exception e) { 511 JMSException jE = null; 512 if (e instanceof EOFException) 513 jE = new MessageEOFException("Unexpected end of bytes array."); 514 else if (e instanceof IOException) 515 jE = new JMSException("Could not read the bytes array."); 516 jE.setLinkedException(e); 517 throw jE; 518 } 519 } 520 521 527 546 552 571 577 public int readBytes(byte[] value) throws JMSException 578 { 579 if (WObody) 580 throw new MessageNotReadableException("Can't read the message body as" 581 + " it is write-only."); 582 int counter = 0; 583 584 try { 585 for (int i = 0; i < value.length; i ++) { 586 value[i] = inputStream.readByte(); 587 counter++; 588 } 589 } 590 catch (EOFException eofE) {} 592 catch (IOException ioE) { 594 JMSException jE = null; 595 jE = new JMSException("Could not read the bytes array."); 596 jE.setLinkedException(ioE); 597 throw jE; 598 } 599 if (counter == 0) 600 return -1; 601 return counter; 602 } 603 604 610 public int readBytes(byte[] value, int length) throws JMSException 611 { 612 if (WObody) 613 throw new MessageNotReadableException("Can't read the message body as" 614 + " it is write-only."); 615 if (length > value.length || length < 0) 616 throw new IndexOutOfBoundsException ("Invalid length parameter: " 617 + length); 618 int counter = 0; 619 620 try { 621 for (int i = 0; i < length; i ++) { 622 value[i] = inputStream.readByte(); 623 counter++; 624 } 625 } 626 catch (EOFException eofE) {} 628 catch (IOException ioE) { 630 JMSException jE = null; 631 jE = new JMSException("Could not read the bytes array."); 632 jE.setLinkedException(ioE); 633 throw jE; 634 } 635 if (counter == 0) 636 return -1; 637 return counter; 638 } 639 640 646 public String readUTF() throws JMSException 647 { 648 if (WObody) 649 throw new MessageNotReadableException("Can't read the message body as" 650 + " it is write-only."); 651 try { 652 return inputStream.readUTF(); 653 } 654 catch (Exception e) { 655 JMSException jE = null; 656 if (e instanceof EOFException) 657 jE = new MessageEOFException("Unexpected end of bytes array."); 658 else if (e instanceof IOException) 659 jE = new JMSException("Could not read the bytes array."); 660 jE.setLinkedException(e); 661 throw jE; 662 } 663 } 664 665 666 672 public void reset() throws JMSException 673 { 674 try { 675 if (WObody) { 676 outputStream.flush(); 677 bytes = outputBuffer.toByteArray(); 678 } 679 else 680 inputStream.close(); 681 682 inputStream = new DataInputStream(new ByteArrayInputStream(bytes)); 683 684 RObody = true; 685 WObody = false; 686 } 687 catch (IOException iE) { 688 JMSException jE = 689 new JMSException("Error while manipulating the stream facilities."); 690 jE.setLinkedException(iE); 691 throw jE; 692 } 693 } 694 695 701 protected void prepare() throws Exception 702 { 703 super.prepare(); 704 705 if (WObody) { 706 outputStream.flush(); 707 bytes = outputBuffer.toByteArray(); 708 prepared = true; 709 } 710 711 momMsg.clearBody(); 712 momMsg.setBytes(bytes); 713 } 714 } 715 | Popular Tags |