Network Serialization

How do you use the Serializer to write and read from ByteBuffer?



I tried and I keep getting a java.lang.UnsupportedOperationException



[java]

Serializer.setStrictRegistration(false);

ByteBuffer buffer = BufferUtils.createByteBuffer(1024);

Serializer.writeClassAndObject(buffer, o); // o is a object

return buffer.array();[/java]

I’d probably have to see the stack trace for the exception and the rest of its message (if there was one).



Note: if you don’t register your classes ahead of time then you likely won’t be able to read the data again… since reading will definitely require registration and it has to be done in exactly the same order. (Unless you are giving everything specific IDs… then the order doesn’t matter as much but you’ll still need to register ahead of reading.)

Here is the stacktrace.



00:00 ERROR: ERROR

java.lang.UnsupportedOperationException

at java.nio.ByteBuffer.array(ByteBuffer.java:940)

at shared.util.Cryption.encryptObject(Cryption.java:25)

at shared.network.packets.SecureMessage.setData(SecureMessage.java:26)

at server.MainApplication.main(MainApplication.java:37)

Looks to me like the bytebuffer.array is the problem. The object is registered and the underlying types in the class are registered as well.

What’s this doing:

shared.util.Cryption.encryptObject(Cryption.java:25)



What is that code trying to do and to which ByteBuffer?

By the way… 99.999% sure this is creating a direct memory buffer:

ByteBuffer buffer = BufferUtils.createByteBuffer(1024);



…you probably don’t want that as the memory is native and therefore access from Java is slightly slower and you can’t do things like array().