Use an extra byte per final-typed field to handle
the case of nulls. Note: this is specific to serialized objects and is therefore not adding a byte to things like Strings or arrays... those will have to be reviewed separately. git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@7119 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
This commit is contained in:
parent
3e5973793e
commit
5012b2d3dd
engine/src/networking/com/jme3/network/serializing
@ -132,7 +132,7 @@ public abstract class Serializer {
|
|||||||
Serializable serializable = (Serializable)cls.getAnnotation(Serializable.class);
|
Serializable serializable = (Serializable)cls.getAnnotation(Serializable.class);
|
||||||
|
|
||||||
Class serializerClass = serializable.serializer();
|
Class serializerClass = serializable.serializer();
|
||||||
short classId = serializable.id();
|
short classId = serializable.id();
|
||||||
if (classId == 0) classId = --nextId;
|
if (classId == 0) classId = --nextId;
|
||||||
|
|
||||||
Serializer serializer = getSerializer(serializerClass);
|
Serializer serializer = getSerializer(serializerClass);
|
||||||
|
@ -88,6 +88,11 @@ public class FieldSerializer extends Serializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public <T> T readObject(ByteBuffer data, Class<T> c) throws IOException {
|
public <T> T readObject(ByteBuffer data, Class<T> c) throws IOException {
|
||||||
|
|
||||||
|
// Read the null/non-null marker
|
||||||
|
if (data.get() == 0x0)
|
||||||
|
return null;
|
||||||
|
|
||||||
SavedField[] fields = savedFields.get(c);
|
SavedField[] fields = savedFields.get(c);
|
||||||
|
|
||||||
T object;
|
T object;
|
||||||
@ -117,6 +122,14 @@ public class FieldSerializer extends Serializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void writeObject(ByteBuffer buffer, Object object) throws IOException {
|
public void writeObject(ByteBuffer buffer, Object object) throws IOException {
|
||||||
|
|
||||||
|
// Add the null/non-null marker
|
||||||
|
buffer.put( (byte)(object != null ? 0x1 : 0x0) );
|
||||||
|
if (object == null) {
|
||||||
|
// Nothing left to do
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
SavedField[] fields = savedFields.get(object.getClass());
|
SavedField[] fields = savedFields.get(object.getClass());
|
||||||
if (fields == null)
|
if (fields == null)
|
||||||
throw new IOException("The " + object.getClass() + " is not registered"
|
throw new IOException("The " + object.getClass() + " is not registered"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user