1 3 package org.jgroups.util; 4 5 6 import org.jgroups.conf.ClassConfigurator; 7 import org.jgroups.ChannelException; 8 9 import java.io.Externalizable ; 10 import java.io.IOException ; 11 import java.io.ObjectInput ; 12 import java.io.ObjectOutput ; 13 14 15 16 17 32 public class Marshaller { 33 36 private static final ClassConfigurator mConfigurator; 37 38 static { 39 try { 40 mConfigurator=ClassConfigurator.getInstance(true); 41 } 42 catch(ChannelException e) { 43 throw new ExceptionInInitializerError (e.toString()); 44 } 45 } 46 47 48 public Marshaller() { 49 } 50 51 63 public static Externalizable read(ObjectInput in) throws IOException { 64 try { 65 boolean is_null=in.readBoolean(); 66 if(is_null) 67 return null; 68 69 boolean usemagic=in.readBoolean(); 71 Class extclass=null; 73 if(usemagic) { 74 int magic=in.readInt(); 76 extclass=mConfigurator.get(magic); 78 } 79 else { 80 String magic=in.readUTF(); 82 extclass=mConfigurator.get(magic); 84 } Externalizable newinstance=(Externalizable )extclass.newInstance(); 87 newinstance.readExternal(in); 89 return newinstance; 91 } 92 catch(Throwable x) { 93 if(x instanceof IOException ) 94 throw (IOException )x; 95 else 96 throw new IOException (x.toString()); 97 } 98 } 99 100 107 public static void write(Externalizable inst, ObjectOutput out) throws IOException { 108 boolean is_null=(inst == null); 109 try { 110 out.writeBoolean(is_null); 112 if(is_null) 113 return; 114 115 int magic=mConfigurator.getMagicNumber(inst.getClass()); 117 if(magic != -1) { 119 out.writeBoolean(true); 121 out.writeInt(magic); 123 } 124 else { 125 out.writeBoolean(false); 127 out.writeUTF(inst.getClass().getName()); 129 } inst.writeExternal(out); 132 } 133 catch(Exception x) { 134 if(x instanceof IOException ) 135 throw (IOException )x; 136 else 137 throw new java.io.IOException (x.toString()); 138 } 139 } 140 141 142 } 143 | Popular Tags |