From e12ff0e9f3f92259609d44c90910d7cbff7019c5 Mon Sep 17 00:00:00 2001 From: Jamie Doornbos Date: Sat, 11 Oct 2008 06:06:06 +0000 Subject: [PATCH] Optional type checking for readObject git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5438 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- src/as/com/threerings/io/ObjectInputStream.as | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/as/com/threerings/io/ObjectInputStream.as b/src/as/com/threerings/io/ObjectInputStream.as index 1c75fdd26..ec363ebd3 100644 --- a/src/as/com/threerings/io/ObjectInputStream.as +++ b/src/as/com/threerings/io/ObjectInputStream.as @@ -30,6 +30,7 @@ import flash.utils.IDataInput; import com.threerings.util.ClassUtil; import com.threerings.util.Log; import com.threerings.util.Long; +import com.threerings.util.safeCast; public class ObjectInputStream { @@ -54,7 +55,14 @@ public class ObjectInputStream _source = source; } - public function readObject () :Object + /** + * Attempts to read the next object from the stream. If a type is passed and the read object's + * type doesn't match, a warning is printed and null returned. The method does not throw, + * presumably to allow the client to hobble along until second order symptoms are observed. + * @param type optional type to check the read object against + * @return null if the object could not be read or is of the wrong type + */ + public function readObject (type :Class = null) :Object //throws IOError { var DEBUG_ID :String = "[" + (++_debugObjectCounter) + "] "; @@ -107,6 +115,10 @@ public class ObjectInputStream //log.debug("Reading object..."); readBareObjectImpl(target, cmap.streamer); if (DEBUG) log.debug(DEBUG_ID + "Read object: " + target); + if (type != null && !(target is type)) { + log.warning("Type mismatch", "expected", type, "read", ClassUtil.getClass(target)); + return null; + } return target; } catch (me :MemoryError) {