1 package org.simantics.gnuplot;
3 import java.io.Closeable;
4 import java.io.IOException;
5 import java.io.PrintStream;
6 import java.nio.file.Files;
7 import java.nio.file.Path;
10 * @author Tuukka Lehtonen
13 public class GnuplotSession implements Closeable {
15 private Process process;
16 private PrintStream out;
18 private Thread stdoutDumper;
19 private Thread stderrDumper;
21 GnuplotSession(Path workingDirectory, Path stdout, Path stderr, Process process) {
22 this.process = process;
23 this.out = new PrintStream(process.getOutputStream());
24 stdoutDumper = new Thread(new InputStreamToFileCopier(process.getInputStream(), stdout));
25 stderrDumper = new Thread(new InputStreamToFileCopier(process.getErrorStream(), stderr));
30 private void assertAlive() {
32 throw new IllegalStateException("session has been closed already");
35 public void evaluate(String commands) throws IOException {
37 out.println(commands);
40 public void evaluateFile(Path file) throws IOException {
42 Files.lines(file).forEachOrdered(l -> {
45 } catch (IOException e) {
46 throw new RuntimeException(e);
49 } catch (RuntimeException e) {
50 Throwable t = e.getCause();
51 if (t instanceof IOException)
52 throw (IOException) t;
58 public void close() throws IOException {
66 // Inform the process that its stdin has closed, i.e. is done.
69 // Wait for the outputs to be closed by the process itself.
73 //System.out.println("gnuplot process ended");
74 } catch (InterruptedException e) {
75 throw new IOException(e);