恢复崩溃的输入流
我正在编写一个服务器,并使用 ObjectInputStream
来处理专有数据包。最近,当我想调用 objectInputStream.readObject()
然后尝试将它们转换为专有数据包时,有时会遇到一些意外的 ClassCastException
异常。我一直在尝试追踪问题,并且想知道是否是因为 objectInputStream
由于某些问题而崩溃。根据方法 objectInputStream.readObject()
的文档...“所有异常对于 InputStream 都是致命的,并使其处于不确定状态;由调用者决定忽略或恢复流状态。”
我想知道“恢复流状态”是什么意思......我是否有正确的方法来恢复 objectInputStream 或者我应该执行 socket.getInputStream()
?
I'm writing a server and I'm using an ObjectInputStream
to handle proprietary packets. I've recently been sometimes getting some unexpected ClassCastException
s when I want call objectInputStream.readObject()
and then trying casting them into the proprietary packets. I've been trying to trace the problem and I've been wondering whether it's because the objectInputStream
is crashing due to some problem. According to the documentation for the method objectInputStream.readObject()
... "All exceptions are fatal to the InputStream and leave it in an indeterminate state; it is up to the caller to ignore or recover the stream state."
I'm wondering what they mean by "recover the stream state"... is there a proper way for me to recover the objectInputStream or should I do a socket.getInputStream()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确,您的异常不是由
objectInputStream.readObject()
引起的,而是通过将(成功返回的)对象转换为您尝试读取的任何内容引起的,因此异常应该' t 影响流。If I understand correctly, your exceptions aren't caused by
objectInputStream.readObject()
, but by casting the (successfully returned) object to whatever it is you're trying to read, so the exception shouldn't affect the stream.