From 5e303799a23a72168b27f5996c396b7f0ec35226 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Thu, 7 Feb 2002 05:27:07 +0000 Subject: [PATCH] Added a field marshaller for byte arrays. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@958 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../presents/io/ByteArrayFieldMarshaller.java | 41 +++++++++++++++++++ .../presents/io/FieldMarshallerRegistry.java | 3 +- 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/java/com/threerings/presents/io/ByteArrayFieldMarshaller.java diff --git a/src/java/com/threerings/presents/io/ByteArrayFieldMarshaller.java b/src/java/com/threerings/presents/io/ByteArrayFieldMarshaller.java new file mode 100644 index 000000000..894d9a410 --- /dev/null +++ b/src/java/com/threerings/presents/io/ByteArrayFieldMarshaller.java @@ -0,0 +1,41 @@ +// +// $Id: ByteArrayFieldMarshaller.java,v 1.1 2002/02/07 05:27:07 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 +{ + /** This is the sort of field that we marshall. */ + public byte[] prototype; + + 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); + } +} diff --git a/src/java/com/threerings/presents/io/FieldMarshallerRegistry.java b/src/java/com/threerings/presents/io/FieldMarshallerRegistry.java index ccc1c53ac..d7d48feeb 100644 --- a/src/java/com/threerings/presents/io/FieldMarshallerRegistry.java +++ b/src/java/com/threerings/presents/io/FieldMarshallerRegistry.java @@ -1,5 +1,5 @@ // -// $Id: FieldMarshallerRegistry.java,v 1.12 2002/02/03 06:06:10 shaper Exp $ +// $Id: FieldMarshallerRegistry.java,v 1.13 2002/02/07 05:27:07 mdb Exp $ package com.threerings.presents.io; @@ -85,6 +85,7 @@ public class FieldMarshallerRegistry registerMarshaller(FloatFieldMarshaller.class); registerMarshaller(DoubleFieldMarshaller.class); registerMarshaller(StringFieldMarshaller.class); + registerMarshaller(ByteArrayFieldMarshaller.class); registerMarshaller(IntArrayFieldMarshaller.class); registerMarshaller(StringArrayFieldMarshaller.class); registerMarshaller(OidListFieldMarshaller.class);