From 4848fe4b293395143e442b178735c40f804e8223 Mon Sep 17 00:00:00 2001 From: Ray Greenwell Date: Wed, 19 Aug 2009 02:18:14 +0000 Subject: [PATCH] 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 --- src/as/com/threerings/io/Streamer.as | 5 +- .../io/streamers/ByteEnumStreamer.as | 52 +++++++++++++++++++ src/java/com/threerings/io/Streamer.java | 19 +++++-- 3 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 src/as/com/threerings/io/streamers/ByteEnumStreamer.as diff --git a/src/as/com/threerings/io/Streamer.as b/src/as/com/threerings/io/Streamer.as index bf60cbc2f..7a71f357c 100644 --- a/src/as/com/threerings/io/Streamer.as +++ b/src/as/com/threerings/io/Streamer.as @@ -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); diff --git a/src/as/com/threerings/io/streamers/ByteEnumStreamer.as b/src/as/com/threerings/io/streamers/ByteEnumStreamer.as new file mode 100644 index 000000000..c7b3e1818 --- /dev/null +++ b/src/as/com/threerings/io/streamers/ByteEnumStreamer.as @@ -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 + } +} +} diff --git a/src/java/com/threerings/io/Streamer.java b/src/java/com/threerings/io/Streamer.java index 3146d55f4..84195054e 100644 --- a/src/java/com/threerings/io/Streamer.java +++ b/src/java/com/threerings/io/Streamer.java @@ -40,6 +40,8 @@ import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.common.collect.Maps; +import com.samskivert.util.ByteEnum; +import com.samskivert.util.ByteEnumUtil; import com.samskivert.util.ClassUtil; import static com.threerings.NaryaLog.log; @@ -212,7 +214,11 @@ public class Streamer // if someone serializes an enum to a file and then adds a value to the enum, changing the // ordinal assignments) if (_target.isEnum()) { - out.writeUTF(((Enum)object).name()); + if (object instanceof ByteEnum) { + out.writeByte(((ByteEnum) object).toByte()); + } else { + out.writeUTF(((Enum)object).name()); + } return; } @@ -264,7 +270,11 @@ public class Streamer } @SuppressWarnings("unchecked") Class eclass = (Class)_target; - return Enum.valueOf(eclass, in.readUTF()); + if (ByteEnum.class.isAssignableFrom(_target)) { + return ByteEnumUtil.fromByte(eclass, in.readByte()); + } else { + return Enum.valueOf(eclass, in.readUTF()); + } } else { if (ObjectInputStream.STREAM_DEBUG) { @@ -493,7 +503,10 @@ public class Streamer } /** Used to coerce the type system into quietude when reading enums from the wire. */ - protected static enum EnumReader { NOT_USED } + protected static enum EnumReader implements ByteEnum { + NOT_USED; + public byte toByte () { return 0; } + } /** The class for which this streamer instance is configured. */ protected Class _target;