1 30 31 package org.objectweb.fractal.rmi.io; 32 33 import org.objectweb.jonathan.apis.binding.NamingContext; 34 import org.objectweb.jonathan.apis.kernel.Context; 35 import org.objectweb.jonathan.apis.kernel.ContextFactory; 36 import org.objectweb.jonathan.apis.kernel.InternalException; 37 import org.objectweb.jonathan.apis.kernel.JonathanException; 38 import org.objectweb.jonathan.apis.presentation.EndOfMessageException; 39 import org.objectweb.jonathan.apis.presentation.UnMarshaller; 40 import org.objectweb.jonathan.apis.resources.Chunk; 41 import org.objectweb.jonathan.apis.resources.ChunkProvider; 42 43 import java.io.IOException ; 44 import java.io.InputStream ; 45 import java.io.ObjectInputStream ; 46 47 51 52 public class RmiUnMarshaller extends InputStream implements UnMarshaller { 53 54 private final static Chunk EMPTY_CHUNK = new Chunk(new byte[0], 0, 0); 55 56 59 60 protected NamingContext domain; 61 62 66 67 protected ContextFactory contextFactory; 68 69 74 75 protected ObjectInputStream is; 76 77 private ChunkProvider provider; 78 79 private Chunk first; 80 81 private int offset; 82 83 private int lastOffset; 84 85 private Context context; 86 87 91 100 101 public RmiUnMarshaller ( 102 NamingContext domain, 103 ContextFactory contextFactory, 104 Chunk chunk, 105 int read) 106 { 107 super(); 108 this.domain = domain; 109 this.contextFactory = contextFactory; 110 this.first = chunk; 111 offset = chunk.offset; 112 lastOffset = read - offset; 113 context = null; 114 } 115 116 124 125 public RmiUnMarshaller ( 126 final NamingContext domain, 127 final ContextFactory contextFactory, 128 final ChunkProvider provider) 129 { 130 this(domain, contextFactory, EMPTY_CHUNK, 0); 131 this.provider = provider; 132 } 133 134 138 143 144 public boolean isLittleEndian () { 145 return false; 146 } 147 148 153 154 public void setByteOrder (final boolean littleEndian) { 155 throw new RuntimeException ("Not implemented."); 156 } 157 158 163 164 public final Context getContext () { 165 if (context == null) { 166 try { 167 context = contextFactory.newContext(); 168 } catch (NullPointerException e) { 169 e.printStackTrace(System.err); 170 throw new InternalException("Context factory required."); 171 } 172 } 173 return context; 174 } 175 176 181 182 public int bytesRead () { 183 return lastOffset + offset; 184 } 185 186 197 198 public void setSize (int size) throws JonathanException { 199 boolean sz0 = (size == 0); 200 Chunk result = null, current = null; 201 int lastOff = lastOffset + offset; 202 int available = first.top - offset; 203 first.offset = offset; 204 while (size > available) { 205 if (available > 0) { 206 if (current == null) { 207 result = first.duplicate(); 208 current = result; 209 } else { 210 current.next = first.duplicate(); 211 current = current.next; 212 } 213 size -= available; 214 offset = first.top; 215 } 216 prepare(); 217 available = first.top - offset; 218 } 219 if (size > 0) { 220 if (current == null) { 221 result = first.duplicate(offset,offset + size); 222 } else { 223 current.next = first.duplicate(offset,offset + size); 224 } 225 offset += size; 226 } 227 Context thisContext = context; 228 if (context != null) { 229 context.acquire(); 230 } 231 close(); 232 context = thisContext; 233 if (sz0) { 234 first = EMPTY_CHUNK; 235 } else { 236 first = result; 237 } 238 offset = first.offset; 239 lastOffset = lastOff - offset; 240 } 241 242 249 250 public InputStream inputStream () { 251 return this; 252 } 253 254 261 262 public byte readByte () throws JonathanException { 263 if (first.top == offset) { 264 prepare(); 265 } 266 return first.data[offset++]; 267 } 268 269 276 277 public boolean readBoolean () throws JonathanException { 278 if (first.top == offset) { 279 prepare(); 280 } 281 return first.data[offset++] != 0; 282 } 283 284 291 292 public char readChar8 () throws JonathanException { 293 if (first.top == offset) { 294 prepare(); 295 } 296 return (char) (first.data[offset++] & 0xFF); 297 } 298 299 306 307 public char readChar16 () throws JonathanException { 308 byte[] data = first.data; 309 int val = 0; 310 if (first.top - offset >= 2) { 311 val = ((data[offset] & 0xFF) << 8) + (data[offset + 1] & 0xFF); 312 offset += 2; 313 } else { 314 int m = 1, p; 315 while (m >= 0) { 316 p = Math.min(m + 1, first.top-offset); 317 if (p == 0) { 318 prepare(); 319 data = first.data; 320 } else { 321 while (--p >= 0) { 322 val += (data[offset++] & 0xFF) << (m-- * 8); 323 } 324 } 325 } 326 } 327 return (char) val; 328 } 329 330 337 338 public short readShort () throws JonathanException { 339 byte[] data = first.data; 340 int val = 0; 341 if (first.top - offset >= 2) { 342 val = ((data[offset] & 0xFF) << 8) + (data[offset + 1] & 0xFF); 343 offset += 2; 344 } else { 345 int m = 1, p; 346 while (m >= 0) { 347 p = Math.min(m + 1, first.top-offset); 348 if (p == 0) { 349 prepare(); 350 data = first.data; 351 } else { 352 while (--p >= 0) { 353 val += (data[offset++] & 0xFF) << (m-- * 8); 354 } 355 } 356 } 357 } 358 return (short) val; 359 } 360 367 368 public int readInt () throws JonathanException { 369 byte[] data = first.data; 370 int val = 0; 371 if (first.top - offset >= 4) { 372 val = 373 ((data[offset] & 0xFF) << 24) + 374 ((data[offset + 1] & 0xFF) << 16) + 375 ((data[offset + 2] & 0xFF) << 8) + 376 (data[offset + 3] & 0xFF); 377 offset += 4; 378 } else { 379 int m = 3, p; 380 while (m >= 0) { 381 p = Math.min(m + 1, first.top-offset); 382 if (p == 0) { 383 prepare(); 384 data = first.data; 385 } else { 386 while (--p >= 0) { 387 val += (data[offset++] & 0xFF) << (m-- * 8); 388 } 389 } 390 } 391 } 392 return val; 393 } 394 395 402 403 public float readFloat () throws JonathanException { 404 byte[] data = first.data; 405 int val = 0; 406 if (first.top - offset >= 4) { 407 val = 408 ((data[offset] & 0xFF) << 24) + 409 ((data[offset + 1] & 0xFF) << 16) + 410 ((data[offset + 2] & 0xFF) << 8) + 411 (data[offset + 3] & 0xFF); 412 offset += 4; 413 } else { 414 int m = 3, p; 415 while (m >= 0) { 416 p = Math.min(m + 1, first.top-offset); 417 if (p == 0) { 418 prepare(); 419 data = first.data; 420 } else { 421 while (--p >= 0) { 422 val += (data[offset++] & 0xFF) << (m-- * 8); 423 } 424 } 425 } 426 } 427 return Float.intBitsToFloat(val); 428 } 429 430 437 438 public long readLong () throws JonathanException { 439 byte[] data = first.data; 440 long val = 0; 441 if (first.top - offset >= 8) { 442 val = 443 (((long)data[offset] & 0xFF) << 56) + 444 (((long)data[offset + 1] & 0xFF) << 48) + 445 (((long)data[offset + 2] & 0xFF) << 40) + 446 (((long)data[offset + 3] & 0xFF) << 32) + 447 (((long)data[offset + 4] & 0xFF) << 24) + 448 (((long)data[offset + 5] & 0xFF) << 16) + 449 (((long)data[offset + 6] & 0xFF) << 8) + 450 ((long)data[offset + 7] & 0xFF); 451 offset += 8; 452 } else { 453 int m = 7, p; 454 while (m >= 0) { 455 p = Math.min(m + 1,first.top-offset); 456 if (p == 0) { 457 prepare(); 458 data = first.data; 459 } else { 460 while (--p >= 0) { 461 val += (((long) data[offset++]) & 0xFF) << (m-- * 8); 462 } 463 464 } 465 } 466 } 467 return val; 468 } 469 470 477 478 public double readDouble () throws JonathanException { 479 byte[] data = first.data; 480 long val = 0; 481 if (first.top - offset >= 8) { 482 val = 483 (((long)data[offset] & 0xFF) << 56) + 484 (((long)data[offset + 1] & 0xFF) << 48) + 485 (((long)data[offset + 2] & 0xFF) << 40) + 486 (((long)data[offset + 3] & 0xFF) << 32) + 487 (((long)data[offset + 4] & 0xFF) << 24) + 488 (((long)data[offset + 5] & 0xFF) << 16) + 489 (((long)data[offset + 6] & 0xFF) << 8) + 490 ((long)data[offset + 7] & 0xFF); 491 offset += 8; 492 return Double.longBitsToDouble(val); 493 } else { 494 int m = 7, p; 495 while (m >= 0) { 496 p = Math.min(m + 1,first.top-offset); 497 if (p == 0) { 498 prepare(); 499 data = first.data; 500 } else { 501 while (--p >= 0) { 502 val += (((long) data[offset++]) & 0xFF) << (m-- * 8); 503 } 504 } 505 } 506 return Double.longBitsToDouble(val); 507 } 508 } 509 510 517 518 public String readString8 () throws JonathanException { 519 int len = readInt() - 1; 520 char[] tab = new char[len]; 521 byte[] data = first.data; 522 int off = 0; 523 int max = first.top - offset; 524 while (max < len) { 526 while (off < max) { 527 tab[off++] = (char) (data[offset++] & 0xFF); 528 } 529 prepare(); 530 data = first.data; 531 max = first.top - offset + off; 532 } 533 while (off < len) { 534 tab[off++] = (char) (data[offset++] & 0xFF); 535 } 536 if (first.top == offset) { 537 prepare(); 538 offset++; 539 } else { 540 offset++; 541 } 542 return new String (tab); 543 } 544 545 552 553 public String readString16 () throws JonathanException { 554 throw new InternalException("Not implemented."); 555 } 556 557 566 567 public void readByteArray (final byte[] array, int off, int len) 568 throws JonathanException 569 { 570 byte[] data = first.data; 571 int max = first.top - offset; 572 while (max < len) { 573 System.arraycopy(data,offset,array,off,max); 574 offset += max; 575 off += max; 576 len -= max; 577 prepare(); 578 data = first.data; 579 max = first.top - offset; 580 } 581 System.arraycopy(data,offset,array,off,len); 582 offset += len; 583 } 584 585 592 593 public Object readReference () throws JonathanException { 594 try { 595 initObjectStream(); 596 return is.readObject(); 597 } catch (Exception e) { 598 System.err.println(Thread.currentThread() + " Exception " + e); 599 throw new JonathanException(e); 600 } 601 } 602 603 610 611 public Object readValue () throws JonathanException { 612 try { 613 initObjectStream(); 614 return is.readObject(); 615 } catch (Exception e) { 616 System.err.println(Thread.currentThread() + " Exception " + e); 617 throw new JonathanException(e); 618 } 619 } 620 621 625 public int available () throws IOException { 626 Chunk chunk = first; 627 int size = chunk.top - offset; 628 chunk = chunk.next; 629 while (chunk != null) { 630 size += chunk.top - chunk.offset; 631 chunk = chunk.next; 632 } 633 return size; 634 } 635 636 public int read () throws IOException { 637 try { 638 if (first.top == offset) { 639 prepare(); 640 } 641 return first.data[offset++] & 0xFF; 642 } catch (JonathanException e) { 643 Throwable f = e.represents(); 644 if (f instanceof EndOfMessageException) { 645 return -1; 646 } else if (f instanceof IOException ) { 647 throw (IOException ) f; 648 } else { 649 throw new IOException (f.getMessage()); 650 } 651 } 652 } 653 654 public int read (final byte[] array, int off, int length) throws IOException { 655 try { 656 if (array == null) { 657 return (int) skip(length); 658 } else { 659 int len = length, toCopy, available; 660 while(len > 0) { 661 available = first.top - offset; 662 if (available == 0) { 663 prepare(); 664 available = first.top - offset; 665 } 666 toCopy = Math.min(available,len); 667 System.arraycopy(first.data,offset,array,off,toCopy); 668 offset += toCopy; 669 off += toCopy; 670 len -= toCopy; 671 } 672 return length; 673 } 674 } catch (JonathanException e) { 675 Throwable f = e.represents(); 676 if (f instanceof EndOfMessageException) { 677 return -1; 678 } else if (f instanceof IOException ) { 679 throw (IOException ) f; 680 } else { 681 throw new IOException (f.getMessage()); 682 } 683 } 684 } 685 686 public long skip (final long n) throws IOException { 687 try { 688 long len = n, toSkip; 689 int available; 690 while(len > 0) { 691 available = first.top - offset; 692 if (available == 0) { 693 prepare(); 694 available = first.top - offset; 695 } 696 toSkip = Math.min(available,len); 697 offset += toSkip; 698 len -= toSkip; 699 } 700 return n; 701 } catch (JonathanException e) { 702 Throwable f = e.represents(); 703 if (f instanceof EndOfMessageException) { 704 return -1; 705 } else if (f instanceof IOException ) { 706 throw (IOException ) f; 707 } else { 708 throw new IOException (f.getMessage()); 709 } 710 } 711 } 712 713 717 718 public void close () { 719 if (first != null) { 720 first.offset = offset; 721 } 722 Chunk next; 723 while (first != null) { 724 next = first.next; 725 first.release(); 726 first = next; 727 } 728 if (provider != null) { 729 provider.close(); 730 provider = null; 731 } 732 if (context != null) { 733 context.release(); 734 context = null; 735 } 736 } 737 738 742 748 749 protected void initObjectStream () throws IOException { 750 if (is == null) { 751 is = new RmiObjectInputStream(this, domain, contextFactory); 752 } 753 } 754 755 private void prepare () throws JonathanException { 756 lastOffset += offset; 757 first.offset = offset; 758 Chunk prev; 759 while (first != null && first.top == first.offset) { 760 prev = first; 761 first = first.next; 762 prev.release(); 763 } 764 if (first != null) { 765 offset = first.offset; 766 lastOffset -= offset; 767 } else if (provider != null) { 768 first = provider.prepare(); 769 offset = first.offset; 770 lastOffset -= offset; 771 } else { 772 throw new EndOfMessageException(); 773 } 774 } 775 } 776 | Popular Tags |