X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.gnuplot%2Fsrc%2Forg%2Fsimantics%2Fgnuplot%2FGnuplotSession.java;h=6bc3e5bafa3f66f3472f5713fccfd780a52bc050;hb=3b7532c07fda3df5b54f8c32372507b0b8728aad;hp=32534e0cf8baff32b0c4e4d9640966fdc50becac;hpb=68e5274fbda4c51b920db69b2e87d5646c446ce4;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.gnuplot/src/org/simantics/gnuplot/GnuplotSession.java b/bundles/org.simantics.gnuplot/src/org/simantics/gnuplot/GnuplotSession.java index 32534e0cf..6bc3e5baf 100644 --- a/bundles/org.simantics.gnuplot/src/org/simantics/gnuplot/GnuplotSession.java +++ b/bundles/org.simantics.gnuplot/src/org/simantics/gnuplot/GnuplotSession.java @@ -1,7 +1,10 @@ package org.simantics.gnuplot; +import java.io.BufferedReader; import java.io.Closeable; import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; import java.io.PrintStream; import java.nio.file.Files; import java.nio.file.Path; @@ -18,13 +21,17 @@ public class GnuplotSession implements Closeable { private Thread stdoutDumper; private Thread stderrDumper; - GnuplotSession(Path workingDirectory, Path stdout, Path stderr, Process process) { + GnuplotSession(Path workingDirectory, boolean drainStdout, boolean drainStderr, Process process) { this.process = process; this.out = new PrintStream(process.getOutputStream()); - stdoutDumper = new Thread(new InputStreamToFileCopier(process.getInputStream(), stdout)); - stderrDumper = new Thread(new InputStreamToFileCopier(process.getErrorStream(), stderr)); - stdoutDumper.start(); - stderrDumper.start(); + if (drainStdout) { + stdoutDumper = new Thread(new InputStreamToFileCopier(process.getInputStream(), null)); + stdoutDumper.start(); + } + if (drainStderr) { + stderrDumper = new Thread(new InputStreamToFileCopier(process.getErrorStream(), null)); + stderrDumper.start(); + } } private void assertAlive() { @@ -37,6 +44,15 @@ public class GnuplotSession implements Closeable { out.println(commands); } + public void evaluateStream(InputStream input, String charset) throws IOException { + try (BufferedReader br = new BufferedReader(new InputStreamReader(input, charset))) { + for (String line = br.readLine(); null != line; line = br.readLine()) { + out.println(line); + out.println(); + } + } + } + public void evaluateFile(Path file) throws IOException { try { Files.lines(file).forEachOrdered(l -> { @@ -67,8 +83,10 @@ public class GnuplotSession implements Closeable { out.close(); try { // Wait for the outputs to be closed by the process itself. - stdoutDumper.join(); - stderrDumper.join(); + if (stdoutDumper != null) + stdoutDumper.join(); + if (stderrDumper != null) + stderrDumper.join(); p.waitFor(); //System.out.println("gnuplot process ended"); } catch (InterruptedException e) {