aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/pacien/tincapp/commands/Executor.java
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/commands/Executor.java')
-rw-r--r--app/src/main/java/org/pacien/tincapp/commands/Executor.java44
1 files changed, 44 insertions, 0 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/commands/Executor.java b/app/src/main/java/org/pacien/tincapp/commands/Executor.java
new file mode 100644
index 0000000..edf229c
--- /dev/null
+++ b/app/src/main/java/org/pacien/tincapp/commands/Executor.java
@@ -0,0 +1,44 @@
1package org.pacien.tincapp.commands;
2
3import java.io.BufferedReader;
4import java.io.IOException;
5import java.io.InputStreamReader;
6import java.util.Collections;
7import java.util.LinkedList;
8import java.util.List;
9
10/**
11 * @author pacien
12 */
13final class Executor {
14
15 private Executor() {
16 // static class
17 }
18
19 static {
20 System.loadLibrary("exec");
21 }
22
23 /**
24 * @return -1 on error, forked child PID otherwise
25 */
26 static private native int forkExec(String[] argcv);
27
28 static public void forkExec(Command cmd) throws IOException {
29 if (forkExec(cmd.asArray()) == -1)
30 throw new IOException();
31 }
32
33 static public List<String> call(Command cmd) throws IOException {
34 Process proc = new ProcessBuilder(cmd.asList()).start();
35 BufferedReader outputReader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
36
37 String line;
38 List<String> list = new LinkedList<>();
39 while ((line = outputReader.readLine()) != null) list.add(line);
40
41 return Collections.unmodifiableList(list);
42 }
43
44}