5

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?

1
  • 2
    Maybe ... "Favor 'object composition' over 'class inheritance'." (Gang of Four 1995:20) Commented Nov 17, 2011 at 14:01

4 Answers 4

3

java.io.PipedInputStream

java.io.PipedOutputStream

Sign up to request clarification or add additional context in comments.

1 Comment

The Javadoc explicitely states "Attempting to use both objects from a single thread is not recommended, as it may deadlock the thread". What happens is that PipedInputStream.read() will block the calling thread until the associated PipedOutputStream.write() is called in another thread.
1

check Pipe class for a ready made implementation

or the PipedInputStream and PipedOutputStream pair

Comments

0

You will not have an instance to use as both input- and output- stream. Instead, you can have a Pipe object that encapsulates (i.e. composition) an InputStream for reading and an OutputStream for writing.

Pretty much like you have System.in and System.out in the standard API.

Comments

-2

This is a good case where you don't need multiple inheritance, and the fact that you asked the question that way concerns me a bit.

In this case you would have a class that had an input stream, and an output stream. No need to extend anything or use an interface.

After changing your code to do this, read this article about composition vs inheritance: http://www.artima.com/lejava/articles/designprinciples4.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.