1 9 10 11 22 23 package JSX; 25 import java.io.*; 28 public class ObjIn extends ObjectInputStream { 29 static final boolean DEBUG = true; 31 32 static final boolean TRACE = false; 33 35 static final boolean NONSTREAM = false; 36 38 public static ClassLoader cl = null; 39 40 41 45 49 final private ValidationList vlist = new ValidationList(); 50 private XMLDeserialize d; 51 private Config config; public ObjIn(Reader in) throws IOException { 53 super(); d = new XMLDeserialize(in); 55 d.setArg(new Object [] {this}); 56 if (TRACE) System.err.println("ObjIn(inReader)"); 58 } 59 60 public ObjIn(Reader in, Config cfg) throws IOException { 61 super(); config = cfg; if (cfg.importFile!=null) { 64 try { 65 XSLTimportI xsltImport = (XSLTimportI) Class.forName("JSX.XSLTimport").newInstance(); 66 in = xsltImport.preprocess(in, cfg); } catch (ClassNotFoundException noXSLTimport) { 69 throw new IOException(noXSLTimport.toString()); } catch (InstantiationException cannotConstruct) { 71 throw new IOException(cannotConstruct.toString()); 72 } catch (IllegalAccessException cannotAccess) { 73 throw new IOException(cannotAccess.toString()); 74 } 75 } 76 d = new XMLDeserialize(in, cfg); 77 d.setArg(new Object [] {this}); if (TRACE) System.err.println("ObjIn(inReader)"); 80 } 81 82 87 public ObjIn(InputStream in) throws IOException { this((Reader)new InputStreamReader(in)); 89 if (TRACE) System.err.println("ObjIn(in)"); 90 } 91 public ObjIn(InputStream in, Config cfg) throws IOException { 92 this((Reader)new InputStreamReader(in), cfg); 93 if (TRACE) System.err.println("ObjIn(in)"); 94 } 95 96 102 public ObjIn() throws IOException, SecurityException { 103 this(System.in); if (TRACE) System.err.println("ObjIn()"); 105 } 106 107 public ObjIn(Config cfg) throws IOException, SecurityException { 108 this(System.in, cfg); if (TRACE) System.err.println("ObjIn()"); 110 } 111 112 113 117 126 public void reset() { d.reset(); } public void close() { 128 d.close(); } 131 132 138 140 155 156 187 188 protected Object readObjectOverride() throws OptionalDataException, 189 ClassNotFoundException , 190 IOException { 191 try { 192 if (!d.superVersion) { if (d.invocationCount>0) defaultReadObject(); } 196 if (TRACE) System.err.println("readObjectOverride()"); 197 Object o = null; 198 o = d.deserialize(); if (TRACE) System.err.println("Object returned:" + o); 200 201 if (d.invocationCount==0) vlist.doCallbacks(); 226 return o; 227 } finally { 228 vlist.clear(); } 230 } 231 232 237 public void defaultReadObject() throws IOException, ClassNotFoundException { 238 if (TRACE) System.err.println("defaultReadObject()"); 239 244 d.defaultReadObject(); } 246 247 public void readFully(byte[] dst) throws 248 IOException { 250 252 byte[] src = null; 253 try { 254 src = (byte[]) readObject(); 255 } catch (ClassNotFoundException e) { 256 throw new IOException("Reading for a byte[]; but: "+e); } 260 System.arraycopy(src, 0, dst, 0, src.length); } 266 267 268 276 void internalDefaultReadObject() throws IOException { if (d.superVersion) return; try { 280 d.defaultReadObject(); } catch (ClassNotFoundException e) { 282 throw new ClassNotFoundInDefaultException(e.getMessage()); 283 } 284 } 285 class ClassNotFoundInDefaultException extends IOException { 286 ClassNotFoundInDefaultException(String msg) { super(msg); } 287 } 288 289 public int readInt() throws IOException { 290 internalDefaultReadObject(); return Integer.parseInt(d.getPrimString()); 292 } 293 public float readFloat() throws IOException { 294 internalDefaultReadObject(); return ParseUtilities.parseFloat(d.getPrimString()); 297 } 298 public boolean readBoolean() throws IOException { 301 internalDefaultReadObject(); return ParseUtilities.parseBoolean(d.getPrimString()); 303 } 304 public byte readByte() throws IOException { 305 internalDefaultReadObject(); return Byte.parseByte(d.getPrimString()); 307 } 308 public char readChar() throws IOException { 309 internalDefaultReadObject(); return (char) Integer.parseInt(d.getPrimString()); 312 } 315 public double readDouble() throws IOException { 316 internalDefaultReadObject(); return ParseUtilities.parseDouble(d.getPrimString()); 319 } 320 public short readShort() throws IOException { 323 internalDefaultReadObject(); return Short.parseShort(d.getPrimString()); 325 } 326 public long readLong() throws IOException { 328 internalDefaultReadObject(); return Long.parseLong(d.getPrimString()); 330 } 331 332 338 public String readUTF() throws IOException { 339 internalDefaultReadObject(); return d.getPrimString(); 342 350 } 351 352 358 359 365 392 393 394 public void registerValidation(ObjectInputValidation obj, int prio) throws InvalidObjectException, NotActiveException { 395 if (d.invocationCount==0) throw new NotActiveException("JSX.ObjIn: readObject not Active"); 398 vlist.register(obj, prio); 399 400 419 } 420 421 422 protected Class resolveClass(ObjectStreamClass v) throws IOException, ClassNotFoundException { 423 String classname = v.getName(); 426 if (config!=null && config.refactor!=null) 427 classname = config.refactor.mapClassname(classname); 428 Class clazz = null; 429 try { 430 clazz=Thread.currentThread().getContextClassLoader().loadClass(classname); 433 } catch (ClassNotFoundException e) { 434 throw new ClassNotFoundException ("forName(\""+v.getName()+"\") didn't find the class. Please report this problem. Wrapped: "+e.getMessage()); 435 } 438 return clazz; 439 } 440 441 protected Object resolveObject(Object obj) { 442 throw new RuntimeException ("resolveObject() invoked - not implemented"); 443 } 445 public int skipBytes(int len) { 446 throw new RuntimeException ("skipBytes() invoked - not implemented"); 447 } 449 450 451 public ObjectInputStream.GetField readFields() throws 452 IOException, 453 ClassNotFoundException { 454 defaultReadObject(); if (TRACE) System.err.println("in.readFields(); invoked - about to return"); 456 return (GetField)d.getCurrentGetField(); 457 } 460 461 462 463 467 public static void main(String [] args) throws Exception { 468 469 InputStream fin = System.in; 470 if (args.length!=0) { fin = new FileInputStream(args[0]); 472 } 473 483 484 Config c = new Config().setFormatted(true).aliasID(false); 487 489 ObjIn in = new ObjIn(fin, c); 490 ObjOut out = new ObjOut(c); 495 Object o; 496 while (true) { 498 try { 499 o = in.readObject(); 500 if (DEBUG) { System.err.println(); 502 System.err.println("Finished reading--------"); 503 System.err.println(); 504 } 505 out.writeObject(o); 506 out.flush(); 507 511 if (DEBUG) 512 System.err.println("Finished writing--------"); 513 } catch (EOFException e) { break; 515 } catch (Exception e) { 516 System.err.println(e); 517 e.printStackTrace(); 518 continue; 519 } 520 } 521 out.close(); 522 in.close(); 523 System.exit(0); } 525 } 526 | Popular Tags |