Search in sources :

Example 1 with Serializer

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

the class CollectionSerializer method writeObject.

public void writeObject(ByteBuffer buffer, Object object) throws IOException {
    Collection collection = (Collection) object;
    int length = collection.size();
    buffer.putInt(length);
    if (length == 0)
        return;
    Iterator it = collection.iterator();
    Class elementClass = it.next().getClass();
    while (it.hasNext()) {
        Object obj = it.next();
        if (obj.getClass() != elementClass) {
            elementClass = null;
            break;
        }
    }
    if (elementClass != null) {
        buffer.put((byte) 1);
        Serializer.writeClass(buffer, elementClass);
        Serializer serializer = Serializer.getSerializer(elementClass);
        for (Object elem : collection) {
            serializer.writeObject(buffer, elem);
        }
    } else {
        buffer.put((byte) 0);
        for (Object elem : collection) {
            Serializer.writeClassAndObject(buffer, elem);
        }
    }
}
Also used : Iterator(java.util.Iterator) Collection(java.util.Collection) Serializer(com.jme3.network.serializing.Serializer)

Example 2 with Serializer

use of com.jme3.network.serializing.Serializer 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 3 with Serializer

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

the class MapSerializer method writeObject.

@SuppressWarnings("unchecked")
public void writeObject(ByteBuffer buffer, Object object) throws IOException {
    Map map = (Map) object;
    int length = map.size();
    buffer.putInt(length);
    if (length == 0)
        return;
    Set<Entry> entries = map.entrySet();
    Iterator<Entry> it = entries.iterator();
    Entry entry = it.next();
    Class keyClass = entry.getKey().getClass();
    Class valClass = entry.getValue().getClass();
    while (it.hasNext()) {
        entry = it.next();
        if (entry.getKey().getClass() != keyClass) {
            keyClass = null;
            if (valClass == null)
                break;
        }
        if (entry.getValue().getClass() != valClass) {
            valClass = null;
            if (keyClass == null)
                break;
        }
    }
    boolean uniqueKeys = keyClass == null;
    boolean uniqueVals = valClass == null;
    int flags = 0;
    if (!uniqueKeys)
        flags |= 0x01;
    if (!uniqueVals)
        flags |= 0x02;
    buffer.put((byte) flags);
    Serializer keySerial = null, valSerial = null;
    if (!uniqueKeys) {
        Serializer.writeClass(buffer, keyClass);
        keySerial = Serializer.getSerializer(keyClass);
    }
    if (!uniqueVals) {
        Serializer.writeClass(buffer, valClass);
        valSerial = Serializer.getSerializer(valClass);
    }
    it = entries.iterator();
    while (it.hasNext()) {
        entry = it.next();
        if (uniqueKeys) {
            Serializer.writeClassAndObject(buffer, entry.getKey());
        } else {
            keySerial.writeObject(buffer, entry.getKey());
        }
        if (uniqueVals) {
            Serializer.writeClassAndObject(buffer, entry.getValue());
        } else {
            valSerial.writeObject(buffer, entry.getValue());
        }
    }
}
Also used : Entry(java.util.Map.Entry) Map(java.util.Map) HashMap(java.util.HashMap) Serializer(com.jme3.network.serializing.Serializer)

Example 4 with Serializer

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

the class ArraySerializer method readObject.

public <T> T readObject(ByteBuffer data, Class<T> c) throws IOException {
    byte dimensionCount = data.get();
    if (dimensionCount == 0)
        return null;
    int[] dimensions = new int[dimensionCount];
    for (int i = 0; i < dimensionCount; i++) dimensions[i] = data.getInt();
    Serializer elementSerializer = null;
    Class elementClass = c;
    while (elementClass.getComponentType() != null) elementClass = elementClass.getComponentType();
    if (Modifier.isFinal(elementClass.getModifiers()))
        elementSerializer = Serializer.getSerializer(elementClass);
    // Create array and read in the data.
    T array = (T) Array.newInstance(elementClass, dimensions);
    readArray(elementSerializer, elementClass, data, array, 0, dimensions);
    return array;
}
Also used : Serializer(com.jme3.network.serializing.Serializer)

Example 5 with Serializer

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

the class ArraySerializer method writeObject.

public void writeObject(ByteBuffer buffer, Object object) throws IOException {
    if (object == null) {
        buffer.put((byte) 0);
        return;
    }
    int[] dimensions = getDimensions(object);
    buffer.put((byte) dimensions.length);
    for (int dimension : dimensions) buffer.putInt(dimension);
    Serializer elementSerializer = null;
    Class elementClass = object.getClass();
    while (elementClass.getComponentType() != null) {
        elementClass = elementClass.getComponentType();
    }
    if (Modifier.isFinal(elementClass.getModifiers()))
        elementSerializer = Serializer.getSerializer(elementClass);
    writeArray(elementSerializer, buffer, object, 0, dimensions.length);
}
Also used : 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