aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/pacien/tincapp/commands/Executor.kt
diff options
context:
space:
mode:
authorpacien2018-02-10 14:16:15 +0100
committerpacien2018-02-10 14:16:15 +0100
commit87ec3620d2259064831356c2f4000ae591756fd2 (patch)
tree9312da1d357fdd3790d5b80af221653696b23089 /app/src/main/java/org/pacien/tincapp/commands/Executor.kt
parentd1c39ba87bb32308c2d3a7a749abffeb773541ef (diff)
downloadtincapp-87ec3620d2259064831356c2f4000ae591756fd2.tar.gz
Reformat code
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/commands/Executor.kt')
-rw-r--r--app/src/main/java/org/pacien/tincapp/commands/Executor.kt48
1 files changed, 24 insertions, 24 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/commands/Executor.kt b/app/src/main/java/org/pacien/tincapp/commands/Executor.kt
index 38c2cb5..eace2e7 100644
--- a/app/src/main/java/org/pacien/tincapp/commands/Executor.kt
+++ b/app/src/main/java/org/pacien/tincapp/commands/Executor.kt
@@ -13,37 +13,37 @@ import java.io.InputStreamReader
13 */ 13 */
14internal object Executor { 14internal object Executor {
15 15
16 class CommandExecutionException(msg: String) : Exception(msg) 16 class CommandExecutionException(msg: String) : Exception(msg)
17 17
18 init { 18 init {
19 System.loadLibrary("exec") 19 System.loadLibrary("exec")
20 } 20 }
21
22 /**
23 * @return -1 on error, forked child PID otherwise
24 */
25 private external fun forkExec(argcv: Array<String>): Int
21 26
22 /** 27 private fun read(stream: InputStream) = BufferedReader(InputStreamReader(stream)).readLines()
23 * @return -1 on error, forked child PID otherwise
24 */
25 private external fun forkExec(argcv: Array<String>): Int
26 28
27 private fun read(stream: InputStream) = BufferedReader(InputStreamReader(stream)).readLines() 29 fun forkExec(cmd: Command) {
30 if (forkExec(cmd.asArray()) == -1) throw CommandExecutionException("Could not fork child process.")
31 }
28 32
29 fun forkExec(cmd: Command) { 33 fun call(cmd: Command): CompletableFuture<List<String>> {
30 if (forkExec(cmd.asArray()) == -1) throw CommandExecutionException("Could not fork child process.") 34 val proc = try {
35 ProcessBuilder(cmd.asList()).start()
36 } catch (e: IOException) {
37 throw CommandExecutionException(e.message ?: "Could not start process.")
31 } 38 }
32 39
33 fun call(cmd: Command): CompletableFuture<List<String>> { 40 return supplyAsyncTask<List<String>> {
34 val proc = try { 41 if (proc.waitFor() == 0) read(proc.inputStream)
35 ProcessBuilder(cmd.asList()).start() 42 else throw CommandExecutionException(read(proc.errorStream).lastOrNull() ?: "Non-zero exit status.")
36 } catch (e: IOException) {
37 throw CommandExecutionException(e.message ?: "Could not start process.")
38 }
39
40 return supplyAsyncTask<List<String>> {
41 if (proc.waitFor() == 0) read(proc.inputStream)
42 else throw CommandExecutionException(read(proc.errorStream).lastOrNull() ?: "Non-zero exit status.")
43 }
44 } 43 }
44 }
45 45
46 fun runAsyncTask(r: () -> Unit) = CompletableFuture.runAsync(Runnable(r), AsyncTask.THREAD_POOL_EXECUTOR)!! 46 fun runAsyncTask(r: () -> Unit) = CompletableFuture.runAsync(Runnable(r), AsyncTask.THREAD_POOL_EXECUTOR)!!
47 fun <U> supplyAsyncTask(s: () -> U) = CompletableFuture.supplyAsync(Supplier(s), AsyncTask.THREAD_POOL_EXECUTOR)!! 47 fun <U> supplyAsyncTask(s: () -> U) = CompletableFuture.supplyAsync(Supplier(s), AsyncTask.THREAD_POOL_EXECUTOR)!!
48 48
49} 49}