views:

170

answers:

1

I'm about to load an online content (say an audio file). If I just open a connection to the remote file (for example by use of new URL().openStream()) and pass the remote InputStream to the audio player, it reads gradually from the network. If audio player library do not ask InputStream for more data, it reads nothing from the network, and then when the library claims more, it reads again.

My problem is that I want to start caching the next online audio file, as soon as the loading process of the first one is finished. Using a normal InputStream, reading is complete as the audio playing is done, which is not good since I want it read faster if network bandwidth allows, so that caching the next audio file. My audio files are smaller that 3mb so can be safely cached to the memory. I only prefetch a single file, so no problem with memory leak.

Is there any type of caching InputStream, which also allows concurrent read? I can then run a thread and cache the audio faster than the audio player consumes the data.

+2  A: 

I would recommend that you create a second thread to perform the downloading. Download files as quickly as possible in this thread and push the data over to your main playback thread.

Dark Falcon
Well, this is what I have in mind, but I should write my own ConcurrentInputStream impl. I asked if there is already an implementation out there.
Mohsen
commons-io has a DemuxInputStream but I'm not sure if it's helpful.
Mohsen