1 24 package org.objectweb.joram.client.jms; 25 26 import java.io.*; 27 28 import javax.jms.JMSException ; 29 import javax.jms.MessageFormatException ; 30 import javax.jms.MessageNotWriteableException ; 31 import javax.jms.MessageNotReadableException ; 32 import javax.jms.MessageEOFException ; 33 34 37 public final class BytesMessage extends Message implements javax.jms.BytesMessage { 38 39 private transient ByteArrayOutputStream outputBuffer = null; 40 41 private transient DataOutputStream outputStream = null; 42 43 private transient DataInputStream inputStream = null; 44 45 private transient boolean prepared = false; 46 47 50 BytesMessage() { 51 super(); 52 momMsg.type = momMsg.BYTES; 53 54 outputBuffer = new ByteArrayOutputStream(); 55 outputStream = new DataOutputStream(outputBuffer); 56 } 57 58 65 BytesMessage(Session sess, org.objectweb.joram.shared.messages.Message momMsg) { 66 super(sess, momMsg); 67 inputStream = new DataInputStream(new ByteArrayInputStream(momMsg.body)); 68 } 69 70 75 public long getBodyLength() throws JMSException { 76 if (! RObody) 77 throw new MessageNotReadableException ("Can't get not readable message's" 78 + " size."); 79 return momMsg.body.length; 80 } 81 82 88 public void clearBody() throws JMSException { 89 try { 90 if (! RObody) { 91 outputStream.close(); 92 outputBuffer.close(); 93 } else { 94 inputStream.close(); 95 } 96 97 outputBuffer = new ByteArrayOutputStream(); 98 outputStream = new DataOutputStream(outputBuffer); 99 100 super.clearBody(); 101 102 prepared = false; 103 } catch (IOException ioE) { 104 JMSException jE = new JMSException ("Error while closing the stream" 105 + " facilities."); 106 jE.setLinkedException(ioE); 107 throw jE; 108 } 109 } 110 111 117 public void writeBoolean(boolean value) throws JMSException { 118 writeObject(new Boolean (value)); 119 } 120 121 127 public void writeByte(byte value) throws JMSException { 128 writeObject(new Byte (value)); 129 } 130 131 137 public void writeBytes(byte[] value) throws JMSException { 138 writeObject(value); 139 } 140 141 147 public void writeBytes(byte[] value, int offset, int length) throws JMSException { 148 if (RObody) 149 throw new MessageNotWriteableException ("Can't write a value as the" 150 + " message body is read-only."); 151 152 if (prepared) { 153 prepared = false; 154 outputBuffer = new ByteArrayOutputStream(); 155 outputStream = new DataOutputStream(outputBuffer); 156 } 157 158 try { 159 outputStream.write(value, offset, length); 160 } catch (IOException ioE) { 161 JMSException jE = new JMSException ("Error while writing the value."); 162 jE.setLinkedException(ioE); 163 throw jE; 164 } 165 } 166 167 173 public void writeChar(char value) throws JMSException { 174 writeObject(new Character (value)); 175 } 176 177 183 public void writeDouble(double value) throws JMSException { 184 writeObject(new Double (value)); 185 } 186 187 193 public void writeFloat(float value) throws JMSException { 194 writeObject(new Float (value)); 195 } 196 197 203 public void writeInt(int value) throws JMSException { 204 writeObject(new Integer (value)); 205 } 206 207 213 public void writeLong(long value) throws JMSException { 214 writeObject(new Long (value)); 215 } 216 217 223 public void writeShort(short value) throws JMSException { 224 writeObject(new Short (value)); 225 } 226 227 233 public void writeUTF(String value) throws JMSException { 234 writeObject(value); 235 } 236 237 244 public void writeObject(Object value) throws JMSException { 245 if (RObody) 246 throw new MessageNotWriteableException ("Can't write a value as the" 247 + " message body is read-only."); 248 249 if (value == null) 250 throw new NullPointerException ("Forbidden null value."); 251 252 if (prepared) { 253 prepared = false; 254 outputBuffer = new ByteArrayOutputStream(); 255 outputStream = new DataOutputStream(outputBuffer); 256 } 257 258 try { 259 if (value instanceof Boolean ) 260 outputStream.writeBoolean(((Boolean ) value).booleanValue()); 261 else if (value instanceof Character ) 262 outputStream.writeChar(((Character ) value).charValue()); 263 else if (value instanceof Integer ) 264 outputStream.writeInt(((Integer ) value).intValue()); 265 else if (value instanceof Short ) 266 outputStream.writeShort(((Short ) value).shortValue()); 267 else if (value instanceof Long ) 268 outputStream.writeLong(((Long ) value).longValue()); 269 else if (value instanceof Float ) 270 outputStream.writeFloat(((Float ) value).floatValue()); 271 else if (value instanceof Double ) 272 outputStream.writeDouble(((Double ) value).doubleValue()); 273 else if (value instanceof String ) 274 outputStream.writeUTF((String ) value); 275 else if (value instanceof Byte ) 276 outputStream.writeByte(((Byte ) value).intValue()); 277 else if (value instanceof byte[]) 278 outputStream.write((byte[]) value); 279 else 280 throw new MessageFormatException ("Can't write non Java primitive type" 281 + " as a bytes array."); 282 } catch (IOException ioE) { 283 JMSException jE = new JMSException ("Error while writing the value."); 284 jE.setLinkedException(ioE); 285 throw jE; 286 } 287 } 288 289 295 public boolean readBoolean() throws JMSException { 296 if (! RObody) 297 throw new MessageNotReadableException ("Can't read the message body as" 298 + " it is write-only."); 299 try { 300 return inputStream.readBoolean(); 301 } catch (Exception e) { 302 JMSException jE = null; 303 if (e instanceof EOFException ) 304 jE = new MessageEOFException ("Unexpected end of bytes array."); 305 else if (e instanceof IOException) 306 jE = new JMSException ("Could not read the bytes array."); 307 jE.setLinkedException(e); 308 throw jE; 309 } 310 } 311 312 318 public byte readByte() throws JMSException { 319 if (! RObody) 320 throw new MessageNotReadableException ("Can't read the message body as" 321 + " it is write-only."); 322 try { 323 return inputStream.readByte(); 324 } catch (Exception e) { 325 JMSException jE = null; 326 if (e instanceof EOFException ) 327 jE = new MessageEOFException ("Unexpected end of bytes array."); 328 else if (e instanceof IOException) 329 jE = new JMSException ("Could not read the bytes array."); 330 jE.setLinkedException(e); 331 throw jE; 332 } 333 } 334 335 341 public int readUnsignedByte() throws JMSException { 342 if (! RObody) 343 throw new MessageNotReadableException ("Can't read the message body as" 344 + " it is write-only."); 345 try { 346 return inputStream.readUnsignedByte(); 347 } catch (Exception e) { 348 JMSException jE = null; 349 if (e instanceof EOFException ) 350 jE = new MessageEOFException ("Unexpected end of bytes array."); 351 else if (e instanceof IOException) 352 jE = new JMSException ("Could not read the bytes array."); 353 jE.setLinkedException(e); 354 throw jE; 355 } 356 } 357 358 364 public short readShort() throws JMSException { 365 if (! RObody) 366 throw new MessageNotReadableException ("Can't read the message body as" 367 + " it is write-only."); 368 try { 369 return inputStream.readShort(); 370 } catch (Exception e) { 371 JMSException jE = null; 372 if (e instanceof EOFException ) 373 jE = new MessageEOFException ("Unexpected end of bytes array."); 374 else if (e instanceof IOException) 375 jE = new JMSException ("Could not read the bytes array."); 376 jE.setLinkedException(e); 377 throw jE; 378 } 379 } 380 381 387 public int readUnsignedShort() throws JMSException { 388 if (! RObody) 389 throw new MessageNotReadableException ("Can't read the message body as" 390 + " it is write-only."); 391 try { 392 return inputStream.readUnsignedShort(); 393 } catch (Exception e) { 394 JMSException jE = null; 395 if (e instanceof EOFException ) 396 jE = new MessageEOFException ("Unexpected end of bytes array."); 397 else if (e instanceof IOException) 398 jE = new JMSException ("Could not read the bytes array."); 399 jE.setLinkedException(e); 400 throw jE; 401 } 402 } 403 404 410 public char readChar() throws JMSException { 411 if (! RObody) 412 throw new MessageNotReadableException ("Can't read the message body as" 413 + " it is write-only."); 414 try { 415 return inputStream.readChar(); 416 } catch (Exception e) { 417 JMSException jE = null; 418 if (e instanceof EOFException ) 419 jE = new MessageEOFException ("Unexpected end of bytes array."); 420 else if (e instanceof IOException) 421 jE = new JMSException ("Could not read the bytes array."); 422 jE.setLinkedException(e); 423 throw jE; 424 } 425 } 426 427 433 public int readInt() throws JMSException { 434 if (! RObody) 435 throw new MessageNotReadableException ("Can't read the message body as" 436 + " it is write-only."); 437 try { 438 return inputStream.readInt(); 439 } catch (Exception e) { 440 JMSException jE = null; 441 if (e instanceof EOFException ) 442 jE = new MessageEOFException ("Unexpected end of bytes array."); 443 else if (e instanceof IOException) 444 jE = new JMSException ("Could not read the bytes array."); 445 jE.setLinkedException(e); 446 throw jE; 447 } 448 } 449 450 456 public long readLong() throws JMSException { 457 if (! RObody) 458 throw new MessageNotReadableException ("Can't read the message body as" 459 + " it is write-only."); 460 try { 461 return inputStream.readLong(); 462 } catch (Exception e) { 463 JMSException jE = null; 464 if (e instanceof EOFException ) 465 jE = new MessageEOFException ("Unexpected end of bytes array."); 466 else if (e instanceof IOException) 467 jE = new JMSException ("Could not read the bytes array."); 468 jE.setLinkedException(e); 469 throw jE; 470 } 471 } 472 473 479 public float readFloat() throws JMSException { 480 if (! RObody) 481 throw new MessageNotReadableException ("Can't read the message body as" 482 + " it is write-only."); 483 try { 484 return inputStream.readFloat(); 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 double readDouble() throws JMSException { 503 if (! RObody) 504 throw new MessageNotReadableException ("Can't read the message body as" 505 + " it is write-only."); 506 try { 507 return inputStream.readDouble(); 508 } catch (Exception e) { 509 JMSException jE = null; 510 if (e instanceof EOFException ) 511 jE = new MessageEOFException ("Unexpected end of bytes array."); 512 else if (e instanceof IOException) 513 jE = new JMSException ("Could not read the bytes array."); 514 jE.setLinkedException(e); 515 throw jE; 516 } 517 } 518 519 525 public int readBytes(byte[] value) throws JMSException { 526 if (! RObody) 527 throw new MessageNotReadableException ("Can't read the message body as" 528 + " it is write-only."); 529 int counter = 0; 530 531 try { 532 for (int i = 0; i < value.length; i ++) { 533 value[i] = inputStream.readByte(); 534 counter++; 535 } 536 } catch (EOFException eofE) { 537 } catch (IOException ioE) { 539 JMSException jE = null; 541 jE = new JMSException ("Could not read the bytes array."); 542 jE.setLinkedException(ioE); 543 throw jE; 544 } 545 if (counter == 0) 546 return -1; 547 return counter; 548 } 549 550 556 public int readBytes(byte[] value, int length) throws JMSException { 557 if (! RObody) 558 throw new MessageNotReadableException ("Can't read the message body as" 559 + " it is write-only."); 560 if (length > value.length || length < 0) 561 throw new IndexOutOfBoundsException ("Invalid length parameter: " 562 + length); 563 int counter = 0; 564 565 try { 566 for (int i = 0; i < length; i ++) { 567 value[i] = inputStream.readByte(); 568 counter++; 569 } 570 } catch (EOFException eofE) { 571 } catch (IOException ioE) { 573 JMSException jE = null; 575 jE = new JMSException ("Could not read the bytes array."); 576 jE.setLinkedException(ioE); 577 throw jE; 578 } 579 if (counter == 0) return -1; 580 581 return counter; 582 } 583 584 590 public String readUTF() throws JMSException { 591 if (! RObody) 592 throw new MessageNotReadableException ("Can't read the message body as" 593 + " it is write-only."); 594 try { 595 return inputStream.readUTF(); 596 } catch (Exception e) { 597 JMSException jE = null; 598 if (e instanceof EOFException ) 599 jE = new MessageEOFException ("Unexpected end of bytes array."); 600 else if (e instanceof IOException) 601 jE = new JMSException ("Could not read the bytes array."); 602 jE.setLinkedException(e); 603 throw jE; 604 } 605 } 606 607 613 public void reset() throws JMSException { 614 try { 615 if (! RObody) { 616 outputStream.flush(); 617 momMsg.body = outputBuffer.toByteArray(); 618 } else { 619 inputStream.close(); 620 } 621 inputStream = new DataInputStream(new ByteArrayInputStream(momMsg.body)); 622 623 RObody = true; 624 } catch (IOException iE) { 625 JMSException jE = 626 new JMSException ("Error while manipulating the stream facilities."); 627 jE.setLinkedException(iE); 628 throw jE; 629 } 630 } 631 632 638 protected void prepare() throws JMSException { 639 super.prepare(); 640 641 try { 642 if (! RObody) { 643 outputStream.flush(); 644 momMsg.body = outputBuffer.toByteArray(); 645 prepared = true; 646 } 647 } catch (IOException exc) { 648 MessageFormatException jExc = 649 new MessageFormatException ("The message body could not be serialized."); 650 jExc.setLinkedException(exc); 651 throw jExc; 652 } 653 } 654 } 655 | Popular Tags |