Input and Output Stream Pipe in Java Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Input and Output Stream Pipe in Java without wasting too much if your time.

The question is published on by Tutorial Guruji team.

Does anyone have any good suggestions for creating a Pipe object in Java which is both an InputStream and and OutputStream since Java does not have multiple inheritance and both of the streams are abstract classes instead of interfaces?

The underlying need is to have a single object that can be passed to things which need either an InputStream or an OutputStream to pipe output from one thread to input for another.

Answer

It seems the point of this question is being missed. If I understand you correctly, you want an object that functions like an InputStream in one thread, and an OutputStream in another to create a means of communicating between the two threads.

Perhaps one answer is to use composition instead of inheritance (which is recommended practice anyway). Create a Pipe which contains a PipedInputStream and a PipedOutputStream connected to each other, with getInputStream() and getOutputStream() methods.

You can’t directly pass the Pipe object to something needing a stream, but you can pass the return value of it’s get methods to do it.

Does that work for you?

We are here to answer your question about Input and Output Stream Pipe in Java - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji