Java Code Examples for com.jme3.network.serializing.Serializer#getSerializer()

The following examples show how to use com.jme3.network.serializing.Serializer#getSerializer() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ArraySerializer.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
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;
}
 
Example 2
Source File: ArraySerializer.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
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);
}
 
Example 3
Source File: ArraySerializer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
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;
}
 
Example 4
Source File: ArraySerializer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
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);
}
 
Example 5
Source File: MapSerializer.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
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());
        }
    }
}
 
Example 6
Source File: FieldSerializer.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void initialize(Class clazz) {

    checkClass(clazz);   

    List<Field> fields = new ArrayList<Field>();

    Class processingClass = clazz;
    while (processingClass != Object.class ) {
        Collections.addAll(fields, processingClass.getDeclaredFields());
        processingClass = processingClass.getSuperclass();
    }

    List<SavedField> cachedFields = new ArrayList<SavedField>(fields.size());
    for (Field field : fields) {
        int modifiers = field.getModifiers();
        if (Modifier.isTransient(modifiers)) continue;
        if (Modifier.isFinal(modifiers)) continue;
        if (Modifier.isStatic(modifiers)) continue;
        if (field.isSynthetic()) continue;
        field.setAccessible(true);

        SavedField cachedField = new SavedField();
        cachedField.field = field;

        if (Modifier.isFinal(field.getType().getModifiers())) {
            // The type of this field is implicit in the outer class
            // definition and because the type is final, it can confidently
            // be determined on the other end.
            // Note: passing false to this method has the side-effect that field.getType()
            // will be registered as a real class that can then be read/written
            // directly as any other registered class.  It should be safe to take
            // an ID like this because Serializer.initialize() is only called 
            // during registration... so this is like nested registration and
            // doesn't have any ordering problems.
            // ...well, as long as the order of fields is consistent from one
            // end to the next. 
            cachedField.serializer = Serializer.getSerializer(field.getType(), false);
        }                

        cachedFields.add(cachedField);
    }

    Collections.sort(cachedFields, new Comparator<SavedField>() {
        @Override
        public int compare (SavedField o1, SavedField o2) {
                return o1.field.getName().compareTo(o2.field.getName());
        }
    });
    savedFields.put(clazz, cachedFields.toArray(new SavedField[cachedFields.size()]));

    
}
 
Example 7
Source File: MapSerializer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@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());
        }
    }
}
 
Example 8
Source File: FieldSerializer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void initialize(Class clazz) {

        checkClass(clazz);   
    
        List<Field> fields = new ArrayList<Field>();

        Class processingClass = clazz;
        while (processingClass != Object.class ) {
            Collections.addAll(fields, processingClass.getDeclaredFields());
            processingClass = processingClass.getSuperclass();
        }

        List<SavedField> cachedFields = new ArrayList<SavedField>(fields.size());
        for (Field field : fields) {
            int modifiers = field.getModifiers();
            if (Modifier.isTransient(modifiers)) continue;
            if (Modifier.isFinal(modifiers)) continue;
            if (Modifier.isStatic(modifiers)) continue;
            if (field.isSynthetic()) continue;
            field.setAccessible(true);

            SavedField cachedField = new SavedField();
            cachedField.field = field;

            if (Modifier.isFinal(field.getType().getModifiers())) {
                // The type of this field is implicit in the outer class
                // definition and because the type is final, it can confidently
                // be determined on the other end.
                // Note: passing false to this method has the side-effect that field.getType()
                // will be registered as a real class that can then be read/written
                // directly as any other registered class.  It should be safe to take
                // an ID like this because Serializer.initialize() is only called 
                // during registration... so this is like nested registration and
                // doesn't have any ordering problems.
                // ...well, as long as the order of fields is consistent from one
                // end to the next. 
                cachedField.serializer = Serializer.getSerializer(field.getType(), false);
            }                

            cachedFields.add(cachedField);
        }

        Collections.sort(cachedFields, new Comparator<SavedField>() {
            public int compare (SavedField o1, SavedField o2) {
                    return o1.field.getName().compareTo(o2.field.getName());
            }
        });
        savedFields.put(clazz, cachedFields.toArray(new SavedField[cachedFields.size()]));

        
    }