Search in sources :

Example 1 with SerializerException

use of com.jme3.network.serializing.SerializerException in project jmonkeyengine by jMonkeyEngine.

the class FieldSerializer method writeObject.

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());
    if (fields == null)
        throw new IOException("The " + object.getClass() + " is not registered" + " in the serializer!");
    for (SavedField savedField : fields) {
        Object val = null;
        try {
            val = savedField.field.get(object);
        } catch (IllegalAccessException e) {
            throw new SerializerException("Unable to access field:" + savedField.field + " on:" + object, e);
        }
        Serializer serializer = savedField.serializer;
        if (log.isLoggable(Level.FINER)) {
            log.log(Level.FINER, "Writing field:{0} using serializer:{1}", new Object[] { savedField.field, serializer });
        }
        try {
            if (serializer != null) {
                serializer.writeObject(buffer, val);
            } else {
                Serializer.writeClassAndObject(buffer, val);
            }
        } catch (BufferOverflowException boe) {
            throw boe;
        } catch (Exception e) {
            throw new SerializerException("Error writing object for field:" + savedField.field, e);
        }
    }
}
Also used : IOException(java.io.IOException) BufferOverflowException(java.nio.BufferOverflowException) SerializerException(com.jme3.network.serializing.SerializerException) BufferOverflowException(java.nio.BufferOverflowException) IOException(java.io.IOException) SerializerException(com.jme3.network.serializing.SerializerException) Serializer(com.jme3.network.serializing.Serializer)

Example 2 with SerializerException

use of com.jme3.network.serializing.SerializerException in project jmonkeyengine by jMonkeyEngine.

the class FieldSerializer method readObject.

@SuppressWarnings("unchecked")
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);
    T object;
    try {
        Constructor<T> ctor = (Constructor<T>) savedCtors.get(c);
        object = ctor.newInstance();
    } catch (Exception e) {
        throw new SerializerException("Error creating object of type:" + c, e);
    }
    for (SavedField savedField : fields) {
        Field field = savedField.field;
        Serializer serializer = savedField.serializer;
        if (log.isLoggable(Level.FINER)) {
            log.log(Level.FINER, "Reading field:{0} using serializer:{1}", new Object[] { field, serializer });
        }
        Object value;
        if (serializer != null) {
            value = serializer.readObject(data, field.getType());
        } else {
            value = Serializer.readClassAndObject(data);
        }
        try {
            field.set(object, value);
        } catch (IllegalAccessException e) {
            throw new SerializerException("Error reading object", e);
        }
    }
    return object;
}
Also used : Field(java.lang.reflect.Field) Constructor(java.lang.reflect.Constructor) SerializerException(com.jme3.network.serializing.SerializerException) BufferOverflowException(java.nio.BufferOverflowException) IOException(java.io.IOException) SerializerException(com.jme3.network.serializing.SerializerException) Serializer(com.jme3.network.serializing.Serializer)

Aggregations

Serializer (com.jme3.network.serializing.Serializer)2 SerializerException (com.jme3.network.serializing.SerializerException)2 IOException (java.io.IOException)2 BufferOverflowException (java.nio.BufferOverflowException)2 Constructor (java.lang.reflect.Constructor)1 Field (java.lang.reflect.Field)1