diff dbus-d-javahelper/jsrc/dbus_d_javahelper/ConsoleProcess.java @ 0:a5576806d36d

recreate repository without any libs for lightweight repository
author Frank Benoit <benoit@tionex.de>
date Sat, 20 Oct 2007 18:07:18 +0200
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dbus-d-javahelper/jsrc/dbus_d_javahelper/ConsoleProcess.java	Sat Oct 20 18:07:18 2007 +0200
@@ -0,0 +1,66 @@
+/*
+ * License: Public Domain
+ * written by Frank Benoit
+ */
+package dbus_d_javahelper;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+
+public class ConsoleProcess {
+	
+	private final Thread threadStdout;
+	private final Thread threadStderr;
+	private final Process process;
+
+	private class StreamPump extends Thread {
+
+		private final InputStream istr;
+		private final OutputStream ostr;
+
+		public StreamPump(InputStream istr, OutputStream ostr) {
+			this.istr = istr;
+			this.ostr = ostr;
+		}
+
+		public void run() {
+			BufferedReader inputStream = new BufferedReader(
+					new InputStreamReader(istr));
+			PrintWriter writer = new PrintWriter(new OutputStreamWriter(ostr));
+			String c;
+			try {
+				while ((c = inputStream.readLine()) != null) {
+					writer.println(c);
+					writer.flush();
+				}
+			} catch (IOException e) {
+				e.printStackTrace();
+			}
+		}
+	}
+	public ConsoleProcess( String[] cmdarray, String[] envp, File basedir, OutputStream stdout, OutputStream stderr ) throws IOException{
+	      process = Runtime.getRuntime().exec( cmdarray, envp, basedir );
+	      threadStdout = new StreamPump( process.getInputStream(), stdout ); 
+	      threadStderr = new StreamPump( process.getErrorStream(), stderr ); 
+	      
+	      threadStdout.start();
+	      threadStderr.start();
+	}
+	
+	public int join() throws InterruptedException{
+		process.waitFor();
+		threadStdout.join();
+		threadStderr.join();
+		return process.exitValue();
+	}
+
+	public void interrupt() {
+		process.destroy();
+	}
+}