In most UML class diagrams of byte streams that I managed to find, it is drawn that BufferedOutputStream and DataOutputStream are descendants of FilterOutputStream , but at the same time both of them are also used for FileOutputStream and other classes that directly implement the OutputStream interface.

The question is: do I understand correctly that each class directly implementing the OutputStream has its own BufferedOutputStream and DataOutputStream or at least can be wrapped in them ... Or does it somehow work differently?

Please help me figure it out.

    1 answer 1

    that each class directly implements an OutputStream

    Interfaces are implemented, and the OutputStream is a class, not an interface, therefore, it is impossible to implement an OutputStream . Other classes may inherit from the (abstract) OutputStream class.

    The classes BufferedOutputStream and DataOutputStream have constructors:

     BufferedOutputStream(OutputStream out) 

    and

     DataOutputStream(OutputStream out) 

    therefore, any classes that have an OutputStream class in the inheritance hierarchy can be wrapped in a BufferedOutputStream and a DataOutputStream .

    FilterOutputStream (and FilterInputStream ) is used to create a layer between the streams, with which, for example, you can manipulate data . With it, you can also implement a thread that will not close when the external thread is closed.

    The FilterOutputStream and FilterInputStream streams are examples of the Decorator design pattern.

    • Yes, pardon, of course, OutputStream is a class ... All the same, this is not clear to me: if you go to the docks, there BufferedOutputStream, for example, is a successor of FilterOutputStream, but why him and not some other? And most importantly, if the BufferedOutputStream accepts an OutputStream in its constructor, then why did it even end up in this hierarchy? Why don't they have the same “dependency” as String and StringBilder for example? Not what but the argument in the constructor ... What's in this FilterOutputStream is so special what it does ..? - Pavel
    • one
      @Pavel, Updated the answer. - post_zeew
    • Thank! I think I understood. FilterOutputStream is an abstract class that BufferedOutputStream and DataOutputStream could be adapters for the upstream ... It seems so. - Pavel
    • one
      FilterOutputStream , FilterOutputStream is not an abstract class, but otherwise, roughly speaking, right. - post_zeew
    • Thanks for the help! Without her, I would be completely screwed up tomorrow at the competition ... - Pavel