Proposed change to readObject().

The purpose is to
- Ensure type checking for "fail-fast" detection of streaming errors.
- But simplify it so that callers don't have to cast themselves and
  potentially screw up by using "String(obj)" or "Array(obj)", or miss out
  on the fail-fastyness by using "obj as SomeType".
- But, at the same time, allow for the sveltest code possible.
    // first one is a bit shorter!
    _name = ins.readObject(Name);
    _name = Name(ins.readObject());

    // first one is a bit shorter!
    _state = ins.readObject(String);
    _state = ins.readObject() as String;


So, to sum up: the code is the shortest and doesn't require special-casing
if you are reading a String, Number, int, uint, Boolean, or Array.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5984 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Ray Greenwell
2009-11-06 01:27:03 +00:00
parent c16de5994e
commit ee27342a24
+10 -7
View File
@@ -62,13 +62,16 @@ public class ObjectInputStream
}
/**
* Attempts to read the next object from the stream. If a type is passed and the read object's
* type doesn't match, a <code>TypeError</code> is thrown.
* @param type optional type to check the read object against
* Reads the next Object (or null) from the stream and returns it as * so that you may assign
* to any variable without casting. It is strongly recommended that you pass a 'checkType'
* parameter so that the type of the Object is verified to be what you believe it to be,
* thus helping you detect streaming errors as quickly as possible.
*
* @param checkType optional type to check the read object against
* @return the Object read, or null.
* @throws TypeError if the object is read successfully but is the wrong type
* @return null if the object could not be read (or is actually null)
*/
public function readObject (type :Class = null) :Object
public function readObject (checkType :Class = null) :*
//throws IOError
{
var DEBUG_ID :String = "[" + (++_debugObjectCounter) + "] ";
@@ -121,9 +124,9 @@ 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)) {
if (checkType != null && !(target is checkType)) {
throw new TypeError(
"Cannot convert " + ClassUtil.getClass(target) + " to " + type);
"Cannot convert " + ClassUtil.getClass(target) + " to " + checkType);
}
return target;