How to write an array of bytes in an OutputStream for a process builder (Java)

byte[] bytes = value.getBytes(); Process q = new ProcessBuilder("process","arg1", "arg2").start(); q.getOutputStream().write(bytes); q.getOutputStream().flush(); System.out.println(q.getInputStream().available()); 

I am trying to transfer the contents of a file to an executable and grab the output, but the output (InputStream) is always empty. I can capture the output if I specify the location of the file, but not with streaming input.

How can I overcome this?

+4
source share
4 answers

Try wrapping your streams with BufferedInputStream() and BufferedOutputStream() :

http://download.oracle.com/javase/6/docs/api/java/lang/Process.html#getOutputStream%28%29

Implementation Note. Good idea for buffering the output stream.

Implementation Note. Good idea for buffering the input stream.

Even with buffered streams, it is still possible to fill the buffer, if you are dealing with large amounts of data, you can handle this by running a separate stream to read from q.getInputStream() , so you can still read from the process while writing into the process.

+2
source

Perhaps the program you are running is just starting out when it discovers the end of its input. This is usually done by waiting for the EOF character (end of file). You can send this by closing the output for the process:

 q.getOutputStream().write(bytes); q.getOutputStream().close(); 

Try this along with the waiting process.

+1
source

I don’t know if something else might be wrong here, but another process (“process”) doesn’t even have time to answer, you don’t wait for it (the method is not available () does not block). To try this, you can first insert sleep (2000) after flush (), and if that works, you should switch to the q.getInputStream () request. Available () several times with short pauses between them.

0
source

I think you need to wait until the process is over. I implemented something like this as follows:

 public class ProcessReader { private static final int PROCESS_LOOP_SLEEP_MILLIS = 100; private String result; public ProcessReader(Process process) { BufferedReader resultReader = new BufferedReader(new InputStreamReader(process.getInputStream())); StringBuilder resultOutput = new StringBuilder(); try { while (!checkProcessTerminated(process, resultReader, resultOutput)) { } } catch (Exception ex1) { throw new RuntimeException(ex1); } result = resultOutput.toString(); } public String getResult(){ return result; } private boolean checkProcessTerminated(Process process, BufferedReader resultReader, StringBuilder resultOutput) throws Exception { try { int exit = process.exitValue(); return true; } catch (IllegalThreadStateException ex) { Thread.sleep(PROCESS_LOOP_SLEEP_MILLIS); } finally { while (resultReader.ready()) { String out = resultReader.readLine(); resultOutput.append(out).append("\n"); } } return false; } 

}

I just deleted some specific code that you don't need, but it should work, try it. Relations

0
source

Source: https://habr.com/ru/post/1341325/


All Articles