im trying to stream a File via the Java library Restlet. But the file gets written to while it been streamed. Here it how it should work.
I create a video and a audio file, then I combine the two files to one, this step takes a fair amount of time. So while the new file is creating I want to stream the file to a Browser that I can watch the Video without waiting 10 minutes.
At the moment I can read the File chunk wise with a FileInputStream, but i have no idea how to serve the file to a browser. Any ideas?
Is it even possible to serve a dynamic file with Restlet?
Thanks in advance, and sorry for my bad English ^^
ZimTis
[UPDATE]
I was able to playback a mp3 File, while its being created, thanks to Jerome Louvel :
public class RestletStreamTest extends ServerResource {
private InputRepresentation inputRepresentation;
public FileInputStream fis;
@Get
public InputRepresentation readFile() throws IOException {
final File f = new File("/path/to/tile.mp3");
fis = new FileInputStream(f);
inputRepresentation = new InputRepresentation(new InputStream() {
private boolean waited = false;
@Override
public int read() throws IOException {
waited = false;
// read the next byte of the FileInputStream, when reaching the
// end of the file, wait for 2 seconds and try again, in case
// the file was not completely created yet
while (true) {
byte[] b = new byte[1];
if (fis.read(b, 0, 1) > 0) {
return b[0] + 256;
} else {
if (waited) {
return -1;
} else {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
waited = true;
}
}
}
}
}, MediaType.AUDIO_MPEG);
return inputRepresentation;
}
}
It is a bit blunt, but works and will be refined later. When I change the code to try streaming a video, the player reads all bytes of the video, then starting to play and read all bytes again. When I hit the play button after the video is finished, nothing happened. Restlet throws a timeout and then the video starts playing again. I try a .mp4 and a .flv file, but always with the same result.
I'm not sure if its a problem with Restlet or the palyer. I use the VLC player in Firefox, and tried the standard html5 player in Chrome. But the Chrome player didn't even start playing.
Did I missed something? Or is it just a problem with the player?
I suggest that you try returning either an InputRepresentation instance wrapping your FileInputStream, or directly a FileRepresentation wrapping the newly created file.