Search in sources :

Example 6 with Serializer

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

the class CollectionSerializer method readObject.

@SuppressWarnings("unchecked")
public <T> T readObject(ByteBuffer data, Class<T> c) throws IOException {
    int length = data.getInt();
    Collection collection;
    try {
        collection = (Collection) c.newInstance();
    } catch (Exception e) {
        log.log(Level.FINE, "[Serializer][???] Could not determine collection type. Using ArrayList.");
        collection = new ArrayList(length);
    }
    if (length == 0)
        return (T) collection;
    if (data.get() == (byte) 1) {
        SerializerRegistration reg = Serializer.readClass(data);
        Class clazz = reg.getType();
        Serializer serializer = reg.getSerializer();
        for (int i = 0; i != length; ++i) {
            collection.add(serializer.readObject(data, clazz));
        }
    } else {
        for (int i = 0; i != length; ++i) {
            collection.add(Serializer.readClassAndObject(data));
        }
    }
    return (T) collection;
}
Also used : ArrayList(java.util.ArrayList) Collection(java.util.Collection) IOException(java.io.IOException) SerializerRegistration(com.jme3.network.serializing.SerializerRegistration) Serializer(com.jme3.network.serializing.Serializer)

Example 7 with Serializer

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

the class MapSerializer method readObject.

/*

    Structure:

    struct Map {
        INT length
        BYTE flags = { 0x01 = all keys have the same type,
                       0x02 = all values have the same type }
        if (flags has 0x01 set)
            SHORT keyType
        if (flags has 0x02 set)
            SHORT valType

        struct MapEntry[length] entries {
            if (flags does not have 0x01 set)
                SHORT keyType
            OBJECT key

            if (flags does not have 0x02 set)
                SHORT valType
            OBJECT value
        }
    }

     */
@SuppressWarnings("unchecked")
public <T> T readObject(ByteBuffer data, Class<T> c) throws IOException {
    int length = data.getInt();
    Map map;
    try {
        map = (Map) c.newInstance();
    } catch (Exception e) {
        log.log(Level.WARNING, "[Serializer][???] Could not determine map type. Using HashMap.");
        map = new HashMap();
    }
    if (length == 0)
        return (T) map;
    int flags = data.get() & 0xff;
    boolean uniqueKeys = (flags & 0x01) == 0;
    boolean uniqueVals = (flags & 0x02) == 0;
    Class keyClazz = null;
    Class valClazz = null;
    Serializer keySerial = null;
    Serializer valSerial = null;
    if (!uniqueKeys) {
        SerializerRegistration reg = Serializer.readClass(data);
        keyClazz = reg.getType();
        keySerial = reg.getSerializer();
    }
    if (!uniqueVals) {
        SerializerRegistration reg = Serializer.readClass(data);
        valClazz = reg.getType();
        valSerial = reg.getSerializer();
    }
    for (int i = 0; i < length; i++) {
        Object key;
        Object value;
        if (uniqueKeys) {
            key = Serializer.readClassAndObject(data);
        } else {
            key = keySerial.readObject(data, keyClazz);
        }
        if (uniqueVals) {
            value = Serializer.readClassAndObject(data);
        } else {
            value = valSerial.readObject(data, valClazz);
        }
        map.put(key, value);
    }
    return (T) map;
}
Also used : HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap) IOException(java.io.IOException) SerializerRegistration(com.jme3.network.serializing.SerializerRegistration) Serializer(com.jme3.network.serializing.Serializer)

Example 8 with Serializer

use of com.jme3.network.serializing.Serializer 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)8 IOException (java.io.IOException)4 SerializerException (com.jme3.network.serializing.SerializerException)2 SerializerRegistration (com.jme3.network.serializing.SerializerRegistration)2 BufferOverflowException (java.nio.BufferOverflowException)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Constructor (java.lang.reflect.Constructor)1 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 Entry (java.util.Map.Entry)1