1 48 49 package com.caucho.hessian.micro; 50 51 import java.io.IOException ; 52 import java.io.OutputStream ; 53 import java.util.Date ; 54 import java.util.Enumeration ; 55 import java.util.Hashtable ; 56 import java.util.Vector ; 57 58 79 public class MicroHessianOutput { 80 protected OutputStream os; 81 82 88 public MicroHessianOutput(OutputStream os) 89 { 90 init(os); 91 } 92 93 96 public MicroHessianOutput() 97 { 98 } 99 100 public void init(OutputStream os) 101 { 102 this.os = os; 103 } 104 105 115 public void startCall(String method) 116 throws IOException 117 { 118 os.write('c'); 119 os.write(0); 120 os.write(1); 121 122 os.write('m'); 123 int len = method.length(); 124 os.write(len >> 8); 125 os.write(len); 126 printString(method, 0, len); 127 } 128 129 136 public void completeCall() 137 throws IOException 138 { 139 os.write('z'); 140 } 141 142 153 public void writeBoolean(boolean value) 154 throws IOException 155 { 156 if (value) 157 os.write('T'); 158 else 159 os.write('F'); 160 } 161 162 172 public void writeInt(int value) 173 throws IOException 174 { 175 os.write('I'); 176 os.write(value >> 24); 177 os.write(value >> 16); 178 os.write(value >> 8); 179 os.write(value); 180 } 181 182 192 public void writeLong(long value) 193 throws IOException 194 { 195 os.write('L'); 196 os.write((byte) (value >> 56)); 197 os.write((byte) (value >> 48)); 198 os.write((byte) (value >> 40)); 199 os.write((byte) (value >> 32)); 200 os.write((byte) (value >> 24)); 201 os.write((byte) (value >> 16)); 202 os.write((byte) (value >> 8)); 203 os.write((byte) (value)); 204 } 205 206 215 public void writeUTCDate(long time) 216 throws IOException 217 { 218 os.write('d'); 219 os.write((byte) (time >> 56)); 220 os.write((byte) (time >> 48)); 221 os.write((byte) (time >> 40)); 222 os.write((byte) (time >> 32)); 223 os.write((byte) (time >> 24)); 224 os.write((byte) (time >> 16)); 225 os.write((byte) (time >> 8)); 226 os.write((byte) (time)); 227 } 228 229 239 public void writeNull() 240 throws IOException 241 { 242 os.write('N'); 243 } 244 245 261 public void writeString(String value) 262 throws IOException 263 { 264 if (value == null) { 265 os.write('N'); 266 } 267 else { 268 int len = value.length(); 269 270 os.write('S'); 271 os.write(len >> 8); 272 os.write(len); 273 274 printString(value); 275 } 276 } 277 278 294 public void writeBytes(byte []buffer) 295 throws IOException 296 { 297 if (buffer == null) 298 os.write('N'); 299 else 300 writeBytes(buffer, 0, buffer.length); 301 } 302 318 public void writeBytes(byte []buffer, int offset, int length) 319 throws IOException 320 { 321 if (buffer == null) { 322 os.write('N'); 323 } 324 else { 325 os.write('B'); 326 os.write(length << 8); 327 os.write(length); 328 os.write(buffer, offset, length); 329 } 330 } 331 332 341 public void writeRef(int value) 342 throws IOException 343 { 344 os.write('R'); 345 os.write(value << 24); 346 os.write(value << 16); 347 os.write(value << 8); 348 os.write(value); 349 } 350 351 354 public void writeObject(Object object) 355 throws IOException 356 { 357 if (object == null) 358 writeNull(); 359 else if (object instanceof String ) 360 writeString((String ) object); 361 else if (object instanceof Boolean ) 362 writeBoolean(((Boolean ) object).booleanValue()); 363 else if (object instanceof Integer ) 364 writeInt(((Number ) object).intValue()); 365 else if (object instanceof Long ) 366 writeLong(((Number ) object).longValue()); 367 else if (object instanceof Date ) 368 writeUTCDate(((Date ) object).getTime()); 369 else if (object instanceof byte[]) { 370 byte []data = (byte []) object; 371 writeBytes(data, 0, data.length); 372 } 373 else if (object instanceof Vector ) { 374 Vector vector = (Vector ) object; 375 376 int size = vector.size(); 377 writeListBegin(size, null); 378 for (int i = 0; i < size; i++) 379 writeObject(vector.get(i)); 380 381 writeListEnd(); 382 } 383 else if (object instanceof Hashtable ) { 384 Hashtable hashtable = (Hashtable ) object; 385 386 writeMapBegin(null); 387 Enumeration e = hashtable.keys(); 388 while (e.hasMoreElements()) { 389 Object key = e.nextElement(); 390 Object value = hashtable.get(key); 391 392 writeObject(key); 393 writeObject(value); 394 } 395 writeMapEnd(); 396 } 397 else 398 writeCustomObject(object); 399 } 400 401 406 public void writeCustomObject(Object object) 407 throws IOException 408 { 409 throw new IOException ("unexpected object: " + object); 410 } 411 412 427 public void writeListBegin(int length, String type) 428 throws IOException 429 { 430 os.write('V'); 431 os.write('t'); 432 printLenString(type); 433 434 os.write('l'); 435 os.write(length >> 24); 436 os.write(length >> 16); 437 os.write(length >> 8); 438 os.write(length); 439 } 440 441 444 public void writeListEnd() 445 throws IOException 446 { 447 os.write('z'); 448 } 449 450 459 public void writeMapBegin(String type) 460 throws IOException 461 { 462 os.write('M'); 463 os.write('t'); 464 printLenString(type); 465 } 466 467 470 public void writeMapEnd() 471 throws IOException 472 { 473 os.write('z'); 474 } 475 476 484 public void writeRemote(String type, String url) 485 throws IOException 486 { 487 os.write('r'); 488 os.write('t'); 489 printLenString(type); 490 os.write('S'); 491 printLenString(url); 492 } 493 494 499 public void printLenString(String v) 500 throws IOException 501 { 502 if (v == null) { 503 os.write(0); 504 os.write(0); 505 } 506 else { 507 int len = v.length(); 508 os.write(len >> 8); 509 os.write(len); 510 511 printString(v, 0, len); 512 } 513 } 514 515 520 public void printString(String v) 521 throws IOException 522 { 523 printString(v, 0, v.length()); 524 } 525 526 531 public void printString(String v, int offset, int length) 532 throws IOException 533 { 534 for (int i = 0; i < length; i++) { 535 char ch = v.charAt(i + offset); 536 537 if (ch < 0x80) 538 os.write(ch); 539 else if (ch < 0x800) { 540 os.write(0xc0 + ((ch >> 6) & 0x1f)); 541 os.write(0x80 + (ch & 0x3f)); 542 } 543 else { 544 os.write(0xe0 + ((ch >> 12) & 0xf)); 545 os.write(0x80 + ((ch >> 6) & 0x3f)); 546 os.write(0x80 + (ch & 0x3f)); 547 } 548 } 549 } 550 } 551 | Popular Tags |