Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Serializable, ObjectInputstream, Non-blocking I/O

I'm just starting out with Java serialization, and I'm not clear on how you are supposed to get objects from a source in a scenario with non-blocking I/O .

All the documentation I can find suggests using ObjectInputStream is the proper way to read in serialized objects. However, as I mentioned I'm using java.nio and performing non-blocking operations. If readObject() will block until a new object is available, this can't help me

Summary .. How do you do serialization when working with Java NIO?

like image 864
Mike Avatar asked Sep 26 '09 16:09

Mike


1 Answers

Wrap the serialized instances in a protocol that reports a payload length, and the payload is the instance in question. Then once you know you have a segment that represents a complete instance you can use ObjectInputStream safely knowing it won't block.

Protocol like this First 32 bits: Payload length Payload length bits: Serialized data

Sometimes I amaze even myself.

like image 69
Mike Avatar answered Oct 05 '22 16:10

Mike