I'm displaying streaming content using a file to buffer it (because it could be large enough to keep it in RAM). I have two threads: the first downloads a file from server and writes it to the local storage, and the second reads that file and displays the content.

The problem is, when the second thread reaches the end of the file, EOFException is thrown from DataInputStream.readFully() method. Is there any way to make it wait while the first thread writes enough data instead of throwing exception?

有帮助吗?

解决方案

Synchronize the threads. There's one possible approach.

其他提示

Check for EOF before you read from the file. Or put a try ... catch(EOFException ex) around the read statement.

Have you tried using InputStream's available() method to get the number of bytes available and then using DataInputStream's read(byte[] b) with a byte[] the length of the number of bytes returned by available? I haven't actually tried this but it seems like it might work.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top