]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.gnuplot/src/org/simantics/gnuplot/GnuplotSession.java
Removed rubbish included in previous commit
[simantics/platform.git] / bundles / org.simantics.gnuplot / src / org / simantics / gnuplot / GnuplotSession.java
index 32534e0cf8baff32b0c4e4d9640966fdc50becac..6bc3e5bafa3f66f3472f5713fccfd780a52bc050 100644 (file)
@@ -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) {