Non-blocking PipedStreams?
PipedInputStream
and PipedOutputStream
are used for inter-thread data transfers. "Data is read from a PipedInputStream
object by one thread and data is written to the corresponding PipedOutputStream
by some other thread".
So far, so good. One potential benefit I see from this paradigm is that a sporadic producer (OutputStream
) does not need to be slowed down by a slow consumer (InputStream
). In other words, in times when the OutputStream
suddenly produces a great amount of data that the consumer is not able to consume right away, the OutputStream
does not need to wait for the produced data to be fully consumed before getting on with its life.
Question 1: Is my understanding correct? Is this one of the benefits of this PipedStreams construct?
If the answer开发者_高级运维 to Question 1 is yes, then:
This works well when the buffer is not full, but when the buffer is full, in JDK6's implementation, the PipedOutputStream blocks and waits for more space to free up. This beats the purpose that I mentioned above, doesn't it?
The issue seems to be very easy to solve by changing the behavior in cases of full bufferes from waiting for new space to allocating a bigger buffer. I can probably implement one in a couple of minutes, but I'd hate to reinvent the wheel if something like that is already available out there. I googled around quite a bit, but couldn't find any however, which is quite surprising to me.
Can someone point me to the right direction? Or point out where I'm wrong in my thinking?
Thanks
PipedOutputStream isn't really intended to buffer results in that way (the buffering is more of a side effect in its implementation). As the Javadoc says:
"The piped input stream contains a buffer, decoupling read operations from write operations, within limits."
For buffering in the way you describe I would tend to use a http://download.oracle.com/javase/6/docs/api/java/util/concurrent/LinkedBlockingQueue.html.
I don't think PipedOutputStream has any guarentees about buffering the output until the PipedInputStream is ready. So, the answer is no, although in practice you may notice some buffering.
EDIT: Try searching for the term "circular buffer". Don't know of one off hand, but that's a common name for this type of case.
精彩评论