Index: [thread] [date] [subject] [author]
  From: Geoff Berry <gcb@cool.cs.duke.edu>
  To  : 
  Date: 30 Apr 1999 02:14:58 -0400

Re: Object Serialization

* Philip K Warren writes:
| Can anyone offer any advice on Object Serialization?  We would
| prefer to send our maze as an object over the network instead of
| making a huge text-based message that would describe all of the
| connections, rooms in our maze and then build it from scratch on the
| client.  However, after making much of the code implement
| Serializable and testing it on our server, it kept saying that we
| were overrunning the stack and the server would die.
| 
| Has anyone been able to get this working in their programs?  Can
| anyone offer any advice on sending these types of objects over the
| network?

Object serialization does a depth-first traversal of the object graph
starting at the object you are writing out (objects are nodes, fields
are edges).  If your object graph is too deep too many recursive calls
will be made and you will exceed the alloted stack space.  There are a
couple of solutions too this problem:

1) Increase the stack size -- this is the easiest solution, but is not
   very robust

2) Eliminate edges in your object graph and write read/writeObject
   methods for your classes.

   For example, you could remove/make transient any fields in the Room
   class that point back to the Graph class and then write a
   writeObject method for the Graph class that does something like
   this:

	void writeObject (ObjectOutput oo)
	{	
	  oo.writeInt (rooms.count ());

	  for (Room r = rooms.first (); rooms.hasNext (); r = rooms.next ())
	    oo.writeObject (r);

	  oo.defaultWriteObject ();
	}
   
   and then you'd have to do a little extra work on the reading side:

	void readObject (ObjectInput oi)
	{
	  int roomcount = oi.readInt ();

	  for (int i = 0; i < roomcount; ++i)
	  {
	    Room r = oi.readObject ();
	    addRoom (r);
	  }

	  oi.defaultReadObject ();
	}

        void addRoom (Room r)
	{
	  r.setGraph (this);
	  rooms.add (r);
	}

3) Use different ObjectIn/OutputStream classes.  I have written
   subclasses of both of these classes that can handle much deeper
   object graphs (they work with Java 2) that I could make available
   on CS/Acpub if you'd like to give them a shot.  They use an
   incompatible (and slightly larger) data format. They are quite
   stable (I've used them for almost a year).

-- 
Ima Lurker Tu


Index: [thread] [date] [subject] [author]