This is a follow up to this question. The answer suggested there is
to copy the Process out, err, and input streams to the System versions
The problem is that IOUtil.copy()
is running while there is data in the InputStream to be copied. Since your process only produces data from time to time, IOUtil.copy()
exits as it thinks there is no data to be copied.
Just copy data by hand and use a boolean to stop the thread form outside:
byte[] buf = new byte[1024];
int len;
while (threadRunning) { // threadRunning is a boolean set outside of your thread
if((len = input.read(buf)) > 0){
output.write(buf, 0, len);
}
}
This reads in chunks as many bytes as there are available on inputStream and copies all of them to output. Internally InputStream puts thread so wait()
and then wakes it when data is available.
So it's as efficient as you can have it in this situation.