Renamed HeterogenousStreamableList to the only slightly less verbose but
easier to spell PolyStreamableList. Use ListUtil and String arrays rather than hashtables to track classname to class id mappings. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@988 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
+28
-20
@@ -1,5 +1,5 @@
|
|||||||
//
|
//
|
||||||
// $Id: HeterogenousStreamableList.java,v 1.1 2002/02/11 22:43:18 shaper Exp $
|
// $Id: PolyStreamableList.java,v 1.1 2002/02/12 01:54:51 shaper Exp $
|
||||||
|
|
||||||
package com.threerings.presents.io;
|
package com.threerings.presents.io;
|
||||||
|
|
||||||
@@ -8,33 +8,34 @@ import java.io.DataOutputStream;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
|
||||||
|
|
||||||
import com.samskivert.util.HashIntMap;
|
import com.samskivert.util.ListUtil;
|
||||||
|
|
||||||
|
import com.threerings.presents.Log;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A heterogenous streamable list is a wrapper around an {@link ArrayList}
|
* A poly streamable list is a wrapper around an {@link ArrayList} that
|
||||||
* that knows how to efficiently stream {@link Streamable} objects of
|
* knows how to efficiently stream {@link Streamable} objects of
|
||||||
* potentially heterogenous classes. Accordingly, all objects inserted
|
* potentially heterogenous classes. Accordingly, all objects inserted
|
||||||
* into the list must implement the {@link Streamable} interface. The
|
* into the list must implement the {@link Streamable} interface. The
|
||||||
* list ordering is properly maintained when sending data over the wire.
|
* list ordering is properly maintained when sending data over the wire.
|
||||||
*/
|
*/
|
||||||
public class HeterogenousStreamableList extends ArrayList
|
public class PolyStreamableList extends ArrayList
|
||||||
implements Streamable
|
implements Streamable
|
||||||
{
|
{
|
||||||
// documentation inherited from interface
|
// documentation inherited from interface
|
||||||
public void writeTo (DataOutputStream out)
|
public void writeTo (DataOutputStream out)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
// create a table to map class names to class ids
|
// create a list used to map class names to class ids
|
||||||
HashMap cnames = new HashMap();
|
Object[] cnames = new String[DEFAULT_SIZE];
|
||||||
|
|
||||||
// write out the size of the list
|
// write out the size of the list
|
||||||
int size = size();
|
int size = size();
|
||||||
out.writeInt(size);
|
out.writeInt(size);
|
||||||
|
|
||||||
// write out each of the list elements
|
// write out each of the list elements
|
||||||
int classId = 0;
|
int nextIdx = 0;
|
||||||
for (int ii = 0; ii < size; ii++) {
|
for (int ii = 0; ii < size; ii++) {
|
||||||
// get the next streamable to write out
|
// get the next streamable to write out
|
||||||
Object value = get(ii);
|
Object value = get(ii);
|
||||||
@@ -47,17 +48,19 @@ public class HeterogenousStreamableList extends ArrayList
|
|||||||
|
|
||||||
// look up the class id
|
// look up the class id
|
||||||
String cname = s.getClass().getName();
|
String cname = s.getClass().getName();
|
||||||
Integer cid = (Integer)cnames.get(cname);
|
int cidx = ListUtil.indexOfEqual(cnames, cname);
|
||||||
if (cid == null) {
|
if (cidx == -1) {
|
||||||
// store this class name to class id mapping
|
// store this class name to class id mapping
|
||||||
cid = new Integer(classId++);
|
cidx = nextIdx++;
|
||||||
cnames.put(cname, cid);
|
cnames = ListUtil.add(cnames, cidx, cname);
|
||||||
out.writeInt(cid.intValue());
|
|
||||||
|
// write out the class id and class name
|
||||||
|
out.writeInt(cidx);
|
||||||
out.writeUTF(cname);
|
out.writeUTF(cname);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// we need only write out the class id
|
// we need only write out the class id
|
||||||
out.writeInt(cid.intValue());
|
out.writeInt(cidx);
|
||||||
}
|
}
|
||||||
|
|
||||||
// write out the object itself
|
// write out the object itself
|
||||||
@@ -69,8 +72,8 @@ public class HeterogenousStreamableList extends ArrayList
|
|||||||
public void readFrom (DataInputStream in)
|
public void readFrom (DataInputStream in)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
// create a table to map class ids to class names
|
// create a list used to map class ids to class names
|
||||||
HashIntMap cids = new HashIntMap();
|
Object[] cnames = new String[DEFAULT_SIZE];
|
||||||
|
|
||||||
// read in the size of the list
|
// read in the size of the list
|
||||||
int size = in.readInt();
|
int size = in.readInt();
|
||||||
@@ -78,14 +81,15 @@ public class HeterogenousStreamableList extends ArrayList
|
|||||||
// read in each of the list elements
|
// read in each of the list elements
|
||||||
for (int ii = 0; ii < size; ii++) {
|
for (int ii = 0; ii < size; ii++) {
|
||||||
// read the class id number
|
// read the class id number
|
||||||
int cid = in.readInt();
|
int cidx = in.readInt();
|
||||||
|
|
||||||
// look up the class name
|
// look up the class name
|
||||||
String cname = (String)cids.get(cid);
|
String cname = (cidx > cnames.length - 1) ? null :
|
||||||
|
(String)cnames[cidx];
|
||||||
if (cname == null) {
|
if (cname == null) {
|
||||||
// store this class id to class name mapping
|
// store this class id to class name mapping
|
||||||
cname = in.readUTF();
|
cname = in.readUTF();
|
||||||
cids.put(cid, cname);
|
cnames = ListUtil.add(cnames, cidx, cname);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -121,4 +125,8 @@ public class HeterogenousStreamableList extends ArrayList
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** The default size of the class name lists used when serializing or
|
||||||
|
* unserializing the list. */
|
||||||
|
protected static final int DEFAULT_SIZE = 8;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user