I have a wrapper for a ProcessBuilder
so that any class needing to run a process can use it.
The class passes the process and the arguments and will return the result.
The result would be the outputstream of the Process
or the errorstream I guess.
My question is how is the stream to be passed among threads? I mean I am doing something like the following:
String line=null;
try {
while ( (line = br.readLine()) != null) {
if (pw != null)
pw.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
if (pw != null)
pw.flush();
I guess I should flush
but not close the steam right? Or should I be doing it differently?
You can write the data to a ByteArrayOutputStream and pass that to the caller. Once you are done writing to the BAOS close the inputstream and pass the outputstream to the caller. It is the caller's responsibility to close / flush the BAOS and release any memory associated with it.