private Path executable;
- public Gnuplot(Path executable) {
+ public Gnuplot(Path executable) throws IOException {
+ if (!Files.exists(executable))
+ throw new IOException("Provided gnuplot executable does not exist: " + executable);
+ if (!Files.isExecutable(executable))
+ throw new IOException("Provided gnuplot executable is not marked executable: " + executable);
this.executable = executable;
}
+ @Override
+ public String toString() {
+ return executable.toString();
+ }
+
public static Gnuplot detect() throws IOException {
try {
return new Gnuplot(findGnuplotPath());
}
private void execute0(Path workingDirectory, Path stdout, Path stderr, String... cmdline) throws IOException {
- Process process = new ProcessBuilder(cmdline)
- .directory(workingDirectory.toFile())
- .start();
+ ProcessBuilder builder = new ProcessBuilder(cmdline)
+ .directory(workingDirectory.toFile());
+
+ if (stdout != null)
+ builder.redirectOutput(stdout.toFile());
+ if (stderr != null)
+ builder.redirectError(stderr.toFile());
+
+ Process process = builder.start();
+ Thread stdoutDumper = stdout == null ? new Thread(new InputStreamToFileCopier(process.getInputStream(), null)) : null;
+ Thread stderrDumper = stderr == null ? new Thread(new InputStreamToFileCopier(process.getErrorStream(), null)) : null;
+ if (stdoutDumper != null)
+ stdoutDumper.start();
+ if (stderrDumper != null)
+ stderrDumper.start();
+
process.getOutputStream().close();
- Thread stdoutDumper = new Thread(new InputStreamToFileCopier(process.getInputStream(), stdout));
- Thread stderrDumper = new Thread(new InputStreamToFileCopier(process.getErrorStream(), stderr));
- stdoutDumper.start();
- stderrDumper.start();
try {
- stdoutDumper.join();
- stderrDumper.join();
+ if (stdoutDumper != null)
+ stdoutDumper.join();
+ if (stderrDumper != null)
+ stderrDumper.join();
process.waitFor();
} catch (InterruptedException e) {
throw new IOException(e);
}
}
+ public void execute(Path workingDirectory, Path stdout, Path stderr, boolean redirectStderr, InputStream input, String charset) throws IOException {
+ try (GnuplotSession session = newSession(workingDirectory, stdout, stderr, redirectStderr)) {
+ session.evaluateStream(input, charset);
+ }
+ }
+
public void execute(Path workingDirectory, Path stdout, Path stderr, Path script) throws IOException {
execute0(workingDirectory, stdout, stderr, executable.toString(), "-c", script.toString());
}
execute0(workingDirectory, stdout, stderr, executable.toString(), "-e", e.toString());
}
- public GnuplotSession newSession(Path workingDirectory, Path stdout, Path stderr) throws IOException {
- Process process = new ProcessBuilder(executable.toString())
- .directory(workingDirectory.toFile())
- .start();
- return new GnuplotSession(workingDirectory, stdout, stderr, process);
+ private ProcessBuilder buildSessionProcess(Path workingDirectory, Path stdout, Path stderr, boolean redirectErrorStream) {
+ ProcessBuilder builder = new ProcessBuilder(executable.toString())
+ .directory(workingDirectory != null ? workingDirectory.toFile() : null)
+ .redirectErrorStream(redirectErrorStream);
+ if (stdout != null)
+ builder.redirectOutput(stdout.toFile());
+ if (stderr != null)
+ builder.redirectError(stderr.toFile());
+ return builder;
+ }
+
+ public GnuplotSession newSession(Path workingDirectory, Path stdout) throws IOException {
+ return newSession(workingDirectory, stdout, null, true);
+ }
+
+ public GnuplotSession newSession(Path workingDirectory, Path stdout, Path stderr, boolean redirectStderr) throws IOException {
+ return new GnuplotSession(workingDirectory, stdout == null, stderr == null,
+ buildSessionProcess(workingDirectory, stdout, stderr, redirectStderr).start());
}
static enum OSType {