8142927: Feed some text to STDIN in ProcessTools.executeProcess()
Reviewed-by: rriggs
This commit is contained in:
parent
0793aa183c
commit
4d01763cc8
@ -38,7 +38,7 @@ public final class OutputAnalyzer {
|
||||
private final OutputBuffer output;
|
||||
private final String stdout;
|
||||
private final String stderr;
|
||||
private final int exitValue;
|
||||
private final int exitValue; // useless now. output contains exit value.
|
||||
|
||||
/**
|
||||
* Create an OutputAnalyzer, a utility class for verifying output and exit
|
||||
|
@ -365,11 +365,31 @@ public final class ProcessTools {
|
||||
* @return The {@linkplain OutputAnalyzer} instance wrapping the process.
|
||||
*/
|
||||
public static OutputAnalyzer executeProcess(ProcessBuilder pb) throws Exception {
|
||||
return executeProcess(pb, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes a process, pipe some text into its STDIN, waits for it
|
||||
* to finish and returns the process output. The process will have exited
|
||||
* before this method returns.
|
||||
* @param pb The ProcessBuilder to execute.
|
||||
* @param input The text to pipe into STDIN. Can be null.
|
||||
* @return The {@linkplain OutputAnalyzer} instance wrapping the process.
|
||||
*/
|
||||
public static OutputAnalyzer executeProcess(ProcessBuilder pb, String input)
|
||||
throws Exception {
|
||||
OutputAnalyzer output = null;
|
||||
Process p = null;
|
||||
boolean failed = false;
|
||||
try {
|
||||
p = pb.start();
|
||||
if (input != null) {
|
||||
try (OutputStream os = p.getOutputStream();
|
||||
PrintStream ps = new PrintStream(os)) {
|
||||
ps.print(input);
|
||||
ps.flush();
|
||||
}
|
||||
}
|
||||
output = new OutputAnalyzer(p);
|
||||
p.waitFor();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user