简体   繁体   English

将InputStream(阻塞)的内容写入非阻塞套接字

[英]Write contents of an InputStream (blocking) to a non-blocking socket

I'm programming a simple Java NIO server and have a little headache: I get normal InputStream si need to pipe to my clients. 我正在编写一个简单的Java NIO服务器并且有点头疼:我得到普通的InputStream需要管道到我的客户端。 I have a single thread performing all writes, so this creates a problem: if the InputStream blocks, all other connection writing will be paused. 我有一个线程执行所有写操作,因此这会产生一个问题:如果InputStream阻塞, 所有其他连接写入将被暂停。

I can use InputStream.available() to check if there are any incoming data I can read without blocking, but if I've reached end-of-stream it seems I must call read() to know. 我可以使用InputStream.available()来检查是否有任何传入的数据我可以无阻塞地读取,但如果我已经到达流末尾,我似乎必须调用read()才能知道。

This creates a major headache for me, but I can't possibly believe I'm the first to have this problem. 这给我带来了很大的麻烦,但我不可能相信我是第一个遇到这个问题的人。

The only options I've come up with so far: 到目前为止我提出的唯一选择:

  • Have a separate thread for each InputStream , however that's just silly since I'm using non-blocking I/O in the first place. 为每个InputStream都有一个单独的线程,但这只是愚蠢的,因为我首先使用的是非阻塞I / O. I could also have a thread pool doing this but then again that limits the amount of simultaneous clients I can pipe the InputStream to. 我也可以让一个线程池执行此操作但是再次限制了我可以将InputStream传递给的并发客户端的数量。
  • Have a separate thread reading these streams with a timeout (using another thread to interrupt if reading has lasted longer than a certain amount of time), but that'll most certainly choke the data flow should I have many open InputStream s not delivering data. 有一个单独的线程读取这些流超时(如果读取持续时间超过一定的时间,则使用另一个线程来中断),但如果我有许多打开的InputStream不提供数据,那么这肯定会阻塞数据流。

Of course, if there was a magic InputStream.isEof() or isClosed() then this wouldn't be any problem at all :'( 当然,如果有一个神奇的InputStream.isEof()isClosed()那么这根本不会有任何问题:'(

" .....Have a separate thread for each InputStream, however that's just silly since I'm using non-blocking I/O in the first place.... " .....每个InputStream都有一个单独的线程,但是这很愚蠢,因为我首先使用非阻塞I / O ......

It's not silly at all. 这根本不是愚蠢的。 First you need to check whether you can retrieve a SelectableChannel from your InputStream implementation. 首先,您需要检查是否可以从InputStream实现中检索SelectableChannel。 If it does you are lucky and you can just register it with a selector and do as usual. 如果确实如此,你很幸运,你可以用选择器注册它并像往常一样。 But chances are that your InputStream may have a channel that's not a SelectableChannel, in which case "Have a separate thread for each InputStream" is the obvious thing to do and probably the right thing to do. 但是很有可能你的InputStream可能有一个不是SelectableChannel的通道,在这种情况下“为每个InputStream创建一个单独的线程”是显而易见的事情,可能是正确的事情。

Note that there is a similar problem discussed in SO about not able to get a SelectableChannel from an inputstream . 请注意,在SO中讨论的类似问题是无法从输入流获取SelectableChannel Unfortunately you are stuck. 不幸的是你被困住了。

I have a single thread performing all writes 我有一个执行所有写操作的线程

Have you stopped to consider whether that is part of the problem rather than part of the solution? 你有没有停下来考虑这是否是问题的一部分而不是解决方案的一部分?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM