Files
narya/src/java/com/threerings/presents/io/ByteArrayFieldMarshaller.java
T
Michael Bayne 7a1a162a0d Changed field marshaller registration to be compatible with DashO-style
field renaming. I'm a bad monkey for overusing reflection.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1587 542714f4-19e9-0310-aa3c-eee0fc999fb1
2002-07-17 23:05:28 +00:00

45 lines
1.2 KiB
Java

//
// $Id: ByteArrayFieldMarshaller.java,v 1.2 2002/07/17 23:05:28 mdb Exp $
package com.threerings.presents.io;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.lang.reflect.Field;
public class ByteArrayFieldMarshaller implements FieldMarshaller
{
/** Returns the sort of field that we marshall. */
public Class getFieldType ()
{
return (new byte[0]).getClass();
}
public void writeTo (DataOutputStream out, Field field, Object obj)
throws IOException, IllegalAccessException
{
byte[] value = (byte[])field.get(obj);
// we convert null arrays to zero length arrays
if (value == null) {
out.writeInt(0);
} else {
out.writeInt(value.length);
out.write(value);
}
}
public void readFrom (DataInputStream in, Field field, Object obj)
throws IOException, IllegalAccessException
{
int vlength = in.readInt();
byte[] value = new byte[vlength];
if (in.read(value) != vlength) {
throw new EOFException();
}
field.set(obj, value);
}
}