1 9 package org.ozoneDB.DxLib; 10 11 import java.io.*; 12 import java.util.*; 13 14 24 public class DxObjectInputStream extends DataInputStream implements ObjectInput { 25 protected Hashtable table; 26 protected Hashtable classes; 27 28 29 public DxObjectInputStream( InputStream in ) { 30 super( in ); 31 table = new Hashtable(); 32 classes = new Hashtable(); 33 } 34 35 36 public DxObjectInputStream( InputStream in, int bufferSize ) { 37 super( new BufferedInputStream( in, bufferSize ) ); 38 table = new Hashtable(); 39 classes = new Hashtable(); 40 } 41 42 43 public void reset() { 44 table.clear(); 45 classes.clear(); 46 } 48 49 50 public String readClassName() throws IOException { 51 String cName = readUTF(); 52 53 if (cName.equals( ">" ) || cName.equals( "_" )) { 55 return cName; 56 } 57 58 if (cName.equals( "+" )) { 60 Integer index = new Integer ( readShort() ); 61 cName = (String )classes.get( index ); 62 } else { 63 classes.put( new Integer ( classes.size() ), cName ); 64 } 65 66 return cName; 67 } 68 69 70 public Object readObject() throws IOException { 71 try { 72 String cName = readClassName(); 74 Object newObj = null; 76 77 if (cName.equals( "_" )) { 78 return null; 81 } else if (cName.equals( ">" )) { 82 Integer hc = new Integer ( readInt() ); 84 newObj = table.get( hc ); 85 if (newObj == null) { 87 throw new IOException( "Object not found in referenz table !!" ); 88 } 89 } else { 90 92 newObj = Thread.currentThread().getContextClassLoader().loadClass( cName ).newInstance(); 96 97 Integer hc = new Integer ( readInt() ); 99 table.put( hc, newObj ); 102 if (newObj instanceof Externalizable) { 104 ((Externalizable)newObj).readExternal( this ); 105 } else { 106 throw new IOException( "Object doesn't implement Externalizable" ); 107 } 108 } 109 return newObj; 110 } catch (IOException e) { 111 throw e; 112 } catch (Exception e) { 113 throw new IOException( e.toString() ); 114 } 115 } 116 } 117 | Popular Tags |