aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit/src/org/eclipse/jgit/util/FS.java
diff options
context:
space:
mode:
authorRobin Rosenberg <robin.rosenberg@dewire.com>2010-12-28 17:15:12 +0100
committerShawn O. Pearce <spearce@spearce.org>2010-12-30 12:41:22 -0800
commit240769e023c4ea6c8394c25d58fdca7f0bb82948 (patch)
treecdb71c9fea3725b35e01833c108f4997c12a37a2 /org.eclipse.jgit/src/org/eclipse/jgit/util/FS.java
parent14b358a6fbe2aa9f04a1bb6a27812458af6a755e (diff)
downloadjgit-240769e023c4ea6c8394c25d58fdca7f0bb82948.tar.gz
jgit-240769e023c4ea6c8394c25d58fdca7f0bb82948.zip
Refactor exec of a command and reading one line into utility
Change-Id: Ia9e5afe7f29c3e5e74b8d226441ed429fb229c82 Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
Diffstat (limited to 'org.eclipse.jgit/src/org/eclipse/jgit/util/FS.java')
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/util/FS.java42
1 files changed, 42 insertions, 0 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/FS.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/FS.java
index 2287f68fa2..0a3fad001b 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/util/FS.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/FS.java
@@ -43,7 +43,10 @@
package org.eclipse.jgit.util;
+import java.io.BufferedReader;
import java.io.File;
+import java.io.IOException;
+import java.io.InputStreamReader;
import java.security.AccessController;
import java.security.PrivilegedAction;
@@ -210,4 +213,43 @@ public abstract class FS {
}
return null;
}
+
+ /**
+ * Execute a command and return a single line of output as a String
+ *
+ * @param dir
+ * Working directory for the command
+ * @param command
+ * as component array
+ * @param encoding
+ * @return the one-line output of the command
+ */
+ protected static String readPipe(File dir, String[] command, String encoding) {
+ try {
+ final Process p = Runtime.getRuntime().exec(command, null, dir);
+ final BufferedReader lineRead = new BufferedReader(
+ new InputStreamReader(p.getInputStream(), encoding));
+ String r = null;
+ try {
+ r = lineRead.readLine();
+ } finally {
+ p.getOutputStream().close();
+ p.getErrorStream().close();
+ lineRead.close();
+ }
+
+ for (;;) {
+ try {
+ if (p.waitFor() == 0 && r != null && r.length() > 0)
+ return r;
+ break;
+ } catch (InterruptedException ie) {
+ // Stop bothering me, I have a zombie to reap.
+ }
+ }
+ } catch (IOException e) {
+ // ignore
+ }
+ return null;
+ }
}