tags:

views:

73

answers:

3

Hmmm. I'm trying to write a class that accepts bytes, and would like to implement a well-known interface for this purpose.

java.io.OutputStream is an abstract class, not an interface (why???) and that makes me nervous, as I don't know what the consequences are of extending it. If there are no consequences, it should have been an interface. Otherwise it makes me think that it defines equals() and hashCode() or maybe one of the Serializable-related behaviors, and there's something I should know about before trying to extend it. Anyway if I do extend it, that means I've used up my 1 superclass and can't extend something else that's more vital to my application.

java.lang.Appendable is an interface that does what I want, but for characters, not bytes.

java.nio.WritableByteChannel is sort of what I want, and I might use it, but it only accepts ByteBuffers as inputs, not byte[] arrays.

Any other suggestions/advice? (p.s. is "input-output" the best tag for I/O questions?)

+1  A: 

java.io.DataOutput might work for you, although it has many more methods than you're asking for.

skaffman
thanks !
Jason S
+1  A: 

Why do you want to use a well-known interface?

There is no harm in creating your own interface for your purpose.

Besides, I wouldn't call Appendable or WritableByteChannel 'Well-known'.

jjnguy
Why? Because using a standard interface can reduce coupling between your application components.
skaffman
How would a standard interface reduce coupling more than one of his own?
jjnguy
Because a standard interface may already be present in both components, and therefore wouldn't require a compile-time dependency between the two.
skaffman
The idea of a 'standard' vs. 'non-standard' interface is not in Java. I don't know what you mean by that.
jjnguy
java.io.DataOutput is standard because it's part of the standard API.
skaffman
+1  A: 

There should be no problems extending java.io.OutputStream. It is designed to be used as a base class. If you are worried, take a look at the source code for OutputStream.

My only question is whether the OutputStream API matches your application requirements, or whether a different API might be more suitable. Nobody will complain if you design your own interface that is more closely aligned to your application requirements.

Stephen C