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:
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<EnumReader> eclass =
|
||||
(Class<EnumReader>)_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;
|
||||
|
||||
Reference in New Issue
Block a user