SerializableSerializer

Hey Guys.
We just stumbled over an unimplemented method and made it work. Here is the code, if you want to use it. I will check if there are questions :slight_smile:

[java]package com.jme3.network.serializing.serializers;

import com.jme3.network.serializing.Serializer;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.nio.ByteBuffer;
import java.util.logging.Level;
import java.util.logging.Logger;

/**

  • Serializes uses Java built-in method.

  • @author Agares
    */
    @SuppressWarnings(“unchecked”)
    public class SerializableSerializer extends Serializer {

    @Override
    public Serializable readObject(ByteBuffer data, Class c) throws IOException {
    byte[] buf = data.array();
    ByteArrayInputStream bis = new ByteArrayInputStream(buf);
    ObjectInputStream ois = new ObjectInputStream(bis);
    ois.close();
    bis.close();
    try {
    return (Serializable) ois.readObject();
    } catch (ClassNotFoundException ex) {
    Logger.getLogger(SerializableSerializer.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
    }

    @Override
    public void writeObject(ByteBuffer buffer, Object object) throws IOException {
    if (object instanceof Serializable){
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(bos);
    oos.writeObject(object);
    oos.flush();
    oos.close();
    bos.close();
    byte[] byteArr = bos.toByteArray();
    buffer.put(byteArr);
    }
    }

}[/java]

regards,
Tirnithil

1 Like

I don’t have the code in front of me at the moment… which method was unimplemented?

@pspeed

Both of them. The code in jme3 Networking looked like this:

[java]
public class SerializableSerializer extends Serializer {

public Serializable readObject(ByteBuffer data, Class c) throws IOException {
    throw new UnsupportedOperationException( "Serializable serialization not supported." );
}

public void writeObject(ByteBuffer buffer, Object object) throws IOException {
    throw new UnsupportedOperationException( "Serializable serialization not supported." );
}

}[/java]

I’m not sure when I will get to this and I will need to tweak it before it’s ready. For example, as it stands, it silently corrupts the stream if a class is not found… and the stream is closed before it is used. (I’m surprised it works at all, actually.)

An aside: sending a raw Serializable object over the network is pretty inefficient. I can’t remember whether these methods were never implemented or were previously implemented poorly and I stubbed them out figuring no one would ever use them.