0

Consider the InputStream with buffer,for example AudioInputStream (i.e.available() return >=0) The inputstream is reading a changing file (a file which is kept downloading from internet and the undownloaded part is packed with zeros).

  • When it read to the part that are not downloaded, wait() will be called
  • When the part is downloaded, notify() will be called

However, when it is waked up, how to make sure it drops the buffer(the useless zeros) and read again the file?

Thank in advance

Bear
  • 5,138
  • 5
  • 50
  • 80
  • Can you give us more info on AudioInputStream? – dash1e Apr 07 '12 at 01:52
  • @dash1e [`AudioInputStream`](http://docs.oracle.com/javase/7/docs/api/javax/sound/sampled/AudioInputStream.html) – Jeffrey Apr 07 '12 at 01:53
  • AudioInputStream is for reading sound file and it has buffer(i.e. available() return >=0) http://docs.oracle.com/javase/6/docs/api/javax/sound/sampled/AudioInputStream.html Actually, I try to write a player that can play a downloading sound file – Bear Apr 07 '12 at 01:54
  • For the downloading part, I first filled the file with zero to make sure the file has the same fileSize with the original file. Then use RandomAccessFile to write it (Multiple write). It stores the byte position that downloaded – Bear Apr 07 '12 at 01:56
  • You want always store your file on disk for subsequent readings? – dash1e Apr 07 '12 at 02:07
  • Because it downloads the file from multiple peer. I can't think of other solution than writing it to a file – Bear Apr 07 '12 at 02:35

1 Answers1

0

I assume you're creating your AudioInputStream with a FileInputStream. What you should be doing is this:

PipedInputStream pipeIn = new PipedInputStream();
PipedOutputStream pipeOut = new PipedOutputStream(pipeIn);
AudioInpuStream ais = new AudioInputStream(pipeIn, ..., ...);

Now as you download more of the file all you have to do is write it to pipeOut.

Jeffrey
  • 44,417
  • 8
  • 90
  • 141