javastreamnioforkfilestreams

Forking Streams in java


First off, I did do my research and completely understand that java explicitly says this should not be done. referencing this question here Can you split a stream into two streams?

that out of the way, im building an application where i would like to both save a file to a place using streams (internet in, local filesystem out), and display that file (usually an image) to a JFrame. The system I am currently using simply chains them together, from the internet in to the file out. then from the file out to the local object, that is sent via a listener to the JFrame. This works but feels very inefficient. So in conclusion my question is, Is there a more efficient way of in essence forking a stream in such a way?


Solution

  • a) Those Java8 Streams in the linked answer have nothing to do with Input/Outputstreams

    b) it's probably not that inefficient as you need the whole bitmap for the image to display it, so starting while it is half-downloaded is not that useful

    c) You can write to two different places at once with a TeeOutputStream.