1 48 49 package com.caucho.burlap.client; 50 51 import java.io.IOException ; 52 import java.io.OutputStream ; 53 import java.util.Calendar ; 54 import java.util.Date ; 55 import java.util.Enumeration ; 56 import java.util.Hashtable ; 57 import java.util.TimeZone ; 58 import java.util.Vector ; 59 60 81 public class MicroBurlapOutput { 82 private OutputStream os; 83 private Date date; 84 private Calendar utcCalendar; 85 private Calendar localCalendar; 86 87 93 public MicroBurlapOutput(OutputStream os) 94 { 95 init(os); 96 } 97 98 101 public MicroBurlapOutput() 102 { 103 } 104 105 public void init(OutputStream os) 106 { 107 this.os = os; 108 } 109 110 113 public void call(String method, Object []args) 114 throws IOException 115 { 116 startCall(method); 117 118 if (args != null) { 119 for (int i = 0; i < args.length; i++) 120 writeObject(args[i]); 121 } 122 123 completeCall(); 124 } 125 126 136 public void startCall(String method) 137 throws IOException 138 { 139 print("<burlap:call><method>"); 140 print(method); 141 print("</method>"); 142 } 143 144 151 public void completeCall() 152 throws IOException 153 { 154 print("</burlap:call>"); 155 } 156 157 167 public void writeBoolean(boolean value) 168 throws IOException 169 { 170 print("<boolean>"); 171 printInt(value ? 1 : 0); 172 print("</boolean>"); 173 } 174 175 185 public void writeInt(int value) 186 throws IOException 187 { 188 print("<int>"); 189 printInt(value); 190 print("</int>"); 191 } 192 193 203 public void writeLong(long value) 204 throws IOException 205 { 206 print("<long>"); 207 printLong(value); 208 print("</long>"); 209 } 210 211 221 public void writeNull() 222 throws IOException 223 { 224 print("<null></null>"); 225 } 226 227 243 public void writeString(String value) 244 throws IOException 245 { 246 if (value == null) { 247 print("<null></null>"); 248 } 249 else { 250 print("<string>"); 251 printString(value); 252 print("</string>"); 253 } 254 } 255 256 272 public void writeBytes(byte []buffer, int offset, int length) 273 throws IOException 274 { 275 if (buffer == null) { 276 print("<null></null>"); 277 } 278 else { 279 print("<base64>"); 280 printBytes(buffer, offset, length); 281 print("</base64>"); 282 } 283 } 284 285 294 public void writeUTCDate(long time) 295 throws IOException 296 { 297 print("<date>"); 298 if (utcCalendar == null) { 299 utcCalendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); 300 date = new Date (); 301 } 302 303 date.setTime(time); 304 utcCalendar.setTime(date); 305 306 printDate(utcCalendar); 307 print("</date>"); 308 } 309 310 319 public void writeLocalDate(long time) 320 throws IOException 321 { 322 print("<date>"); 323 if (localCalendar == null) { 324 localCalendar = Calendar.getInstance(); 325 date = new Date (); 326 } 327 328 date.setTime(time); 329 localCalendar.setTime(date); 330 331 printDate(localCalendar); 332 print("</date>"); 333 } 334 335 344 public void writeRef(int value) 345 throws IOException 346 { 347 print("<ref>"); 348 printInt(value); 349 print("</ref>"); 350 } 351 352 369 public void writeObject(Object object) 370 throws IOException 371 { 372 if (object == null) 373 writeNull(); 374 else if (object instanceof String ) 375 writeString((String ) object); 376 else if (object instanceof Boolean ) 377 writeBoolean(((Boolean ) object).booleanValue()); 378 else if (object instanceof Integer ) 379 writeInt(((Integer ) object).intValue()); 380 else if (object instanceof Long ) 381 writeLong(((Long ) object).longValue()); 382 else if (object instanceof Date ) 383 writeUTCDate(((Date ) object).getTime()); 384 else if (object instanceof byte[]) { 385 byte []data = (byte []) object; 386 writeBytes(data, 0, data.length); 387 } 388 else if (object instanceof Vector ) { 389 Vector vector = (Vector ) object; 390 391 int size = vector.size(); 392 writeListBegin(size, null); 393 for (int i = 0; i < size; i++) 394 writeObject(vector.elementAt(i)); 395 396 writeListEnd(); 397 } 398 else if (object instanceof Hashtable ) { 399 Hashtable hashtable = (Hashtable ) object; 400 401 writeMapBegin(null); 402 Enumeration e = hashtable.keys(); 403 while (e.hasMoreElements()) { 404 Object key = e.nextElement(); 405 Object value = hashtable.get(key); 406 407 writeObject(key); 408 writeObject(value); 409 } 410 writeMapEnd(); 411 } 412 else 413 writeCustomObject(object); 414 } 415 416 421 public void writeCustomObject(Object object) 422 throws IOException 423 { 424 throw new IOException ("unexpected object: " + object); 425 } 426 427 442 public void writeListBegin(int length, String type) 443 throws IOException 444 { 445 print("<list><type>"); 446 if (type != null) 447 print(type); 448 print("</type><length>"); 449 printInt(length); 450 print("</length>"); 451 } 452 453 456 public void writeListEnd() 457 throws IOException 458 { 459 print("</list>"); 460 } 461 462 476 public void writeMapBegin(String type) 477 throws IOException 478 { 479 print("<map><type>"); 480 if (type != null) 481 print(type); 482 print("</type>"); 483 } 484 485 488 public void writeMapEnd() 489 throws IOException 490 { 491 print("</map>"); 492 } 493 494 505 public void writeRemote(String type, String url) 506 throws IOException 507 { 508 print("<remote><type>"); 509 if (type != null) 510 print(type); 511 print("</type><string>"); 512 print(url); 513 print("</string></remote>"); 514 } 515 516 521 public void printInt(int v) 522 throws IOException 523 { 524 print(String.valueOf(v)); 525 } 526 527 532 public void printLong(long v) 533 throws IOException 534 { 535 print(String.valueOf(v)); 536 } 537 538 543 public void printString(String v) 544 throws IOException 545 { 546 int len = v.length(); 547 548 for (int i = 0; i < len; i++) { 549 char ch = v.charAt(i); 550 551 switch (ch) { 552 case '<': 553 print("<"); 554 break; 555 556 case '&': 557 print("&"); 558 break; 559 560 case '\r': 561 print(" "); 562 break; 563 564 default: 565 if (ch < 0x80) 566 os.write(ch); 567 else if (ch < 0x800) { 568 os.write(0xc0 + ((ch >> 6) & 0x1f)); 569 os.write(0x80 + (ch & 0x3f)); 570 } 571 else { 572 os.write(0xe0 + ((ch >> 12) & 0xf)); 573 os.write(0x80 + ((ch >> 6) & 0x3f)); 574 os.write(0x80 + (ch & 0x3f)); 575 } 576 break; 577 } 578 } 579 } 580 581 586 public void printBytes(byte []data, int offset, int length) 587 throws IOException 588 { 589 int i; 590 591 for (; length >= 3; length -= 3) { 592 int chunk = (((data[offset] & 0xff) << 16) + 593 ((data[offset + 1] & 0xff) << 8) + 594 (data[offset + 2] & 0xff)); 595 596 os.write(base64encode(chunk >> 18)); 597 os.write(base64encode(chunk >> 12)); 598 os.write(base64encode(chunk >> 6)); 599 os.write(base64encode(chunk)); 600 601 offset += 3; 602 } 603 604 if (length == 2) { 605 int chunk = ((data[offset] & 0xff) << 8) + (data[offset + 1] & 0xff); 606 607 os.write(base64encode(chunk >> 12)); 608 os.write(base64encode(chunk >> 6)); 609 os.write(base64encode(chunk)); 610 os.write('='); 611 } else if (length == 1) { 612 int chunk = data[offset] & 0xff; 613 os.write(base64encode(chunk >> 6)); 614 os.write(base64encode(chunk)); 615 os.write('='); 616 os.write('='); 617 } 618 } 619 620 623 public static char base64encode(int d) 624 { 625 d &= 0x3f; 626 if (d < 26) 627 return (char) (d + 'A'); 628 else if (d < 52) 629 return (char) (d + 'a' - 26); 630 else if (d < 62) 631 return (char) (d + '0' - 52); 632 else if (d == 62) 633 return '+'; 634 else 635 return '/'; 636 } 637 638 643 public void printDate(Calendar calendar) 644 throws IOException 645 { 646 int year = calendar.get(Calendar.YEAR); 647 648 os.write((char) ('0' + (year / 1000 % 10))); 649 os.write((char) ('0' + (year / 100 % 10))); 650 os.write((char) ('0' + (year / 10 % 10))); 651 os.write((char) ('0' + (year % 10))); 652 653 int month = calendar.get(Calendar.MONTH) + 1; 654 os.write((char) ('0' + (month / 10 % 10))); 655 os.write((char) ('0' + (month % 10))); 656 657 int day = calendar.get(Calendar.DAY_OF_MONTH); 658 os.write((char) ('0' + (day / 10 % 10))); 659 os.write((char) ('0' + (day % 10))); 660 661 os.write('T'); 662 663 int hour = calendar.get(Calendar.HOUR_OF_DAY); 664 os.write((char) ('0' + (hour / 10 % 10))); 665 os.write((char) ('0' + (hour % 10))); 666 667 int minute = calendar.get(Calendar.MINUTE); 668 os.write((char) ('0' + (minute / 10 % 10))); 669 os.write((char) ('0' + (minute % 10))); 670 671 int second = calendar.get(Calendar.SECOND); 672 os.write((char) ('0' + (second / 10 % 10))); 673 os.write((char) ('0' + (second % 10))); 674 675 os.write('Z'); 676 } 677 678 684 public void print(String s) 685 throws IOException 686 { 687 int len = s.length(); 688 for (int i = 0; i < len; i++) { 689 int ch = s.charAt(i); 690 691 os.write(ch); 692 } 693 } 694 } 695 | Popular Tags |