Java:共享输入流和输出流 [英] Java: InputStreams and OutputStreams being shared
问题描述
我可以共享 InputStream
或 OutputStream
吗?
例如,假设我首先拥有:
DataInputStreaminput = new DataInputStream(socket.getInputStream()));
... 传入
是对象变量。后来我暂时做:
BufferedReader dataReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
我了解该流是具体的,无论从何处读取流都将消耗其输入完成...但是完成上述操作后,我仍可以同时访问传入
和 dataReader
还是 InputStream
刚刚连接到一个对象,因此一旦我声明 dataReader $ c,
传入
就会丢失输入$ c>?我知道,如果我关闭 dataReader
,那么我也将关闭套接字,我将避免这样做,但是我想知道是否需要回收 InputStream
以某种方式将传入
转移到 dataReader
之后?我是否必须这样做:
incoming = new DataInputStream(socket.getInputStream());
再次完成整个操作之后?
好,我自己解决了这个问题。有趣的链接:
http://www.coderanch.com/t/276168//java/InputStream-multiple-Readers
基本上... InputStream
可以连接到多个读取并使用它的对象。但是, BufferedReader
会预先读取,因此当涉及其中一个时,从例如<$>切换时实现某种信号可能是一个好主意。 c $ c> BufferedReader 到 DataInputStream
(即您要使用 DataInputStream
突然处理 InputStream
而不是 BufferedReader
)。因此,一旦知道所有数据已发送给 BufferedReader
处理,就停止向 InputStream
发送数据。 。此后,我等待其他部分使用 BufferedReader
处理它。然后它发送一个信号,表明它已准备好进行新输入。发送部分应该一直处于阻塞状态,直到接收到信号输入,然后它才能再次开始发送数据。如果在此之后我不使用 BufferedReader
,它将没有机会缓冲所有输入并从 DataInputStream
,一切都很好:)但是要小心,从 BufferedReader
进行的一次读操作,您将回到相同的情况。
Can I share an InputStream
or OutputStream
?
For example, let's say I first have:
DataInputStream incoming = new DataInputStream(socket.getInputStream()));
...incoming
being an object variable. Later on I temporarily do:
BufferedReader dataReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
I understand that the stream is concrete and reading from it will consume its input, no matter from where it's done... But after doing the above, can I still access both incoming
and dataReader
simultaneously or is the InputStream
just connected to ONE object and therefore incoming
loses its input once I declare dataReader
? I understand that if I close the dataReader
then I will close the socket as well and I will refrain from this but I'm wondering whether I need to "reclaim" the InputStream
somehow to incoming
after having "transferred" it to dataReader
? Do I have to do:
incoming = new DataInputStream(socket.getInputStream());
again after this whole operation?
Ok, I solved this myself.. interesting links:
http://www.coderanch.com/t/276168//java/InputStream-multiple-Readers
Multiple readers for InputStream in Java
Basically... the InputStream
can be connected to multiple objects reading from it and consuming it. However, a BufferedReader
reads ahead, so when involving one of those, it might be a good idea to implement some sort of signal when you're switching from for example a BufferedReader
to a DataInputStream
(that is you want to use the DataInputStream
to process the InputStream
all of a sudden instead of the BufferedReader
). Therefore I stop sending data to the InputStream
once I know that all data has been sent that is for the BufferedReader
to handle. After this, I wait for the other part to process what it should with the BufferedReader
. It then sends a signal to show that it's ready for new input. The sending part should be blocking until it receives the signal input and then it can start sending data again. If I don't use the BufferedReader
after this point, it won't have a chance to buffer up all the input and "steal" it from the DataInputStream
and everything works very well :) But be careful, one read operation from the BufferedReader
and you will be back in the same situation... Good to know!
这篇关于Java:共享输入流和输出流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!