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

The following examples show how to use com.jme3.network.serializing.Serializer#writeClassAndObject() . 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: SerializerMessageProtocol.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 *  Converts a message to a ByteBuffer using the com.jme3.network.serializing.Serializer
 *  and the (short length) + data protocol.  If target is null
 *  then a 32k byte buffer will be created and filled.
 */
@Override
public ByteBuffer toByteBuffer( Message message, ByteBuffer target ) {

    // Could let the caller pass their own in       
    ByteBuffer buffer = target == null ? ByteBuffer.allocate(32767 + 2) : target;
    
    try {
        buffer.position(2);
        Serializer.writeClassAndObject(buffer, message);
        buffer.flip();
        short dataLength = (short)(buffer.remaining() - 2);
        buffer.putShort(dataLength);
        buffer.position(0);
        
        return buffer;
    } catch( IOException e ) {
        throw new RuntimeException("Error serializing message", e);
    }
}
 
Example 2
Source File: RmiSerializer.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void writeMethodCall(ByteBuffer buffer, RemoteMethodCallMessage call) throws IOException{
    buffer.putShort((short)call.objectId);
    buffer.putShort(call.methodId);
    buffer.putShort(call.invocationId);
    if (call.args == null){
        buffer.put((byte)0);
    }else{
        buffer.put((byte)call.args.length);

        // Right now it writes 0 for every null argument
        // and 1 for every non-null argument followed by the serialized
        // argument. For the future, using a bit set should be considered.
        for (Object obj : call.args){
            if (obj != null){
                buffer.put((byte)0x01);
                Serializer.writeClassAndObject(buffer, obj);
            }else{
                buffer.put((byte)0x00);
            }
        }
    }
}
 
Example 3
Source File: ZIPSerializer.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 instanceof ZIPCompressedMessage)) return;

    ZIPCompressedMessage zipMessage = (ZIPCompressedMessage)object;
    Message message = zipMessage.getMessage();
    ByteBuffer tempBuffer = ByteBuffer.allocate(512000);
    Serializer.writeClassAndObject(tempBuffer, message);

    ByteArrayOutputStream byteArrayOutput = new ByteArrayOutputStream();
    ZipOutputStream zipOutput = new ZipOutputStream(byteArrayOutput);
    zipOutput.setLevel(zipMessage.getLevel());

    ZipEntry zipEntry = new ZipEntry("zip");

    zipOutput.putNextEntry(zipEntry);
    tempBuffer.flip();
    zipOutput.write(tempBuffer.array(), 0, tempBuffer.limit());
    zipOutput.flush();
    zipOutput.closeEntry();
    zipOutput.close();

    buffer.put(byteArrayOutput.toByteArray());
}
 
Example 4
Source File: GZIPSerializer.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 instanceof GZIPCompressedMessage)) return;
    Message message = ((GZIPCompressedMessage)object).getMessage();

    ByteBuffer tempBuffer = ByteBuffer.allocate(512000);
    Serializer.writeClassAndObject(tempBuffer, message);

    ByteArrayOutputStream byteArrayOutput = new ByteArrayOutputStream();
    GZIPOutputStream gzipOutput = new GZIPOutputStream(byteArrayOutput);

    tempBuffer.flip();
    gzipOutput.write(tempBuffer.array(), 0, tempBuffer.limit());
    gzipOutput.flush();
    gzipOutput.finish();
    gzipOutput.close();

    buffer.put(byteArrayOutput.toByteArray());
}
 
Example 5
Source File: ArraySerializer.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void writeArray(Serializer elementSerializer, ByteBuffer buffer, Object array, int dimension, int dimensionCount) throws IOException {
    int length = Array.getLength(array);
    if (dimension > 0) {
        buffer.putInt(length);
    }
    // Write array data.
    boolean elementsAreArrays = dimension < dimensionCount - 1;
    for (int i = 0; i < length; i++) {
        Object element = Array.get(array, i);
        if (elementsAreArrays) {
            if (element != null) writeArray(elementSerializer, buffer, element, dimension + 1, dimensionCount);
        } else if (elementSerializer != null) {
            elementSerializer.writeObject(buffer, element);
        } else {
            // Each element could be a different type. Store the class with the object.
            Serializer.writeClassAndObject(buffer, element);
        }
    }
}
 
Example 6
Source File: MessageProtocol.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 *  Converts a message to a ByteBuffer using the Serializer
 *  and the (short length) + data protocol.  If target is null
 *  then a 32k byte buffer will be created and filled.
 */
public static ByteBuffer messageToBuffer( Message message, ByteBuffer target )
{
    // Could let the caller pass their own in       
    ByteBuffer buffer = target == null ? ByteBuffer.allocate( 32767 + 2 ) : target;
    
    try {
        buffer.position( 2 );
        Serializer.writeClassAndObject( buffer, message );
        buffer.flip();
        short dataLength = (short)(buffer.remaining() - 2);
        buffer.putShort( dataLength );
        buffer.position( 0 );
        
        return buffer;
    } catch( IOException e ) {
        throw new RuntimeException( "Error serializing message", e );
    }
}
 
Example 7
Source File: RmiSerializer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void writeMethodCall(ByteBuffer buffer, RemoteMethodCallMessage call) throws IOException{
    buffer.putShort((short)call.objectId);
    buffer.putShort(call.methodId);
    buffer.putShort(call.invocationId);
    if (call.args == null){
        buffer.put((byte)0);
    }else{
        buffer.put((byte)call.args.length);

        // Right now it writes 0 for every null argument
        // and 1 for every non-null argument followed by the serialized
        // argument. For the future, using a bit set should be considered.
        for (Object obj : call.args){
            if (obj != null){
                buffer.put((byte)0x01);
                Serializer.writeClassAndObject(buffer, obj);
            }else{
                buffer.put((byte)0x00);
            }
        }
    }
}
 
Example 8
Source File: ZIPSerializer.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 instanceof ZIPCompressedMessage)) return;

    ZIPCompressedMessage zipMessage = (ZIPCompressedMessage)object;
    Message message = zipMessage.getMessage();
    ByteBuffer tempBuffer = ByteBuffer.allocate(512000);
    Serializer.writeClassAndObject(tempBuffer, message);

    ByteArrayOutputStream byteArrayOutput = new ByteArrayOutputStream();
    ZipOutputStream zipOutput = new ZipOutputStream(byteArrayOutput);
    zipOutput.setLevel(zipMessage.getLevel());

    ZipEntry zipEntry = new ZipEntry("zip");

    zipOutput.putNextEntry(zipEntry);
    zipOutput.write(tempBuffer.array());
    zipOutput.flush();
    zipOutput.closeEntry();
    zipOutput.close();

    buffer.put(byteArrayOutput.toByteArray());
}
 
Example 9
Source File: GZIPSerializer.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 instanceof GZIPCompressedMessage)) return;
    Message message = ((GZIPCompressedMessage)object).getMessage();

    ByteBuffer tempBuffer = ByteBuffer.allocate(512000);
    Serializer.writeClassAndObject(tempBuffer, message);

    ByteArrayOutputStream byteArrayOutput = new ByteArrayOutputStream();
    GZIPOutputStream gzipOutput = new GZIPOutputStream(byteArrayOutput);

    gzipOutput.write(tempBuffer.array());
    gzipOutput.flush();
    gzipOutput.finish();
    gzipOutput.close();

    buffer.put(byteArrayOutput.toByteArray());
}
 
Example 10
Source File: ArraySerializer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void writeArray(Serializer elementSerializer, ByteBuffer buffer, Object array, int dimension, int dimensionCount) throws IOException {
    int length = Array.getLength(array);
    if (dimension > 0) {
        buffer.putInt(length);
    }
    // Write array data.
    boolean elementsAreArrays = dimension < dimensionCount - 1;
    for (int i = 0; i < length; i++) {
        Object element = Array.get(array, i);
        if (elementsAreArrays) {
            if (element != null) writeArray(elementSerializer, buffer, element, dimension + 1, dimensionCount);
        } else if (elementSerializer != null) {
            elementSerializer.writeObject(buffer, element);
        } else {
            // Each element could be a different type. Store the class with the object.
            Serializer.writeClassAndObject(buffer, element);
        }
    }
}
 
Example 11
Source File: RmiSerializer.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void writeMethodReturn(ByteBuffer buffer, RemoteMethodReturnMessage ret) throws IOException{
    buffer.putShort(ret.invocationID);
    if (ret.retVal != null){
        buffer.put((byte)0x01);
        Serializer.writeClassAndObject(buffer, ret.retVal);
    }else{
        buffer.put((byte)0x00);
    }
}
 
Example 12
Source File: RmiSerializer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void writeMethodReturn(ByteBuffer buffer, RemoteMethodReturnMessage ret) throws IOException{
    buffer.putShort(ret.invocationID);
    if (ret.retVal != null){
        buffer.put((byte)0x01);
        Serializer.writeClassAndObject(buffer, ret.retVal);
    }else{
        buffer.put((byte)0x00);
    }
}
 
Example 13
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 14
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());
        }
    }
}