1 48 49 package com.caucho.burlap.io; 50 51 import com.caucho.hessian.io.Serializer; 52 import com.caucho.hessian.io.SerializerFactory; 53 54 import java.io.IOException ; 55 import java.io.OutputStream ; 56 import java.util.Calendar ; 57 import java.util.Date ; 58 import java.util.IdentityHashMap ; 59 import java.util.TimeZone ; 60 61 81 public class BurlapOutput extends AbstractBurlapOutput { 82 protected OutputStream os; 84 private IdentityHashMap _refs; 86 87 private Date date; 88 private Calendar utcCalendar; 89 private Calendar localCalendar; 90 96 public BurlapOutput(OutputStream os) 97 { 98 init(os); 99 } 100 101 104 public BurlapOutput() 105 { 106 } 107 108 111 public void init(OutputStream os) 112 { 113 this.os = os; 114 115 _refs = null; 116 117 if (_serializerFactory == null) 118 _serializerFactory = new SerializerFactory(); 119 } 120 121 124 public void call(String method, Object []args) 125 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 126 { 127 startCall(method); 128 129 if (args != null) { 130 for (int i = 0; i < args.length; i++) 131 writeObject(args[i]); 132 } 133 134 completeCall(); 135 } 136 137 149 public void startCall(String method) 150 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 151 { 152 print("<burlap:call><method>"); 153 print(method); 154 print("</method>"); 155 } 156 157 168 public void startCall() 169 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 170 { 171 print("<burlap:call>"); 172 } 173 174 183 public void writeMethod(String method) 184 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 185 { 186 print("<method>"); 187 print(method); 188 print("</method>"); 189 } 190 191 192 199 public void completeCall() 200 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 201 { 202 print("</burlap:call>"); 203 } 204 205 214 public void startReply() 215 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 216 { 217 print("<burlap:reply>"); 218 } 219 220 229 public void completeReply() 230 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 231 { 232 print("</burlap:reply>"); 233 } 234 235 242 public void writeHeader(String name) 243 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 244 { 245 print("<header>"); 246 printString(name); 247 print("</header>"); 248 } 249 250 271 public void writeFault(String code, String message, Object detail) 272 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 273 { 274 print("<fault>"); 275 writeString("code"); 276 writeString(code); 277 278 writeString("message"); 279 writeString(message); 280 281 if (detail != null) { 282 writeString("detail"); 283 writeObject(detail); 284 } 285 print("</fault>"); 286 } 287 288 291 public void writeObject(Object object) 292 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 293 { 294 if (object == null) { 295 writeNull(); 296 return; 297 } 298 299 Serializer serializer; 300 301 serializer = _serializerFactory.getSerializer(object.getClass()); 302 303 serializer.writeObject(object, this); 304 } 305 306 321 public boolean writeListBegin(int length, String type) 322 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 323 { 324 print("<list><type>"); 325 326 if (type != null) 327 print(type); 328 329 print("</type><length>"); 330 print(length); 331 print("</length>"); 332 333 return true; 334 } 335 336 339 public void writeListEnd() 340 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 341 { 342 print("</list>"); 343 } 344 345 357 public void writeMapBegin(String type) 358 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 359 { 360 print("<map><type>"); 361 if (type != null) 362 print(type); 363 364 print("</type>"); 365 } 366 367 370 public void writeMapEnd() 371 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 372 { 373 print("</map>"); 374 } 375 376 387 public void writeRemote(String type, String url) 388 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 389 { 390 print("<remote><type>"); 391 print(type); 392 print("</type><string>"); 393 print(url); 394 print("</string></remote>"); 395 } 396 397 408 public void writeBoolean(boolean value) 409 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 410 { 411 if (value) 412 print("<boolean>1</boolean>"); 413 else 414 print("<boolean>0</boolean>"); 415 } 416 417 427 public void writeInt(int value) 428 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 429 { 430 print("<int>"); 431 print(value); 432 print("</int>"); 433 } 434 435 445 public void writeLong(long value) 446 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 447 { 448 print("<long>"); 449 print(value); 450 print("</long>"); 451 } 452 453 463 public void writeDouble(double value) 464 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 465 { 466 print("<double>"); 467 print(value); 468 print("</double>"); 469 } 470 471 480 public void writeUTCDate(long time) 481 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 482 { 483 print("<date>"); 484 if (utcCalendar == null) { 485 utcCalendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); 486 date = new Date (); 487 } 488 489 date.setTime(time); 490 utcCalendar.setTime(date); 491 492 printDate(utcCalendar); 493 print("</date>"); 494 } 495 496 506 public void writeNull() 507 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 508 { 509 print("<null></null>"); 510 } 511 512 528 public void writeString(String value) 529 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 530 { 531 if (value == null) { 532 print("<null></null>"); 533 } 534 else { 535 print("<string>"); 536 printString(value); 537 print("</string>"); 538 } 539 } 540 541 557 public void writeString(char []buffer, int offset, int length) 558 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 559 { 560 if (buffer == null) { 561 print("<null></null>"); 562 } 563 else { 564 print("<string>"); 565 printString(buffer, offset, length); 566 print("</string>"); 567 } 568 } 569 570 586 public void writeBytes(byte []buffer) 587 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 588 { 589 if (buffer == null) 590 print("<null></null>"); 591 else 592 writeBytes(buffer, 0, buffer.length); 593 } 594 610 public void writeBytes(byte []buffer, int offset, int length) 611 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 612 { 613 if (buffer == null) { 614 print("<null></null>"); 615 } 616 else { 617 print("<base64>"); 618 619 int i = 0; 620 for (; i + 2 < length; i += 3) { 621 if (i != 0 && (i & 0x3f) == 0) 622 print('\n'); 623 624 int v = (((buffer[offset + i] & 0xff) << 16) + 625 ((buffer[offset + i + 1] & 0xff) << 8) + 626 (buffer[offset + i + 2] & 0xff)); 627 628 print(encode(v >> 18)); 629 print(encode(v >> 12)); 630 print(encode(v >> 6)); 631 print(encode(v)); 632 } 633 634 if (i + 1 < length) { 635 int v = (((buffer[offset + i] & 0xff) << 8) + 636 (buffer[offset + i + 1] & 0xff)); 637 638 print(encode(v >> 10)); 639 print(encode(v >> 4)); 640 print(encode(v << 2)); 641 print('='); 642 } 643 else if (i < length) { 644 int v = buffer[offset + i] & 0xff; 645 646 print(encode(v >> 2)); 647 print(encode(v << 4)); 648 print('='); 649 print('='); 650 } 651 652 print("</base64>"); 653 } 654 } 655 656 659 public void writeByteBufferStart() 660 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 661 { 662 throw new UnsupportedOperationException (); 663 } 664 665 672 public void writeByteBufferPart(byte []buffer, int offset, int length) 673 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 674 { 675 throw new UnsupportedOperationException (); 676 } 677 678 685 public void writeByteBufferEnd(byte []buffer, int offset, int length) 686 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 687 { 688 throw new UnsupportedOperationException (); 689 } 690 691 694 private char encode(int d) 695 { 696 d &= 0x3f; 697 if (d < 26) 698 return (char) (d + 'A'); 699 else if (d < 52) 700 return (char) (d + 'a' - 26); 701 else if (d < 62) 702 return (char) (d + '0' - 52); 703 else if (d == 62) 704 return '+'; 705 else 706 return '/'; 707 } 708 709 718 public void writeRef(int value) 719 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 720 { 721 print("<ref>"); 722 print(value); 723 print("</ref>"); 724 } 725 726 731 public boolean addRef(Object object) 732 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 733 { 734 if (_refs == null) 735 _refs = new IdentityHashMap (); 736 737 Integer ref = (Integer ) _refs.get(object); 738 739 if (ref != null) { 740 int value = ref.intValue(); 741 742 writeRef(value); 743 return true; 744 } 745 else { 746 _refs.put(object, new Integer (_refs.size())); 747 748 return false; 749 } 750 } 751 752 755 public boolean removeRef(Object obj) 756 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 757 { 758 if (_refs != null) { 759 _refs.remove(obj); 760 761 return true; 762 } 763 else 764 return false; 765 } 766 767 770 public boolean replaceRef(Object oldRef, Object newRef) 771 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 772 { 773 Integer value = (Integer ) _refs.remove(oldRef); 774 775 if (value != null) { 776 _refs.put(newRef, value); 777 return true; 778 } 779 else 780 return false; 781 } 782 783 788 public void printString(String v) 789 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 790 { 791 printString(v, 0, v.length()); 792 } 793 794 799 public void printString(String v, int offset, int length) 800 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 801 { 802 for (int i = 0; i < length; i++) { 803 char ch = v.charAt(i + offset); 804 805 if (ch == '<') { 806 os.write('&'); 807 os.write('#'); 808 os.write('6'); 809 os.write('0'); 810 os.write(';'); 811 } 812 else if (ch == '&') { 813 os.write('&'); 814 os.write('#'); 815 os.write('3'); 816 os.write('8'); 817 os.write(';'); 818 } 819 else if (ch < 0x80) 820 os.write(ch); 821 else if (ch < 0x800) { 822 os.write(0xc0 + ((ch >> 6) & 0x1f)); 823 os.write(0x80 + (ch & 0x3f)); 824 } 825 else { 826 os.write(0xe0 + ((ch >> 12) & 0xf)); 827 os.write(0x80 + ((ch >> 6) & 0x3f)); 828 os.write(0x80 + (ch & 0x3f)); 829 } 830 } 831 } 832 833 838 public void printString(char []v, int offset, int length) 839 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 840 { 841 for (int i = 0; i < length; i++) { 842 char ch = v[i + offset]; 843 844 if (ch < 0x80) 845 os.write(ch); 846 else if (ch < 0x800) { 847 os.write(0xc0 + ((ch >> 6) & 0x1f)); 848 os.write(0x80 + (ch & 0x3f)); 849 } 850 else { 851 os.write(0xe0 + ((ch >> 12) & 0xf)); 852 os.write(0x80 + ((ch >> 6) & 0x3f)); 853 os.write(0x80 + (ch & 0x3f)); 854 } 855 } 856 } 857 858 863 public void printDate(Calendar calendar) 864 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 865 { 866 int year = calendar.get(Calendar.YEAR); 867 868 os.write((char) ('0' + (year / 1000 % 10))); 869 os.write((char) ('0' + (year / 100 % 10))); 870 os.write((char) ('0' + (year / 10 % 10))); 871 os.write((char) ('0' + (year % 10))); 872 873 int month = calendar.get(Calendar.MONTH) + 1; 874 os.write((char) ('0' + (month / 10 % 10))); 875 os.write((char) ('0' + (month % 10))); 876 877 int day = calendar.get(Calendar.DAY_OF_MONTH); 878 os.write((char) ('0' + (day / 10 % 10))); 879 os.write((char) ('0' + (day % 10))); 880 881 os.write('T'); 882 883 int hour = calendar.get(Calendar.HOUR_OF_DAY); 884 os.write((char) ('0' + (hour / 10 % 10))); 885 os.write((char) ('0' + (hour % 10))); 886 887 int minute = calendar.get(Calendar.MINUTE); 888 os.write((char) ('0' + (minute / 10 % 10))); 889 os.write((char) ('0' + (minute % 10))); 890 891 int second = calendar.get(Calendar.SECOND); 892 os.write((char) ('0' + (second / 10 % 10))); 893 os.write((char) ('0' + (second % 10))); 894 895 int ms = calendar.get(Calendar.MILLISECOND); 896 os.write('.'); 897 os.write((char) ('0' + (ms / 100 % 10))); 898 os.write((char) ('0' + (ms / 10 % 10))); 899 os.write((char) ('0' + (ms % 10))); 900 901 os.write('Z'); 902 } 903 904 909 protected void print(char v) 910 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 911 { 912 os.write(v); 913 } 914 915 920 protected void print(int v) 921 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 922 { 923 print(String.valueOf(v)); 924 } 925 926 931 protected void print(long v) 932 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 933 { 934 print(String.valueOf(v)); 935 } 936 937 942 protected void print(double v) 943 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 944 { 945 print(String.valueOf(v)); 946 } 947 948 954 protected void print(String s) 955 throws IOException ![JavaDoc](../../../../../cmn/javadoc.gif) 956 { 957 int len = s.length(); 958 for (int i = 0; i < len; i++) { 959 int ch = s.charAt(i); 960 961 os.write(ch); 962 } 963 } 964 } 965 | Popular Tags |