如何在输入字节流上创建 readInt() 块?
我有一个来自黑匣子的输入流(比如 B)。来自该流的所有消息都是序列化的二进制数据,每条消息都以四字节 int 开头。其中大部分用于记录数据并每天 24 小时运行。我使用 readInt() 方法读取这四个字节。现在,主线程有时会因 EOFException 退出并导致程序崩溃。
经过研究,我发现当 readInt() 时输入流中的字节少于四个字节时,就会发生这种情况。我的猜测是缓冲区在连续读取之间填充得不够快。我正在考虑的一些可能的解决方案包括在读取之前检查 available() (考虑到数据量,消耗太多周期)或在发生异常时重新启动(听起来像是糟糕的编程)。我认为,如果我可以使用 readInt() 进行阻止,那将是最好的方法。我已经研究了 readInt() 的实现,但它再次归结为使用 read() 进行阻塞。
有人知道更好的解决方案吗?
I have an input stream coming form a blackbox (say B). All the messages coming in from this stream are serialized binary data and each message starts with a four byte int. Most of it is logging data and runs 24 hrs a day. I read these four bytes using readInt() method. Now, ocasionally, the main thread would exit with EOFException and crash the program.
After researching on this, I found that it happens when there are less than four bytes in the input stream at the time of readInt(). My guess is that the buffer is not filling in fast enough between successive reads. Some of the possible solutions I am thinking of include checking available() before reading (consumes too many cycles considering the amt of data) or restart when exception occurs (sounds like poor programing). If only I could block using readInt(), it would be the best way, I think. I've looked at implementation of readInt() but again it boils down to blocking with read().
Anyone knows of a better solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
调用层次结构中的任何阻塞调用都会“绑定”,以使链上的所有调用都阻塞,因为这两个调用都是同一执行线程的一部分。
DataInputStream
的readInt
方法对底层输入流的read
方法进行了四次调用,只要不发送数据,该方法肯定会阻塞因此,您对“缓冲区填充速度不够快”的担心似乎不合逻辑。我在服务器进程终止或断开连接的情况下遇到过此类异常,在这种情况下客户端最终读取 -1 并抛出异常。您是否在客户端/服务器代码中遇到任何类型的异常?您的日志是否显示任何可疑内容?
Any blocking call down the call hierarchy is "bound" to make all the calls up the chain blocking as along as both calls are part of same thread of execution. The
readInt
method ofDataInputStream
makes four calls to theread
method of the underlying input stream which will surely block as long as the data is not made available hence your fear of "buffer doesn't fill in fast enough" doesn't seem to be logical.I have encountered these kind of exceptions in cases where either the server process dies or or drops the connection in which case the client ends up reading a -1 and throws an exception. Are you gobbling any sort of exceptions in your client/server code? Do your logs show anything suspicious?
我相信您正在使用 DataInputStream。当它包装的流从 read() 方法返回 -1(实际上会阻塞,直到输入数据可用)时,该类会抛出 EOFException。
我想,您应该首先看看为什么主流的读取返回 -1。
I believe that you are using DataInputStream. That class throws EOFException in situation when the stream which it wraps, returns -1 from the read() method (which actually blocks until input data is available).
I suppose, you should have a look why the main stream's read returns with -1 in the first place.
基本的 InputStream 接口需要阻塞读取,当 readInt() 遇到流结束标记时,会抛出 EOFException 异常,因为返回不完整的 int 是一个坏主意,它会抛出 End Of 文件异常。
抛出 EOFException 因为另一个流的末尾已到达末尾,已关闭或不再连接。您应该检查黑盒是否终止连接。
由于流是基于网络的,您的套接字可能设置了超时,如果是这种情况,请尝试更改 SOTimeout 套接字的值。
The basic InputStream interface requires blocking reads, the EOFException you get is thrown when the readInt() encounters the end of stream marker, since returning the incomplete int would be a bad idea it throws the End Of File Exception.
The EOFException is thrown because the other end of the stream reached its end,has been closed or is no longer connected. You should check if the blackbox terminates the connection.
Since the stream is network based your socket may have a timeout set, if this is the case try changing the SOTimeout value of the socket.