aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/pacien/tincapp/commands/Executor.java
blob: edf229c83e8b4f62e5a317cde77caa8c29522b38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package org.pacien.tincapp.commands;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * @author pacien
 */
final class Executor {

	private Executor() {
		// static class
	}

	static {
		System.loadLibrary("exec");
	}

	/**
	 * @return -1 on error, forked child PID otherwise
	 */
	static private native int forkExec(String[] argcv);

	static public void forkExec(Command cmd) throws IOException {
		if (forkExec(cmd.asArray()) == -1)
			throw new IOException();
	}

	static public List<String> call(Command cmd) throws IOException {
		Process proc = new ProcessBuilder(cmd.asList()).start();
		BufferedReader outputReader = new BufferedReader(new InputStreamReader(proc.getInputStream()));

		String line;
		List<String> list = new LinkedList<>();
		while ((line = outputReader.readLine()) != null) list.add(line);

		return Collections.unmodifiableList(list);
	}

}