1 28 29 package com.caucho.jms.message; 30 31 import com.caucho.jms.JMSExceptionWrapper; 32 import com.caucho.util.CharBuffer; 33 import com.caucho.vfs.ReadStream; 34 import com.caucho.vfs.TempStream; 35 import com.caucho.vfs.WriteStream; 36 37 import javax.jms.BytesMessage ; 38 import javax.jms.JMSException ; 39 import javax.jms.MessageEOFException ; 40 import javax.jms.MessageFormatException ; 41 import java.io.IOException ; 42 import java.util.logging.Level ; 43 44 47 public class BytesMessageImpl extends MessageImpl implements BytesMessage { 48 private TempStream _tempStream; 49 private ReadStream _rs; 50 private WriteStream _ws; 51 52 55 public void setReceive() 56 throws JMSException 57 { 58 super.setReceive(); 59 60 reset(); 61 } 62 63 66 public void reset() 67 throws JMSException 68 { 69 setBodyReadOnly(); 70 71 try { 72 if (_ws != null) 74 _ws.close(); 75 76 if (_tempStream != null) { 77 if (_rs != null) 78 _rs.close(); 79 80 _rs = _tempStream.openRead(); 81 } 82 } catch (IOException e) { 83 throw new JMSExceptionWrapper(e); 84 } 85 } 86 87 90 public boolean readBoolean() 91 throws JMSException 92 { 93 ReadStream is = getReadStream(); 94 95 try { 96 return is.read() == 1; 97 } catch (IOException e) { 98 throw new JMSExceptionWrapper(e); 99 } 100 } 101 102 105 public byte readByte() 106 throws JMSException 107 { 108 ReadStream is = getReadStream(); 109 110 try { 111 return (byte) is.read(); 112 } catch (IOException e) { 113 throw new JMSExceptionWrapper(e); 114 } 115 } 116 117 120 public int readUnsignedByte() 121 throws JMSException 122 { 123 ReadStream is = getReadStream(); 124 125 if (is == null) 126 return -1; 127 128 try { 129 return is.read(); 130 } catch (IOException e) { 131 throw new JMSExceptionWrapper(e); 132 } 133 } 134 135 138 public short readShort() 139 throws JMSException 140 { 141 ReadStream is = getReadStream(); 142 143 try { 144 int d1 = is.read(); 145 int d2 = is.read(); 146 147 return (short) ((d1 << 8) + d2); 148 } catch (IOException e) { 149 throw new JMSExceptionWrapper(e); 150 } 151 } 152 153 156 public int readUnsignedShort() 157 throws JMSException 158 { 159 ReadStream is = getReadStream(); 160 161 try { 162 int d1 = is.read(); 163 int d2 = is.read(); 164 165 return ((d1 << 8) + d2); 166 } catch (IOException e) { 167 throw new JMSExceptionWrapper(e); 168 } 169 } 170 171 174 public int readInt() 175 throws JMSException 176 { 177 ReadStream is = getReadStream(); 178 179 try { 180 int d1 = is.read(); 181 int d2 = is.read(); 182 int d3 = is.read(); 183 int d4 = is.read(); 184 185 return (d1 << 24) + (d2 << 16) + (d3 << 8) + d4; 186 } catch (IOException e) { 187 throw new JMSExceptionWrapper(e); 188 } 189 } 190 191 194 public long readLong() 195 throws JMSException 196 { 197 ReadStream is = getReadStream(); 198 199 try { 200 long d1 = is.read(); 201 long d2 = is.read(); 202 long d3 = is.read(); 203 long d4 = is.read(); 204 long d5 = is.read(); 205 long d6 = is.read(); 206 long d7 = is.read(); 207 long d8 = is.read(); 208 209 return ((d1 << 56) + 210 (d2 << 48) + 211 (d3 << 40) + 212 (d4 << 32) + 213 (d5 << 24) + 214 (d6 << 16) + 215 (d7 << 8) + 216 (d8)); 217 } catch (IOException e) { 218 throw new JMSExceptionWrapper(e); 219 } 220 } 221 222 225 public float readFloat() 226 throws JMSException 227 { 228 return Float.intBitsToFloat(readInt()); 229 } 230 231 234 public double readDouble() 235 throws JMSException 236 { 237 return Double.longBitsToDouble(readLong()); 238 } 239 240 243 public char readChar() 244 throws JMSException 245 { 246 ReadStream is = getReadStream(); 247 248 try { 249 int d1 = is.read(); 250 int d2 = is.read(); 251 252 return (char) ((d1 << 8) + d2); 253 } catch (IOException e) { 254 throw new JMSExceptionWrapper(e); 255 } 256 } 257 258 261 public String readUTF() 262 throws JMSException 263 { 264 ReadStream is = getReadStream(); 265 CharBuffer cb = new CharBuffer(); 266 267 try { 268 int d1; 269 270 while ((d1 = is.read()) > 0) { 271 if (d1 < 0x80) 272 cb.append((char) d1); 273 else if ((d1 & 0xe0) == 0xc0) { 274 int d2 = is.read(); 275 276 cb.append((char) (((d1 & 0x1f) << 6) + (d2 & 0x3f))); 277 } 278 else if ((d1 & 0xf0) == 0xe0) { 279 int d2 = is.read(); 280 int d3 = is.read(); 281 282 cb.append((char) (((d1 & 0xf) << 12) + 283 ((d2 & 0x3f) << 6) + 284 (d3 & 0x3f))); 285 } 286 else 287 throw new MessageFormatException (L.l("invalid UTF-8 in bytes message")); 288 } 289 290 if (d1 < 0) 291 throw new MessageEOFException ("end of message in byte stream"); 292 } catch (JMSException e) { 293 throw e; 294 } catch (RuntimeException e) { 295 throw e; 296 } catch (Throwable e) { 297 throw new JMSExceptionWrapper(e); 298 } 299 300 return cb.toString(); 301 } 302 303 306 public int readBytes(byte []value) 307 throws JMSException 308 { 309 ReadStream is = getReadStream(); 310 311 try { 312 return is.read(value); 313 } catch (IOException e) { 314 throw new JMSExceptionWrapper(e); 315 } 316 } 317 318 321 public int readBytes(byte []value, int length) 322 throws JMSException 323 { 324 ReadStream is = getReadStream(); 325 326 try { 327 return is.read(value, 0, length); 328 } catch (IOException e) { 329 throw new JMSExceptionWrapper(e); 330 } 331 } 332 333 protected ReadStream getReadStream() 334 throws JMSException 335 { 336 checkBodyReadable(); 337 338 342 343 return _rs; 344 } 345 346 349 public void clearBody() 350 throws JMSException 351 { 352 super.clearBody(); 353 354 _ws = null; 355 _tempStream = null; 356 _rs = null; 357 } 358 359 362 public void writeBoolean(boolean b) 363 throws JMSException 364 { 365 try { 366 getWriteStream().write(b ? 1 : 0); 367 } catch (IOException e) { 368 throw new JMSExceptionWrapper(e); 369 } 370 } 371 372 375 public void writeByte(byte b) 376 throws JMSException 377 { 378 try { 379 getWriteStream().write(b); 380 } catch (IOException e) { 381 throw new JMSExceptionWrapper(e); 382 } 383 } 384 385 388 public void writeShort(short s) 389 throws JMSException 390 { 391 try { 392 WriteStream ws = getWriteStream(); 393 394 ws.write(s >> 8); 395 ws.write(s); 396 } catch (IOException e) { 397 throw new JMSExceptionWrapper(e); 398 } 399 } 400 401 404 public void writeInt(int i) 405 throws JMSException 406 { 407 try { 408 WriteStream ws = getWriteStream(); 409 410 ws.write(i >> 24); 411 ws.write(i >> 16); 412 ws.write(i >> 8); 413 ws.write(i); 414 } catch (IOException e) { 415 throw new JMSExceptionWrapper(e); 416 } 417 } 418 419 422 public void writeLong(long l) 423 throws JMSException 424 { 425 try { 426 WriteStream ws = getWriteStream(); 427 428 ws.write((int) (l >> 56)); 429 ws.write((int) (l >> 48)); 430 ws.write((int) (l >> 40)); 431 ws.write((int) (l >> 32)); 432 ws.write((int) (l >> 24)); 433 ws.write((int) (l >> 16)); 434 ws.write((int) (l >> 8)); 435 ws.write((int) l); 436 } catch (IOException e) { 437 throw new JMSExceptionWrapper(e); 438 } 439 } 440 441 444 public void writeFloat(float f) 445 throws JMSException 446 { 447 int i = Float.floatToIntBits(f); 448 writeInt(i); 449 } 450 451 454 public void writeDouble(double d) 455 throws JMSException 456 { 457 long l = Double.doubleToLongBits(d); 458 writeLong(l); 459 } 460 461 464 public void writeUTF(String s) 465 throws JMSException 466 { 467 try { 468 WriteStream ws = getWriteStream(); 469 470 int len = s.length(); 471 for (int i = 0; i < len; i++) { 472 int ch = s.charAt(i); 473 474 if (ch == 0) { 475 ws.write(0xc0); 476 ws.write(0x80); 477 } 478 else if (ch < 0x80) 479 ws.write(ch); 480 else if (ch < 0x800) { 481 ws.write(0xc0 + ((ch >> 6) & 0x1f)); 482 ws.write(0x80 + (ch & 0x3f)); 483 } 484 else if (ch < 0x8000) { 485 ws.write(0xe0 + ((ch >> 12) & 0x0f)); 486 ws.write(0x80 + ((ch >> 6) & 0x3f)); 487 ws.write(0x80 + (ch & 0x3f)); 488 } 489 } 490 491 ws.write(0); 492 } catch (IOException e) { 493 throw new JMSExceptionWrapper(e); 494 } 495 } 496 497 500 public void writeChar(char ch) 501 throws JMSException 502 { 503 try { 504 WriteStream ws = getWriteStream(); 505 506 ws.write(ch >> 8); 507 ws.write(ch); 508 } catch (IOException e) { 509 throw new JMSExceptionWrapper(e); 510 } 511 } 512 513 516 public void writeBytes(byte []buf) 517 throws JMSException 518 { 519 writeBytes(buf, 0, buf.length); 520 } 521 522 525 public void writeBytes(byte []buf, int offset, int length) 526 throws JMSException 527 { 528 try { 529 WriteStream ws = getWriteStream(); 530 531 ws.write(buf, offset, length); 532 } catch (IOException e) { 533 throw new JMSExceptionWrapper(e); 534 } 535 } 536 537 540 public void writeObject(Object obj) 541 throws JMSException 542 { 543 if (obj instanceof Boolean ) 544 writeBoolean(((Boolean ) obj).booleanValue()); 545 else if (obj instanceof Byte ) 546 writeByte(((Byte ) obj).byteValue()); 547 else if (obj instanceof Short ) 548 writeShort(((Short ) obj).shortValue()); 549 else if (obj instanceof Character ) 550 writeChar(((Character ) obj).charValue()); 551 else if (obj instanceof Integer ) 552 writeInt(((Integer ) obj).intValue()); 553 else if (obj instanceof Long ) 554 writeLong(((Long ) obj).longValue()); 555 else if (obj instanceof Float ) 556 writeFloat(((Float ) obj).floatValue()); 557 else if (obj instanceof Double ) 558 writeDouble(((Double ) obj).doubleValue()); 559 else if (obj instanceof String ) 560 writeUTF((String ) obj); 561 else if (obj instanceof byte[]) 562 writeBytes((byte[]) obj); 563 else 564 throw new JMSException ("jms"); 565 } 566 567 public long getBodyLength() 568 throws JMSException 569 { 570 if (_tempStream == null) 571 return 0; 572 else 573 return _tempStream.getLength(); 574 } 575 576 protected WriteStream getWriteStream() 577 throws JMSException 578 { 579 checkBodyWriteable(); 580 581 if (_tempStream == null) 582 _tempStream = new TempStream(null); 583 584 if (_ws == null) 585 _ws = new WriteStream(_tempStream); 586 587 return _ws; 588 } 589 590 public MessageImpl copy() 591 { 592 BytesMessageImpl msg = new BytesMessageImpl(); 593 594 copy(msg); 595 596 return msg; 597 } 598 599 protected void copy(BytesMessageImpl newMsg) 600 { 601 super.copy(newMsg); 602 603 try { 604 if (_ws != null) 605 _ws.flush(); 606 607 if (_tempStream != null) 608 newMsg._tempStream = _tempStream.copy(); 609 } catch (Exception e) { 610 log.log(Level.WARNING, e.toString(), e); 611 } 612 } 613 } 614 615 | Popular Tags |