Now that ByteEnum has been liberated from depot, lets

support streaming them as bytes.
It's better than writing a modified UTF String, but
it'd be even nicer if we could avoid the short to read the class name
(Enums are final), but I suppose we still have to know whether one is null
or not. Hm.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5920 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Ray Greenwell
2009-08-19 02:18:14 +00:00
parent cc5a87d18d
commit 4848fe4b29
3 changed files with 72 additions and 4 deletions
+4 -1
View File
@@ -24,6 +24,7 @@ package com.threerings.io {
import flash.utils.ByteArray;
import flash.utils.Dictionary;
import com.threerings.util.ByteEnum;
import com.threerings.util.ClassUtil;
import com.threerings.util.Enum;
import com.threerings.util.env.Environment;
@@ -31,6 +32,7 @@ import com.threerings.util.env.Environment;
import com.threerings.io.streamers.ArrayStreamer;
import com.threerings.io.streamers.ByteStreamer;
import com.threerings.io.streamers.ByteArrayStreamer;
import com.threerings.io.streamers.ByteEnumStreamer;
import com.threerings.io.streamers.EnumStreamer;
import com.threerings.io.streamers.FloatStreamer;
import com.threerings.io.streamers.IntegerStreamer;
@@ -77,7 +79,8 @@ public class Streamer
var clazz :Class = ClassUtil.getClassByName(Translations.getFromServer(jname));
if (Environment.isAssignableAs(Enum, clazz)) {
streamer = new EnumStreamer(clazz, jname);
streamer = Environment.isAssignableAs(ByteEnum, clazz) ?
new ByteEnumStreamer(clazz, jname) : new EnumStreamer(clazz, jname);
} else if (Environment.isAssignableAs(Streamable, clazz)) {
streamer = new Streamer(clazz, jname);
@@ -0,0 +1,52 @@
//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2009 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.io.streamers {
import com.threerings.util.ByteEnum;
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.io.Streamer;
public class ByteEnumStreamer extends Streamer
{
public function ByteEnumStreamer (enumClass :Class, jname :String = null)
{
super(enumClass, jname);
}
override public function createObject (ins :ObjectInputStream) :Object
{
return ByteEnum.fromByte(_target, ins.readByte());
}
override public function writeObject (obj :Object, out :ObjectOutputStream) :void
{
out.writeByte(ByteEnum(obj).toByte());
}
override public function readObject (obj :Object, ins :ObjectInputStream) :void
{
// unneeded, done in createObject
}
}
}