1 48 49 package com.caucho.hessian.io; 50 51 import java.io.IOException ; 52 import java.util.ArrayList ; 53 import java.util.Date ; 54 55 58 public class BasicDeserializer extends AbstractDeserializer { 59 public static final int NULL = BasicSerializer.NULL; 60 public static final int BOOLEAN = BasicSerializer.BOOLEAN; 61 public static final int BYTE = BasicSerializer.BYTE; 62 public static final int SHORT = BasicSerializer.SHORT; 63 public static final int INTEGER = BasicSerializer.INTEGER; 64 public static final int LONG = BasicSerializer.LONG; 65 public static final int FLOAT = BasicSerializer.FLOAT; 66 public static final int DOUBLE = BasicSerializer.DOUBLE; 67 public static final int CHARACTER = BasicSerializer.CHARACTER; 68 public static final int STRING = BasicSerializer.STRING; 69 public static final int DATE = BasicSerializer.DATE; 70 public static final int NUMBER = BasicSerializer.NUMBER; 71 public static final int OBJECT = BasicSerializer.OBJECT; 72 73 public static final int BOOLEAN_ARRAY = BasicSerializer.BOOLEAN_ARRAY; 74 public static final int BYTE_ARRAY = BasicSerializer.BYTE_ARRAY; 75 public static final int SHORT_ARRAY = BasicSerializer.SHORT_ARRAY; 76 public static final int INTEGER_ARRAY = BasicSerializer.INTEGER_ARRAY; 77 public static final int LONG_ARRAY = BasicSerializer.LONG_ARRAY; 78 public static final int FLOAT_ARRAY = BasicSerializer.FLOAT_ARRAY; 79 public static final int DOUBLE_ARRAY = BasicSerializer.DOUBLE_ARRAY; 80 public static final int CHARACTER_ARRAY = BasicSerializer.CHARACTER_ARRAY; 81 public static final int STRING_ARRAY = BasicSerializer.STRING_ARRAY; 82 public static final int OBJECT_ARRAY = BasicSerializer.OBJECT_ARRAY; 83 84 private int _code; 85 86 public BasicDeserializer(int code) 87 { 88 _code = code; 89 } 90 91 public Class getType() 92 { 93 switch (_code) { 94 case NULL: 95 return void.class; 96 case BOOLEAN: 97 return Boolean .class; 98 case BYTE: 99 return Byte .class; 100 case SHORT: 101 return Short .class; 102 case INTEGER: 103 return Integer .class; 104 case LONG: 105 return Long .class; 106 case FLOAT: 107 return Float .class; 108 case DOUBLE: 109 return Double .class; 110 case CHARACTER: 111 return Character .class; 112 case STRING: 113 return String .class; 114 case DATE: 115 return Date .class; 116 case NUMBER: 117 return Number .class; 118 119 case BOOLEAN_ARRAY: 120 return boolean[].class; 121 case BYTE_ARRAY: 122 return byte[].class; 123 case SHORT_ARRAY: 124 return short[].class; 125 case INTEGER_ARRAY: 126 return int[].class; 127 case LONG_ARRAY: 128 return long[].class; 129 case FLOAT_ARRAY: 130 return float[].class; 131 case DOUBLE_ARRAY: 132 return double[].class; 133 case CHARACTER_ARRAY: 134 return char[].class; 135 case STRING_ARRAY: 136 return String [].class; 137 case OBJECT_ARRAY: 138 return Object [].class; 139 default: 140 throw new UnsupportedOperationException (); 141 } 142 } 143 144 public Object readObject(AbstractHessianInput in) 145 throws IOException 146 { 147 switch (_code) { 148 case NULL: 149 in.readObject(); 151 152 return null; 153 154 case BOOLEAN: 155 return new Boolean (in.readBoolean()); 156 157 case BYTE: 158 return new Byte ((byte) in.readInt()); 159 160 case SHORT: 161 return new Short ((short) in.readInt()); 162 163 case INTEGER: 164 return new Integer (in.readInt()); 165 166 case LONG: 167 return new Long (in.readLong()); 168 169 case FLOAT: 170 return new Float ((float) in.readDouble()); 171 172 case DOUBLE: 173 return new Double (in.readDouble()); 174 175 case STRING: 176 return in.readString(); 177 178 case OBJECT: 179 return in.readObject(); 180 181 case CHARACTER: 182 { 183 String s = in.readString(); 184 if (s == null || s.equals("")) 185 return null; 186 else 187 return new Character (s.charAt(0)); 188 } 189 190 case DATE: 191 return new Date (in.readUTCDate()); 192 193 case NUMBER: 194 return in.readObject(); 195 196 case BYTE_ARRAY: 197 return in.readBytes(); 198 199 case CHARACTER_ARRAY: 200 { 201 String s = in.readString(); 202 203 if (s == null) 204 return null; 205 else { 206 int len= s.length(); 207 char []chars = new char[len]; 208 s.getChars(0, len, chars, 0); 209 return chars; 210 } 211 } 212 213 case BOOLEAN_ARRAY: 214 case SHORT_ARRAY: 215 case INTEGER_ARRAY: 216 case LONG_ARRAY: 217 case FLOAT_ARRAY: 218 case DOUBLE_ARRAY: 219 case STRING_ARRAY: 220 { 221 int code = in.readListStart(); 222 223 switch (code) { 224 case 'N': 225 return null; 226 227 case 0x10: case 0x11: case 0x12: case 0x13: 228 case 0x14: case 0x15: case 0x16: case 0x17: 229 case 0x18: case 0x19: case 0x1a: case 0x1b: 230 case 0x1c: case 0x1d: case 0x1e: case 0x1f: 231 int length = code - 0x10; 232 in.readInt(); 233 234 return readLengthList(in, length); 235 236 default: 237 String type = in.readType(); 238 length = in.readLength(); 239 240 return readList(in, length); 241 } 242 } 243 244 default: 245 throw new UnsupportedOperationException (); 246 } 247 } 248 249 public Object readList(AbstractHessianInput in, int length) 250 throws IOException 251 { 252 switch (_code) { 253 case BOOLEAN_ARRAY: { 254 if (length >= 0) { 255 boolean []data = new boolean[length]; 256 257 in.addRef(data); 258 259 for (int i = 0; i < data.length; i++) 260 data[i] = in.readBoolean(); 261 262 in.readEnd(); 263 264 return data; 265 } 266 else { 267 ArrayList list = new ArrayList (); 268 269 while (! in.isEnd()) 270 list.add(new Boolean (in.readBoolean())); 271 272 in.readEnd(); 273 274 boolean []data = new boolean[list.size()]; 275 276 in.addRef(data); 277 278 for (int i = 0; i < data.length; i++) 279 data[i] = ((Boolean ) list.get(i)).booleanValue(); 280 281 return data; 282 } 283 } 284 285 case SHORT_ARRAY: { 286 if (length >= 0) { 287 short []data = new short[length]; 288 289 in.addRef(data); 290 291 for (int i = 0; i < data.length; i++) 292 data[i] = (short) in.readInt(); 293 294 in.readEnd(); 295 296 return data; 297 } 298 else { 299 ArrayList list = new ArrayList (); 300 301 while (! in.isEnd()) 302 list.add(new Short ((short) in.readInt())); 303 304 in.readEnd(); 305 306 short []data = new short[list.size()]; 307 for (int i = 0; i < data.length; i++) 308 data[i] = ((Short ) list.get(i)).shortValue(); 309 310 in.addRef(data); 311 312 return data; 313 } 314 } 315 316 case INTEGER_ARRAY: { 317 if (length >= 0) { 318 int []data = new int[length]; 319 320 in.addRef(data); 321 322 for (int i = 0; i < data.length; i++) 323 data[i] = in.readInt(); 324 325 in.readEnd(); 326 327 return data; 328 } 329 else { 330 ArrayList list = new ArrayList (); 331 332 while (! in.isEnd()) 333 list.add(new Integer (in.readInt())); 334 335 336 in.readEnd(); 337 338 int []data = new int[list.size()]; 339 for (int i = 0; i < data.length; i++) 340 data[i] = ((Integer ) list.get(i)).intValue(); 341 342 in.addRef(data); 343 344 return data; 345 } 346 } 347 348 case LONG_ARRAY: { 349 if (length >= 0) { 350 long []data = new long[length]; 351 352 in.addRef(data); 353 354 for (int i = 0; i < data.length; i++) 355 data[i] = in.readLong(); 356 357 in.readEnd(); 358 359 return data; 360 } 361 else { 362 ArrayList list = new ArrayList (); 363 364 while (! in.isEnd()) 365 list.add(new Long (in.readLong())); 366 367 in.readEnd(); 368 369 long []data = new long[list.size()]; 370 for (int i = 0; i < data.length; i++) 371 data[i] = ((Long ) list.get(i)).longValue(); 372 373 in.addRef(data); 374 375 return data; 376 } 377 } 378 379 case FLOAT_ARRAY: { 380 if (length >= 0) { 381 float []data = new float[length]; 382 in.addRef(data); 383 384 for (int i = 0; i < data.length; i++) 385 data[i] = (float) in.readDouble(); 386 387 in.readEnd(); 388 389 return data; 390 } 391 else { 392 ArrayList list = new ArrayList (); 393 394 while (! in.isEnd()) 395 list.add(new Float (in.readDouble())); 396 397 in.readEnd(); 398 399 float []data = new float[list.size()]; 400 for (int i = 0; i < data.length; i++) 401 data[i] = ((Float ) list.get(i)).floatValue(); 402 403 in.addRef(data); 404 405 return data; 406 } 407 } 408 409 case DOUBLE_ARRAY: { 410 if (length >= 0) { 411 double []data = new double[length]; 412 in.addRef(data); 413 414 for (int i = 0; i < data.length; i++) 415 data[i] = in.readDouble(); 416 417 in.readEnd(); 418 419 return data; 420 } 421 else { 422 ArrayList list = new ArrayList (); 423 424 while (! in.isEnd()) 425 list.add(new Double (in.readDouble())); 426 427 in.readEnd(); 428 429 double []data = new double[list.size()]; 430 in.addRef(data); 431 for (int i = 0; i < data.length; i++) 432 data[i] = ((Double ) list.get(i)).intValue(); 433 434 in.readEnd(); 435 436 return data; 437 } 438 } 439 440 case STRING_ARRAY: { 441 if (length >= 0) { 442 String []data = new String [length]; 443 in.addRef(data); 444 445 for (int i = 0; i < data.length; i++) 446 data[i] = in.readString(); 447 448 in.readEnd(); 449 450 return data; 451 } 452 else { 453 ArrayList list = new ArrayList (); 454 455 while (! in.isEnd()) 456 list.add(in.readString()); 457 458 in.readEnd(); 459 460 String []data = new String [list.size()]; 461 in.addRef(data); 462 for (int i = 0; i < data.length; i++) 463 data[i] = (String ) list.get(i); 464 465 in.readEnd(); 466 467 return data; 468 } 469 } 470 471 case OBJECT_ARRAY: { 472 if (length >= 0) { 473 Object []data = new Object [length]; 474 in.addRef(data); 475 476 for (int i = 0; i < data.length; i++) 477 data[i] = in.readObject(); 478 479 in.readEnd(); 480 481 return data; 482 } 483 else { 484 ArrayList list = new ArrayList (); 485 486 in.addRef(list); 488 while (! in.isEnd()) 489 list.add(in.readObject()); 490 491 in.readEnd(); 492 493 Object []data = new Object [list.size()]; 494 for (int i = 0; i < data.length; i++) 495 data[i] = (Object ) list.get(i); 496 497 return data; 498 } 499 } 500 501 default: 502 throw new UnsupportedOperationException (String.valueOf(this)); 503 } 504 } 505 506 public Object readLengthList(AbstractHessianInput in, int length) 507 throws IOException 508 { 509 switch (_code) { 510 case BOOLEAN_ARRAY: { 511 boolean []data = new boolean[length]; 512 513 in.addRef(data); 514 515 for (int i = 0; i < data.length; i++) 516 data[i] = in.readBoolean(); 517 518 return data; 519 } 520 521 case SHORT_ARRAY: { 522 short []data = new short[length]; 523 524 in.addRef(data); 525 526 for (int i = 0; i < data.length; i++) 527 data[i] = (short) in.readInt(); 528 529 return data; 530 } 531 532 case INTEGER_ARRAY: { 533 int []data = new int[length]; 534 535 in.addRef(data); 536 537 for (int i = 0; i < data.length; i++) 538 data[i] = in.readInt(); 539 540 return data; 541 } 542 543 case FLOAT_ARRAY: { 544 float []data = new float[length]; 545 in.addRef(data); 546 547 for (int i = 0; i < data.length; i++) 548 data[i] = (float) in.readDouble(); 549 550 return data; 551 } 552 553 case DOUBLE_ARRAY: { 554 double []data = new double[length]; 555 in.addRef(data); 556 557 for (int i = 0; i < data.length; i++) 558 data[i] = in.readDouble(); 559 560 return data; 561 } 562 563 case STRING_ARRAY: { 564 String []data = new String [length]; 565 in.addRef(data); 566 567 for (int i = 0; i < data.length; i++) 568 data[i] = in.readString(); 569 570 return data; 571 } 572 573 case OBJECT_ARRAY: { 574 Object []data = new Object [length]; 575 in.addRef(data); 576 577 for (int i = 0; i < data.length; i++) 578 data[i] = in.readObject(); 579 580 return data; 581 } 582 583 default: 584 throw new UnsupportedOperationException (String.valueOf(this)); 585 } 586 } 587 } 588 | Popular Tags |