Here is part of the source of java.util.Hashtable
which shows its
default serialization state (threshold
, and loadFactor
).
The keys and values themselves are stored in a data structure using
HashtableEntry
objects which is marked transient.
The writeObject
method first calls defaultWriteObject
method to write the default serialization state and then writes the
number of keys in the table and then traverses the data structure and
writes out the individual keys and values.
The readObject
method first calls the defaultReadObject
method to read in the default serialization state and then reads the
number of keys and then reads that many keys and values constructing its
internal data structure again.
package java.util; import java.io.*; /** Hashtable collision list. */ class HashtableEntry { int hash; Object key; Object value; HashtableEntry next; protected Object clone() { | HashtableEntry entry = new HashtableEntry(); | entry.hash = hash; | entry.key = key; | entry.value = value; | entry.next = (next != null) ? (HashtableEntry)next.clone() : null; | return entry; } } public class Hashtable extends Dictionary implements Cloneable, java.io.Serializable { /** The hash table data. */ private transient HashtableEntry table[]; /** The total number of entries in the hash table. */ private transient int count; /** Rehashes the table when count exceeds this threshold. */ private int threshold; /** The load factor for the hashtable. */ private float loadFactor; /** use serialVersionUID from JDK 1.0.2 for interoperability */ private static final long serialVersionUID = 1421746759512286392L; // ... /** WriteObject is called to save the state of the hashtable to a stream. Only the keys and values are serialized since the hash values may be different when the contents are restored. iterate over the contents and write out the keys and values. */ private synchronized void writeObject(java.io.ObjectOutputStream s) throws IOException { | // Write out the length, threshold, loadfactor | s.defaultWriteObject(); | | // Write out length, count of elements and then the key/value objects | s.writeInt(table.length); | s.writeInt(count); | for (int index = table.length-1; index >= 0; index--) { | | HashtableEntry entry = table[index]; | | | | while (entry != null) { | | | s.writeObject(entry.key); | | | s.writeObject(entry.value); | | | entry = entry.next; | | } | } } /** readObject is called to restore the state of the hashtable from a stream. Only the keys and values are serialized since the hash values may be different when the contents are restored. Read count elements and insert into the hashtable. */ private synchronized void readObject(java.io.ObjectInputStream s) throws IOException, ClassNotFoundException { | // Read in the length, threshold, and loadfactor | s.defaultReadObject(); | | // Read the original length of the array and number of elements | int origlength = s.readInt(); | int elements = s.readInt(); | | // Compute new size with a bit of room 5% to grow but | // No larger than the original size. Make the length | // odd if it's large enough, this helps distribute the entries. | // Guard against the length ending up zero, that's not valid. | int length = (int)(elements * loadFactor) + (elements / 20) + 3; | if (length > elements && (length & 1) == 0) | length--; | if (origlength > 0 && length > origlength) | length = origlength; | | table = new HashtableEntry[length]; | count = 0; | | // Read the number of elements and then all the key/value objects | for (; elements > 0; elements--) { | | Object key = s.readObject(); | | Object value = s.readObject(); | | put(key, value); | } } }
The Portfolio
interface with a getStocks
method
on it that returns a Dictionary
.
package customMarshal; import java.util.Dictionary; import java.rmi.Remote; import java.rmi.RemoteException; public interface Portfolio extends Remote { Dictionary getStocks() throws RemoteException; }
The PortfolioImpl
implements the Portfolio
interface.
The getStocks
method returns a hash table containing the various stocks
held by the portfolio.
package customMarshal; import java.util.Hashtable; import java.util.Dictionary; import java.rmi.RemoteException; import javax.rmi.PortableRemoteObject; public class PortfolioImpl extends PortableRemoteObject implements Portfolio { private Hashtable _table = new Hashtable(); public PortfolioImpl() throws RemoteException {} public Dictionary getStocks() throws RemoteException { | return _table; } void addStock(String symbol, Holding holding) { | _table.put(symbol, holding); } }
package customMarshal; import util.Util; import java.util.Date; import org.omg.CORBA.ORB; import org.omg.CORBA.Object; import javax.rmi.PortableRemoteObject; public class Server { public static void main(String[] args) { | try { | | | | // create the jBroker ORB | | ORB orb = ORB.init(args, null); | | | | // create a servant | | PortfolioImpl portfolio = new PortfolioImpl(); | | | | // add holdings to the portfolio | | portfolio.addStock("IBM", new Holding(100, 150.0, new Date())); | | portfolio.addStock("SUNW", new Holding(200, 100.0, new Date())); | | portfolio.addStock("ORCL", new Holding(100, 30.0, new Date())); | | portfolio.addStock("MSFT", new Holding(100, 155.0, new Date())); | | | | // create a stringified object reference | | String portfolioIOR = orb.object_to_string((Object) | | PortableRemoteObject.toStub(portfolio)); | | | | // write the stringified object reference | | Util.writeIOR(portfolioIOR, "ior", true); | | | | // wait for invocations | | System.out.println("waiting for invocations ..."); | | orb.run(); | | | } catch (Exception ex) { | | ex.printStackTrace(); | } } }
package customMarshal; import util.Util; import org.omg.CORBA.ORB; import javax.rmi.PortableRemoteObject; public class Client { public static void main(String[] args) { | try { | | | | // create the jBroker ORB | | ORB orb = ORB.init(args, null); | | | | // read the stringified object reference | | String portfolioIOR = Util.readIOR("ior"); | | | | // narrow the stringified object | | Portfolio portfolio = (Portfolio) orb.string_to_object( | | portfolioIOR); | | | | // print the stocks held by the portfolio | | System.out.println(portfolio.getStocks()); | | | } catch (Exception ex) { | | ex.printStackTrace(); | } } }
Copyright © 2003, 2004 Novell, Inc. All rights reserved. Copyright © 2001, 2002, 2003 SilverStream Software, LLC. All rights reserved.