Input and Output Stream Pipe in Java

后端 未结 7 1631
闹比i
闹比i 2020-12-17 21:18

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 inherit

7条回答
  •  有刺的猬
    2020-12-17 21:57

    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?

提交回复
热议问题