1 9 package org.ozoneDB.DxLib; 10 11 import java.io.*; 12 import java.util.*; 13 14 23 public class DxObjectOutputStream extends DataOutputStream implements ObjectOutput { 24 protected Hashtable table; 25 protected Hashtable classes; 26 27 28 public DxObjectOutputStream( OutputStream out ) { 29 super( new BufferedOutputStream( out, 4096 ) ); 30 table = new Hashtable(); 31 classes = new Hashtable(); 32 } 33 34 35 public DxObjectOutputStream( OutputStream out, int bufferSize ) { 36 super( new BufferedOutputStream( out, bufferSize ) ); 37 table = new Hashtable(); 38 classes = new Hashtable(); 39 } 40 41 42 public void reset() throws IOException { 43 table.clear(); 44 classes.clear(); 45 flush(); 46 } 48 49 50 protected void writeClassName( Object obj ) throws IOException { 51 String cName = obj.getClass().getName(); 52 Integer index = (Integer )classes.get( cName ); 53 if (index == null) { 54 writeUTF( cName ); 56 classes.put( cName, new Integer ( classes.size() ) ); 57 } else { 58 writeUTF( "+" ); 60 writeShort( index.intValue() ); 61 } 62 } 63 64 65 public void writeObject( Object obj ) throws IOException { 66 try { 67 if (obj == null) { 69 writeUTF( "_" ); 70 return; 72 } 73 74 if (!(obj instanceof Externalizable)) { 75 throw new IOException( "Class " + obj.getClass().getName() + " doesn't implement Externalizable" ); 76 } 77 78 Integer hc = new Integer ( System.identityHashCode( obj ) ); 79 if (!table.containsKey( hc )) { 80 table.put( hc, obj ); 82 writeClassName( obj ); 84 writeInt( hc.intValue() ); 87 ((Externalizable)obj).writeExternal( this ); 89 } else { 90 writeUTF( ">" ); 92 writeInt( hc.intValue() ); 94 } 96 } catch (IOException e) { 97 throw e; 98 } catch (Exception e) { 99 throw new IOException( e.toString() ); 100 } 101 } 102 } 103 | Popular Tags |