Java的序列化,ObjectInputStream的,非阻塞I / O [英] Java Serializable, ObjectInputstream, Non-blocking I/O
问题描述
我刚刚开始接触Java序列化,我不是你应该如何从一个源对象的情况下与非阻塞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 .
所有我能找到的文档建议使用ObjectInputStream的是正确的方式序列化对象读取。但是,正如我所说,我使用的java.nio和执行非阻塞操作。如果调用readObject()将阻塞,直到一个新的对象是可用的,这并不能帮助我
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
摘要..如何与Java NIO的工作,当你做的序列?
Summary .. How do you do serialization when working with Java NIO?
推荐答案
在总结报告的有效载荷长度的协议序列化实例,和有效载荷是有问题的实例。然后,一旦你知道你有重新presents一个完整的实例,你可以使用ObjectInputStream的安全知道它不会阻止段。
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.
协议这样的
前32位:有效载荷长度
有效载荷长度位:序列数据
Protocol like this First 32 bits: Payload length Payload length bits: Serialized data
有时我惊奇甚至自己。
这篇关于Java的序列化,ObjectInputStream的,非阻塞I / O的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!