From 892bdce01db39327af090d75d600211ebaafee6b Mon Sep 17 00:00:00 2001 From: Ray Greenwell Date: Wed, 15 Feb 2006 04:05:40 +0000 Subject: [PATCH] Old man Occam came a knockin'. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3857 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../com/threerings/io/ObjectInputStream.java | 32 ++++---- .../com/threerings/io/ObjectOutputStream.java | 81 +++++++++---------- 2 files changed, 52 insertions(+), 61 deletions(-) diff --git a/src/java/com/threerings/io/ObjectInputStream.java b/src/java/com/threerings/io/ObjectInputStream.java index 190a272a0..ccbd4c747 100644 --- a/src/java/com/threerings/io/ObjectInputStream.java +++ b/src/java/com/threerings/io/ObjectInputStream.java @@ -150,24 +150,13 @@ public class ObjectInputStream extends DataInputStream } // create an instance of the appropriate object - _streamer = cmap.streamer; - Object target = _streamer.createObject(this); - _current = target; - - // now read the instance data - _streamer.readObject(target, this, true); - - // and return the newly read object + Object target = cmap.streamer.createObject(this); + readBareObject(target, cmap.streamer); return target; } catch (OutOfMemoryError oome) { throw (IOException) new IOException("Malformed object data").initCause(oome); - - } finally { - // clear out our current object references - _current = null; - _streamer = null; } } @@ -181,12 +170,19 @@ public class ObjectInputStream extends DataInputStream public void readBareObject (Object object) throws IOException, ClassNotFoundException { - try { - // obtain the streamer for objects of this class - _streamer = Streamer.getStreamer(object.getClass()); - _current = object; + readBareObject(object, Streamer.getStreamer(object.getClass())); + } - // now read the instance data + /** + * Reads an object from the input stream that was previously written + * with {@link ObjectOutputStream#writeBareObject}. + */ + protected void readBareObject (Object object, Streamer streamer) + throws IOException, ClassNotFoundException + { + _current = object; + _streamer = streamer; + try { _streamer.readObject(object, this, true); } finally { diff --git a/src/java/com/threerings/io/ObjectOutputStream.java b/src/java/com/threerings/io/ObjectOutputStream.java index 90451a26f..c3639bdac 100644 --- a/src/java/com/threerings/io/ObjectOutputStream.java +++ b/src/java/com/threerings/io/ObjectOutputStream.java @@ -1,5 +1,5 @@ // -// $Id: ObjectOutputStream.java,v 1.3 2004/08/27 02:12:36 mdb Exp $ +// $Id$ // // Narya library - tools for developing networked games // Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved @@ -81,47 +81,37 @@ public class ObjectOutputStream extends DataOutputStream _classmap = new HashMap(); } - try { - // otherwise, look up the class mapping record - Class sclass = object.getClass(); - ClassMapping cmap = (ClassMapping)_classmap.get(sclass); - _current = object; + // otherwise, look up the class mapping record + Class sclass = object.getClass(); + ClassMapping cmap = (ClassMapping)_classmap.get(sclass); - // create a class mapping for this class if we've not got one - if (cmap == null) { - // create a streamer instance and assign a code to this class - Streamer streamer = Streamer.getStreamer(sclass); - // we specifically do not inline the getStreamer() call - // into the ClassMapping constructor because we want to be - // sure not to call _nextCode++ if getStreamer() throws an - // exception - cmap = new ClassMapping(_nextCode++, sclass, streamer); - _classmap.put(sclass, cmap); + // create a class mapping for this class if we've not got one + if (cmap == null) { + // create a streamer instance and assign a code to this class + Streamer streamer = Streamer.getStreamer(sclass); + // we specifically do not inline the getStreamer() call + // into the ClassMapping constructor because we want to be + // sure not to call _nextCode++ if getStreamer() throws an + // exception + cmap = new ClassMapping(_nextCode++, sclass, streamer); + _classmap.put(sclass, cmap); - // make sure we didn't blow past our maximum class count - if (_nextCode <= 0) { - throw new RuntimeException("Too many unique classes " + - "written to ObjectOutputStream"); - } - - // writing a negative class code indicates that the class - // name will follow - writeShort(-cmap.code); - writeUTF(sclass.getName()); - - } else { - writeShort(cmap.code); + // make sure we didn't blow past our maximum class count + if (_nextCode <= 0) { + throw new RuntimeException("Too many unique classes " + + "written to ObjectOutputStream"); } - // now write the instance data - _streamer = cmap.streamer; - _streamer.writeObject(object, this, true); + // writing a negative class code indicates that the class + // name will follow + writeShort(-cmap.code); + writeUTF(sclass.getName()); - } finally { - // clear out our current object references - _current = null; - _streamer = null; + } else { + writeShort(cmap.code); } + + writeBareObject(object, cmap.streamer); } /** @@ -137,16 +127,21 @@ public class ObjectOutputStream extends DataOutputStream public void writeBareObject (Object object) throws IOException { - try { - // get the streamer for objects of this type - _streamer = Streamer.getStreamer(object.getClass()); - _current = object; + writeBareObject(object, Streamer.getStreamer(object.getClass())); + } - // now write the instance data - _streamer.writeObject(object, this, true); + /** + * Write a {@link Streamable} instance without associated class metadata. + */ + protected void writeBareObject (Object obj, Streamer streamer) + throws IOException + { + _current = obj; + _streamer = streamer; + try { + _streamer.writeObject(obj, this, true); } finally { - // clear out our current object references _current = null; _streamer = null; }