1 48 49 package com.caucho.hessian.micro; 50 51 import java.io.ByteArrayOutputStream ; 52 import java.io.IOException ; 53 import java.io.InputStream ; 54 import java.util.Date ; 55 56 77 public class MicroHessianInput { 78 protected InputStream is; 79 85 public MicroHessianInput(InputStream is) 86 { 87 init(is); 88 } 89 90 93 public MicroHessianInput() 94 { 95 } 96 97 100 public void init(InputStream is) 101 { 102 this.is = is; 103 } 104 105 114 public void startReply() 115 throws IOException 116 { 117 int tag = is.read(); 118 119 if (tag != 'r') 120 protocolException("expected hessian reply"); 121 122 int major = is.read(); 123 int minor = is.read(); 124 } 125 126 135 public void completeReply() 136 throws IOException 137 { 138 int tag = is.read(); 139 140 if (tag != 'z') 141 protocolException("expected end of reply"); 142 } 143 144 152 public boolean readBoolean() 153 throws IOException 154 { 155 int tag = is.read(); 156 157 switch (tag) { 158 case 'T': return true; 159 case 'F': return false; 160 default: 161 throw expect("boolean", tag); 162 } 163 } 164 165 172 public int readInt() 173 throws IOException 174 { 175 int tag = is.read(); 176 177 if (tag != 'I') 178 throw expect("integer", tag); 179 180 int b32 = is.read(); 181 int b24 = is.read(); 182 int b16 = is.read(); 183 int b8 = is.read(); 184 185 return (b32 << 24) + (b24 << 16) + (b16 << 8) + b8; 186 } 187 188 195 public long readLong() 196 throws IOException 197 { 198 int tag = is.read(); 199 200 if (tag != 'L') 201 throw protocolException("expected long"); 202 203 long b64 = is.read(); 204 long b56 = is.read(); 205 long b48 = is.read(); 206 long b40 = is.read(); 207 long b32 = is.read(); 208 long b24 = is.read(); 209 long b16 = is.read(); 210 long b8 = is.read(); 211 212 return ((b64 << 56) + 213 (b56 << 48) + 214 (b48 << 40) + 215 (b40 << 32) + 216 (b32 << 24) + 217 (b24 << 16) + 218 (b16 << 8) + 219 b8); 220 } 221 222 229 public long readUTCDate() 230 throws IOException 231 { 232 int tag = is.read(); 233 234 if (tag != 'd') 235 throw protocolException("expected date"); 236 237 long b64 = is.read(); 238 long b56 = is.read(); 239 long b48 = is.read(); 240 long b40 = is.read(); 241 long b32 = is.read(); 242 long b24 = is.read(); 243 long b16 = is.read(); 244 long b8 = is.read(); 245 246 return ((b64 << 56) + 247 (b56 << 48) + 248 (b48 << 40) + 249 (b40 << 32) + 250 (b32 << 24) + 251 (b24 << 16) + 252 (b16 << 8) + 253 b8); 254 } 255 256 263 public String readString() 264 throws IOException 265 { 266 int tag = is.read(); 267 268 if (tag == 'N') 269 return null; 270 271 if (tag != 'S') 272 throw expect("string", tag); 273 274 int b16 = is.read(); 275 int b8 = is.read(); 276 277 int len = (b16 << 8) + b8; 278 279 return readStringImpl(len); 280 } 281 282 289 public byte []readBytes() 290 throws IOException 291 { 292 int tag = is.read(); 293 294 if (tag == 'N') 295 return null; 296 297 if (tag != 'B') 298 throw expect("bytes", tag); 299 300 int b16 = is.read(); 301 int b8 = is.read(); 302 303 int len = (b16 << 8) + b8; 304 305 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 306 307 for (int i = 0; i < len; i++) 308 bos.write(is.read()); 309 310 return bos.toByteArray(); 311 } 312 313 316 public Object readObject(Class expectedClass) 317 throws IOException 318 { 319 int tag = is.read(); 320 321 switch (tag) { 322 case 'N': 323 return null; 324 325 case 'T': 326 return new Boolean (true); 327 328 case 'F': 329 return new Boolean (false); 330 331 case 'I': { 332 int b32 = is.read(); 333 int b24 = is.read(); 334 int b16 = is.read(); 335 int b8 = is.read(); 336 337 return new Integer ((b32 << 24) + (b24 << 16) + (b16 << 8) + b8); 338 } 339 340 case 'L': { 341 long b64 = is.read(); 342 long b56 = is.read(); 343 long b48 = is.read(); 344 long b40 = is.read(); 345 long b32 = is.read(); 346 long b24 = is.read(); 347 long b16 = is.read(); 348 long b8 = is.read(); 349 350 return new Long ((b64 << 56) + 351 (b56 << 48) + 352 (b48 << 40) + 353 (b40 << 32) + 354 (b32 << 24) + 355 (b24 << 16) + 356 (b16 << 8) + 357 b8); 358 } 359 360 case 'd': { 361 long b64 = is.read(); 362 long b56 = is.read(); 363 long b48 = is.read(); 364 long b40 = is.read(); 365 long b32 = is.read(); 366 long b24 = is.read(); 367 long b16 = is.read(); 368 long b8 = is.read(); 369 370 return new Date ((b64 << 56) + 371 (b56 << 48) + 372 (b48 << 40) + 373 (b40 << 32) + 374 (b32 << 24) + 375 (b24 << 16) + 376 (b16 << 8) + 377 b8); 378 } 379 380 case 'S': 381 case 'X': { 382 int b16 = is.read(); 383 int b8 = is.read(); 384 385 int len = (b16 << 8) + b8; 386 387 return readStringImpl(len); 388 } 389 390 case 'B': { 391 if (tag != 'B') 392 throw expect("bytes", tag); 393 394 int b16 = is.read(); 395 int b8 = is.read(); 396 397 int len = (b16 << 8) + b8; 398 399 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 400 401 for (int i = 0; i < len; i++) 402 bos.write(is.read()); 403 404 return bos.toByteArray(); 405 } 406 default: 407 throw new IOException ("unknown code:" + (char) tag); 408 } 409 } 410 411 414 protected String readStringImpl(int length) 415 throws IOException 416 { 417 StringBuffer sb = new StringBuffer (); 418 419 for (int i = 0; i < length; i++) { 420 int ch = is.read(); 421 422 if (ch < 0x80) 423 sb.append((char) ch); 424 else if ((ch & 0xe0) == 0xc0) { 425 int ch1 = is.read(); 426 int v = ((ch & 0x1f) << 6) + (ch1 & 0x3f); 427 428 sb.append((char) v); 429 } 430 else if ((ch & 0xf0) == 0xe0) { 431 int ch1 = is.read(); 432 int ch2 = is.read(); 433 int v = ((ch & 0x0f) << 12) + ((ch1 & 0x3f) << 6) + (ch2 & 0x3f); 434 435 sb.append((char) v); 436 } 437 else 438 throw new IOException ("bad utf-8 encoding"); 439 } 440 441 return sb.toString(); 442 } 443 444 protected IOException expect(String expect, int ch) 445 { 446 if (ch < 0) 447 return protocolException("expected " + expect + " at end of file"); 448 else 449 return protocolException("expected " + expect + " at " + (char) ch); 450 } 451 452 protected IOException protocolException(String message) 453 { 454 return new IOException (message); 455 } 456 } 457 | Popular Tags |