Search code examples
javastreamforknio

Is it possible to Fork (clone) a stream in java.nio (or regular java io)?


Is it possible (using the standard java.nio api, without major hacking or proxying or facading) to take an output stream, and clone it, so that every write to the stream gets copied to two, independent output streams from one ?


Solution

  • No. You would have to write an implementation of WritableByteChannel that delegated to both (all) target channels. Not too difficult, only one constructor and three methods to write.

    EDIT You can do the same thing for streams by writing a FilterOutputStream derived class. Again very easy.