Broken pipe exception while feeding a console's inputstream from a non-finished file
I'm into streaming mp3 from a Text To Speech enging (TTS). The challange is that the TTS outputs raw PCM and it does that in real time. 10 seconds of speech takes 10 seconds to generate. To minimize the time users have to wait for the response I want to start sending them the sound as soon as the process has started.
I'm doing the TTS call in a separate thread. The thread synchs with the caller thread when it has started to produce sound. Then I call Lame (I use Lame to do MP3 conversion) via the console. The Lame command takes takes standard input as its source of data.
When I know that the TTS process has started to produce PCM which it writes to a file I start to read that file (while it is still being written to by the TTS thread). The bytes read from that file I want to feed into the consoles standard input so Lame can do the conversion.
It is when trying to write the bytes into that stream I get the java.io.IOException: Broken pipe exception (the row with below)
Here is the code-snippet. Before this the TTS thread has been started and synched with this thread at a point telling that it has started the conversion.
Any hints on how this should be done?
String command = "/bin/bash -c \"/usr/bin/lame -r -x -m m -b 32 -s 22.05 - - > output.mp3\" ";
log.info("COMMAND: "+command );
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(command);
bouts=new BufferedOutputStream(pr.getOutputStream());//will use this stream to send data to Lame
File tmpFile = new File(tmpOutputFileNamePath);// this is the file into which data is being written from the TTS thread
log.info("File does not exist yet... going to sleep" );
bfins = new BufferedInputStream(new FileInputStream(tmpFile));
// Read chunks of bytes and write them to the Lame input stream
while(ttsThreadObj.isAlive()) // TODO might miss some data when the thread has finished
log.info(noOfBytesAvailable +" bytes available" );
bouts.write(b); //Broken Pipe Exception!!! here
log.info("Nothing available..." );