I'm trying to run an asynchronous process and getting its inputstream (if there is).
This is my code:
CommandCall commandCall = new CommandCall(commands);
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<Integer> task = executor.submit(commandCall);
and this is the process run Task
public class CommandCall implements Callable<Integer> {
private byte[] output;
private int retval=-1;
private String[] commands=null;
Process process=null;
public CommandCall(String[] commands) throws Exception {
this.commands=commands;
this.output=null;
}
public void destroy() {
try {
if(process!=null) process.destroy();
}catch(Exception e){}
}
public byte[] getByteArray() {
return output;
}
public int getRetval() {
return retval;
}
@Override
public Integer call() throws Exception {
try{
process = new ProcessBuilder(commands).start();
// here i must read the process input and store it to this.output
// this must be a non lockable read because the process can be without any output
retval= process.waitFor();
}finally{
try{
if(bos!=null) bos.close();
}catch(Exception e){}
}
return retval;
}
}
I can't get the process output, please mind 2 very important thing:
- Process must be async because I need to manage a timeout
- Process's InputStream can be optional and must not lock the thread waiting for content: there can be a process without any output.
UPDATE
I'm trying this version... seems to work but I don't know if it is strong enought.
@Override
public Integer call() throws Exception {
InputStream is=null;
try{
process = new ProcessBuilder(commands).start();
is=process.getInputStream();
int len;
int size = 1024;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[size];
while ((len = is.read(buf, 0, size)) != -1)
bos.write(buf, 0, len);
output = bos.toByteArray();
retval= process.waitFor();
}finally{
try{
if(is!=null) is.close();
}catch(Exception e){}
}
return retval;
}