So, these classes like Integer and langBoolean exist solely for

streaming to our java server, so let's just make them streamable directly.
Does away with the need for custom Streamers.
Also made them Hashable, in case we read a StreamableHashMap containing
Integers as keys, for example.
Less code is better code.
(Why was BooleanStreamer added the other day? langBoolean was already Streamable
 and was already in Translations)


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5960 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Ray Greenwell
2009-09-17 21:42:25 +00:00
parent 055aac9b55
commit a24b0d2d03
14 changed files with 147 additions and 368 deletions
+36 -14
View File
@@ -24,20 +24,22 @@ package com.threerings.util {
import flash.utils.ByteArray;
import flash.utils.Endian;
import com.threerings.io.ObjectInputStream;
import com.threerings.io.ObjectOutputStream;
import com.threerings.io.Streamable;
/**
* Equivalent to java.lang.Long.
*/
public class Long
implements Equalable
public final class Long
implements Hashable, Streamable
{
public var bytes :ByteArray;
public function Long ()
{
bytes = new ByteArray();
bytes.endian = Endian.BIG_ENDIAN;
bytes.writeDouble(0);
bytes.position = 0;
_bytes = new ByteArray();
_bytes.endian = Endian.BIG_ENDIAN;
_bytes.writeDouble(0);
_bytes.position = 0;
}
/**
@@ -57,7 +59,7 @@ public class Long
var n :Number = Math.round(value);
var l :Long = new Long();
for (var ii :int = 7; ii >= 0; ii--) {
l.bytes[ii] = (n % 256);
l._bytes[ii] = (n % 256);
n = Math.floor(n / 256);
}
return l;
@@ -70,10 +72,10 @@ public class Long
public function toNumber () :Number
{
var n :Number = 0;
var positive :Boolean = ((bytes[0] & 0x80) == 0x00);
var positive :Boolean = ((_bytes[0] & 0x80) == 0x00);
for (var ii :int = 0; ii < 8; ii++) {
// if the number is negative, complement each byte as it comes in, and fix up later
n = n * 256 + (positive ? bytes[ii] : (255 - bytes[ii]));
n = n * 256 + (positive ? _bytes[ii] : (255 - _bytes[ii]));
}
// now fix up negative numbers
if (! positive) {
@@ -88,8 +90,8 @@ public class Long
for (var ii :int = 0; ii < 8; ii++) {
// my kingdom for a hex formatting routine!
if (ii != 0) { s += " "; }
if (bytes[ii] < 16) { s += "0"; }
s += int(bytes[ii]).toString(16);
if (_bytes[ii] < 16) { s += "0"; }
s += int(_bytes[ii]).toString(16);
}
s += "]";
return s;
@@ -98,7 +100,27 @@ public class Long
// from Equalable
public function equals (other :Object) :Boolean
{
return (other is Long) && Util.equals(this.bytes, Long(other).bytes);
return (other is Long) && Util.equals(_bytes, Long(other)._bytes);
}
// from Hashable
public function hashCode () :int
{
return _bytes[0];
}
// from Streamable
public function readObject (ins :ObjectInputStream) :void
{
ins.readBytes(_bytes, 0, 8);
}
// from Streamable
public function writeObject (out :ObjectOutputStream) :void
{
out.writeBytes(_bytes, 0, 8);
}
protected var _bytes :ByteArray;
}
}