From 6a0ee2f291c8c10193e6bd9a9efb8cb0fd2226b1 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Tue, 19 Feb 2002 03:30:55 +0000 Subject: [PATCH] Added support for null Streamable fields. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1029 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../io/StreamableFieldMarshaller.java | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/java/com/threerings/presents/io/StreamableFieldMarshaller.java b/src/java/com/threerings/presents/io/StreamableFieldMarshaller.java index b444e0313..82812cc05 100644 --- a/src/java/com/threerings/presents/io/StreamableFieldMarshaller.java +++ b/src/java/com/threerings/presents/io/StreamableFieldMarshaller.java @@ -1,5 +1,5 @@ // -// $Id: StreamableFieldMarshaller.java,v 1.3 2002/02/01 23:32:37 mdb Exp $ +// $Id: StreamableFieldMarshaller.java,v 1.4 2002/02/19 03:30:55 mdb Exp $ package com.threerings.presents.io; @@ -18,20 +18,29 @@ public class StreamableFieldMarshaller implements FieldMarshaller Streamable value = (Streamable)field.get(obj); // we freak out if our streamable is null if (value == null) { - throw new IllegalAccessException( - "No streamable instance to marshall!"); + out.writeByte(0); + } else { + out.writeByte(1); + value.writeTo(out); } - value.writeTo(out); } public void readFrom (DataInputStream in, Field field, Object obj) throws IOException, IllegalAccessException { try { - // create a new instance into which to unmarshall the field - Streamable value = (Streamable)field.getType().newInstance(); - // unserialize it - value.readFrom(in); + byte isNull = in.readByte(); + Streamable value = null; + + // instantiate and unserialize the streamable if we actually + // have a value + if (isNull == 1) { + // create a new instance into which to unmarshall the field + value = (Streamable)field.getType().newInstance(); + // unserialize it + value.readFrom(in); + } + // and set the value in the object field.set(obj, value);