1

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?

1 Answer 1

2

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.

5
  • a) I was making reference specifically to the answer a few down, quoting the java docs explaining how they don't like forking b) agreed, however my concern was that after you read the stream, its in ram for a moment, written to a file, forgotten, then reacquired from the file. the inefficiency lying in the reacquiring of the data as opposed to loading the image as a byte array (or something of the sort), then opening two threads, one for saving, and one for displaying. c) i will continue my research looking into that, thank you Commented Nov 18, 2014 at 4:33
  • a) well, those Streams are unrelated to I/O streams. b) the file should not be in memory entirely while downloading, you can download and write to disk in small blocks. But you do get the disk cache working for you when reading the file in again.
    – Thilo
    Commented Nov 18, 2014 at 4:37
  • a) please, excuse my ignorance then. b) ahh, I didn't think about the caching at all! assuming ram wasn't an issue at all, and that we were working with say, 600 files simultaneously, would loading each file completely into ram before forking out practically any faster? as well, in order for TeeOutputStream to work, i would require a class that has both an input and output stream associated with it; something that simply holds everything output to it as the input for later. At the cause of ImageIO.read requiring an input stream. Commented Nov 18, 2014 at 4:47
  • You may be overthinking this. Do you actually experience performance problems with your approach? The slowest part will be downloading the image from the network when the local copy is not yet there. After that it should be quite fast.
    – Thilo
    Commented Nov 18, 2014 at 4:49
  • I may be, to be honest I do tend to do that. On occasion I see a bit of lag, but that's probably on account of the... well, lots of threads running network connections. Commented Nov 18, 2014 at 4:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.