Piped I/O stream in java

旧时模样 提交于 2021-02-20 04:08:10

问题


I am not able to understand the purpose of piped i/o streams in java. If I write something using an outputstream, I can anyways read it again using an inputstream.

So what is the need of a pipedstream?


回答1:


Short motivation:

  1. Separation of concerns (always good - in this case it helps split your code into reading and writing part)
  2. Multithreading (previous point lead to "easy" (its never easy :D) parallelization)

Long explanation/example:

Imagine you have "producer-consumer" like application. You have one part of your code that generates output and you want provide it with place to where write data to. For example:

void generateLines(int numberOfLines, OutputStream target);

There is only single responsibility of this component i.e. it "produces" lines and it doesnt know where the lines are written to. You just call it like generateLines(5, System.out) or generateLines(100, fileOutputStream). But imagine you want to assemble "pipeline" i.e. you do not want to store/print the data but instead you want to pass them to another component "consumer".

You could have component with signature like that:

int countLines(InputStream source);

Only thing this does is it reads data from InputStream which is provided externally.

Now thanks to "Piped" streams you can glue those two component together by creating "pipe" like:

PipedOutputStream pos = new PipedOutputStream();
PipedInputStream pis = new PipedInputStream(pos, 1024);

Then you can run two threads, one that generates data and another that consumes them. Pipe api is really convenient, reading/writing is blocking (see javadoc for more details) so the usage is really simple/convenient.

// Withing main thread start generating of x lines into pos OutputStream
generateLines(50000, pos);
// Spawn new thread within which we consume pipe from its InputStream end
Executors.newCachedThreadPool().submit(() -> {
   pis.read();
   // The rest of "reading-loop" omitted for sake of simplicity...
});

Of course this example is dummy but one can easily imagine use cases like "generating huge xml" (produce) into writable stream and "compressing/validating" (consume) it and so on.

Maybe there is better example but personally I use those pipes for producer-consumer jobs as described before.




回答2:


Pipes in Java IO provides the ability for two threads running in the same JVM to communicate. As such pipes are a common source or destination of data.

A PipedInputStream should be connected to a PipedOutputStream. The data written to the PipedOutputStream by one thread, can thus be read from the connected PipedInputStream by another thread

Normal Java IO does not support streaming to the same stream from multiple threads.




回答3:


I think its supposed to be for reading and writing in a multi-threaded application. Where you can write in a thread and read in the other one, without flushing the data.



来源:https://stackoverflow.com/questions/24348320/piped-i-o-stream-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!