Added line-buffer output for processes. Removed trailing new-line

from output.
This commit is contained in:
nathan 2016-12-11 22:59:16 +01:00
parent 0dcdc137e5
commit 4b89308b69
2 changed files with 126 additions and 84 deletions

View File

@ -85,8 +85,15 @@ class ProcessProxy extends Thread {
while ((readInt = is.read()) != -1) { while ((readInt = is.read()) != -1) {
os.write(readInt); os.write(readInt);
// always flush after a write
os.flush(); // flush the output on new line. (same for both windows '\r\n' and linux '\n')
if (readInt == '\n') {
os.flush();
synchronized (os) {
os.notify();
}
}
} }
} }
} catch (Exception ignore) { } catch (Exception ignore) {

View File

@ -15,8 +15,6 @@
*/ */
package dorkbox.util.process; package dorkbox.util.process;
import dorkbox.util.OS;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.InputStream; import java.io.InputStream;
@ -25,6 +23,8 @@ import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import dorkbox.util.OS;
/** /**
* If you want to save off the output from the process, set a PrintStream to the following: * If you want to save off the output from the process, set a PrintStream to the following:
* <pre> {@code * <pre> {@code
@ -39,6 +39,8 @@ import java.util.List;
public public
class ShellProcessBuilder { class ShellProcessBuilder {
// TODO: Add the ability to get the process PID via java for mac/windows/linux. Linux is avail from jvm, windows needs JNA
private final PrintStream outputStream; private final PrintStream outputStream;
private final PrintStream outputErrorStream; private final PrintStream outputErrorStream;
private final InputStream inputStream; private final InputStream inputStream;
@ -47,14 +49,17 @@ class ShellProcessBuilder {
private String workingDirectory = null; private String workingDirectory = null;
private String executableName = null; private String executableName = null;
private String executableDirectory = null; private String executableDirectory = null;
private Process process = null;
// true if we want to save off (usually for debugging) the initial output from this private Process process = null;
private boolean debugInfo = false; private ProcessProxy writeToProcess_input = null;
private ProcessProxy readFromProcess_output = null;
private ProcessProxy readFromProcess_error = null;
private boolean createReadWriterThreads = false; private boolean createReadWriterThreads = false;
private boolean isShell; private boolean isShell;
private String pipeToNullString = ""; private String pipeToNullString = "";
private List<String> fullCommand;
/** /**
* This will cause the spawned process to pipe it's output to null. * This will cause the spawned process to pipe it's output to null.
@ -65,17 +70,17 @@ class ShellProcessBuilder {
} }
public public
ShellProcessBuilder(PrintStream out) { ShellProcessBuilder(final PrintStream out) {
this(null, out, out); this(null, out, out);
} }
public public
ShellProcessBuilder(InputStream in, PrintStream out) { ShellProcessBuilder(final InputStream in, final PrintStream out) {
this(in, out, out); this(in, out, out);
} }
public public
ShellProcessBuilder(InputStream in, PrintStream out, PrintStream err) { ShellProcessBuilder(final InputStream in, final PrintStream out, final PrintStream err) {
this.inputStream = in; this.inputStream = in;
this.outputStream = out; this.outputStream = out;
this.outputErrorStream = err; this.outputErrorStream = err;
@ -101,20 +106,20 @@ class ShellProcessBuilder {
* When launched from eclipse, the working directory is USUALLY the root of the project folder * When launched from eclipse, the working directory is USUALLY the root of the project folder
*/ */
public final public final
ShellProcessBuilder setWorkingDirectory(String workingDirectory) { ShellProcessBuilder setWorkingDirectory(final String workingDirectory) {
// MUST be absolute path!! // MUST be absolute path!!
this.workingDirectory = new File(workingDirectory).getAbsolutePath(); this.workingDirectory = new File(workingDirectory).getAbsolutePath();
return this; return this;
} }
public final public final
ShellProcessBuilder addArgument(String argument) { ShellProcessBuilder addArgument(final String argument) {
this.arguments.add(argument); this.arguments.add(argument);
return this; return this;
} }
public final public final
ShellProcessBuilder addArguments(String... paths) { ShellProcessBuilder addArguments(final String... paths) {
for (String path : paths) { for (String path : paths) {
this.arguments.add(path); this.arguments.add(path);
} }
@ -122,32 +127,33 @@ class ShellProcessBuilder {
} }
public final public final
ShellProcessBuilder addArguments(List<String> paths) { ShellProcessBuilder addArguments(final List<String> paths) {
this.arguments.addAll(paths); this.arguments.addAll(paths);
return this; return this;
} }
public final public final
ShellProcessBuilder setExecutable(String executableName) { ShellProcessBuilder setExecutable(final String executableName) {
this.executableName = executableName; this.executableName = executableName;
return this; return this;
} }
public public
ShellProcessBuilder setExecutableDirectory(String executableDirectory) { ShellProcessBuilder setExecutableDirectory(final String executableDirectory) {
// MUST be absolute path!! // MUST be absolute path!!
this.executableDirectory = new File(executableDirectory).getAbsolutePath(); this.executableDirectory = new File(executableDirectory).getAbsolutePath();
return this; return this;
} }
/**
* Sends all output data for this process to "null" in a cross platform method
*/
public public
ShellProcessBuilder addDebugInfo() { ShellProcessBuilder pipeOutputToNull() throws IllegalArgumentException {
this.debugInfo = true; if (outputStream != null || outputErrorStream != null) {
return this; throw new IllegalArgumentException("Cannot pipe shell command to 'null' if an output stream is specified");
} }
public
ShellProcessBuilder pipeOutputToNull() {
if (OS.isWindows()) { if (OS.isWindows()) {
// >NUL on windows // >NUL on windows
pipeToNullString = ">NUL"; pipeToNullString = ">NUL";
@ -160,10 +166,30 @@ class ShellProcessBuilder {
return this; return this;
} }
/**
* @return the executable command issued to the shell
*/
public
String getCommand() {
StringBuilder execCommand = new StringBuilder();
Iterator<String> iterator = fullCommand.iterator();
while (iterator.hasNext()) {
String s = iterator.next();
execCommand.append(s);
if (iterator.hasNext()) {
execCommand.append(" ");
}
}
return execCommand.toString();
}
public public
int start() { int start() {
List<String> argumentsList = new ArrayList<String>(); fullCommand = new ArrayList<String>();
// if no executable, then use the command shell // if no executable, then use the command shell
if (this.executableName == null) { if (this.executableName == null) {
@ -173,8 +199,8 @@ class ShellProcessBuilder {
// windows // windows
this.executableName = "cmd"; this.executableName = "cmd";
argumentsList.add(this.executableName); fullCommand.add(this.executableName);
argumentsList.add("/c"); fullCommand.add("/c");
} }
else { else {
// *nix // *nix
@ -185,8 +211,8 @@ class ShellProcessBuilder {
this.executableName = "/bin/sh"; this.executableName = "/bin/sh";
} }
argumentsList.add(this.executableName); fullCommand.add(this.executableName);
argumentsList.add("-c"); fullCommand.add("-c");
} }
} }
else { else {
@ -203,9 +229,9 @@ class ShellProcessBuilder {
this.executableDirectory += File.separator; this.executableDirectory += File.separator;
} }
argumentsList.add(0, this.executableDirectory + this.executableName); fullCommand.add(0, this.executableDirectory + this.executableName);
} else { } else {
argumentsList.add(this.executableName); fullCommand.add(this.executableName);
} }
} }
@ -231,55 +257,30 @@ class ShellProcessBuilder {
} }
} }
argumentsList.add(stringBuilder.toString()); fullCommand.add(stringBuilder.toString());
} else { } else {
for (String arg : this.arguments) { for (String arg : this.arguments) {
argumentsList.add(arg); if (arg.contains(" ")) {
// individual arguments MUST be in their own element in order to be processed properly
// (this is how it works on the command line!)
String[] split = arg.split(" ");
for (String s : split) {
fullCommand.add(s);
}
} else {
fullCommand.add(arg);
}
} }
if (pipeToNull) { if (pipeToNull) {
argumentsList.add(pipeToNullString); fullCommand.add(pipeToNullString);
} }
} }
if (this.debugInfo) { ProcessBuilder processBuilder = new ProcessBuilder(fullCommand);
if (outputErrorStream != null) {
this.outputErrorStream.print("Executing: ");
} else {
System.err.print("Executing: ");
}
Iterator<String> iterator = argumentsList.iterator();
while (iterator.hasNext()) {
String s = iterator.next();
if (outputErrorStream != null) {
this.outputErrorStream.print(s);
} else {
System.err.print(s);
}
if (iterator.hasNext()) {
if (outputErrorStream != null) {
this.outputErrorStream.print(" ");
} else {
System.err.print(" ");
}
}
}
if (outputErrorStream != null) {
this.outputErrorStream.print(OS.LINE_SEPARATOR);
} else {
System.err.print(OS.LINE_SEPARATOR);
}
}
ProcessBuilder processBuilder = new ProcessBuilder(argumentsList);
if (this.workingDirectory != null) { if (this.workingDirectory != null) {
processBuilder.directory(new File(this.workingDirectory)); processBuilder.directory(new File(this.workingDirectory));
} }
@ -315,10 +316,6 @@ class ShellProcessBuilder {
} }
if (this.process != null) { if (this.process != null) {
ProcessProxy writeToProcess_input = null;
ProcessProxy readFromProcess_output = null;
ProcessProxy readFromProcess_error = null;
if (this.outputErrorStream == null && this.outputStream == null) { if (this.outputErrorStream == null && this.outputStream == null) {
if (!pipeToNull) { if (!pipeToNull) {
NullOutputStream nullOutputStream = new NullOutputStream(); NullOutputStream nullOutputStream = new NullOutputStream();
@ -360,24 +357,26 @@ class ShellProcessBuilder {
// the process can be killed in two ways // the process can be killed in two ways
// If not in eclipse, by this shutdown hook. (clicking the red square to terminate a process will not run it's shutdown hooks) // If not in IDE, by this shutdown hook. (clicking the red square to terminate a process will not run it's shutdown hooks)
// Typing "exit" will always terminate the process // Typing "exit" will always terminate the process
Thread hook = new Thread(new Runnable() { Thread hook = new Thread(new Runnable() {
@Override @Override
public public
void run() { void run() {
if (ShellProcessBuilder.this.debugInfo) {
final PrintStream errorStream = ShellProcessBuilder.this.outputErrorStream;
if (errorStream != null) {
errorStream.println("Terminating process: " + ShellProcessBuilder.this.executableName);
}
}
ShellProcessBuilder.this.process.destroy(); ShellProcessBuilder.this.process.destroy();
} }
}); });
hook.setName("ShellProcess Shutdown Hook");
// add a shutdown hook to make sure that we properly terminate our spawned processes. // add a shutdown hook to make sure that we properly terminate our spawned processes.
Runtime.getRuntime() // hook is NOT set to daemon mode, because this is run during shutdown
.addShutdownHook(hook); // add a shutdown hook to make sure that we properly terminate our spawned processes.
try {
Runtime.getRuntime()
.addShutdownHook(hook);
} catch (IllegalStateException ignored) {
// can happen, safe to ignore
}
if (writeToProcess_input != null) { if (writeToProcess_input != null) {
if (createReadWriterThreads) { if (createReadWriterThreads) {
@ -433,8 +432,11 @@ class ShellProcessBuilder {
} }
// remove the shutdown hook now that we've shutdown. // remove the shutdown hook now that we've shutdown.
Runtime.getRuntime() try {
.removeShutdownHook(hook); Runtime.getRuntime().removeShutdownHook(hook);
} catch (IllegalStateException ignored) {
// can happen, safe to ignore
}
return exitValue; return exitValue;
} }
@ -444,12 +446,12 @@ class ShellProcessBuilder {
} }
/** /**
* Converts the baos to a string in a safe way. There might be a trailing newline character at the end of this output. * Converts the baos to a string in a safe way. There will never be a trailing newline character at the end of this output.
* *
* @param byteArrayOutputStream the baos that is used in the {@link ShellProcessBuilder#ShellProcessBuilder(PrintStream)} (or similar * @param byteArrayOutputStream the baos that is used in the {@link ShellProcessBuilder#ShellProcessBuilder(PrintStream)} (or similar
* calls) * calls)
* *
* @return A string representing the output of the process * @return A string representing the output of the process, null if the thread for this was interrupted
*/ */
public static public static
String getOutput(final ByteArrayOutputStream byteArrayOutputStream) { String getOutput(final ByteArrayOutputStream byteArrayOutputStream) {
@ -459,6 +461,39 @@ class ShellProcessBuilder {
byteArrayOutputStream.reset(); byteArrayOutputStream.reset();
} }
// remove trailing newline character(s)
int endIndex = s.lastIndexOf(OS.LINE_SEPARATOR);
if (endIndex > -1) {
return s.substring(0, endIndex);
}
return s;
}
/**
* Converts the baos to a string in a safe way. There will never be a trailing newline character at the end of this output. This will
* block until there is a line of input available.
*
* @param byteArrayOutputStream the baos that is used in the {@link ShellProcessBuilder#ShellProcessBuilder(PrintStream)} (or similar
* calls)
*
* @return A string representing the output of the process, null if the thread for this was interrupted
*/
public String
getOutputLineBuffered(final ByteArrayOutputStream byteArrayOutputStream) {
String s;
synchronized (byteArrayOutputStream) {
try {
byteArrayOutputStream.wait();
} catch (InterruptedException ignored) {
return null;
}
s = byteArrayOutputStream.toString();
byteArrayOutputStream.reset();
}
return s; return s;
} }
} }