aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/pacien/tincapp/commands/Executor.kt
blob: 160f0cd0d0ffb00fa063d956aae0626022335a9a (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
package org.pacien.tincapp.commands

import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader
import java.util.*

/**
 * @author pacien
 */
internal object Executor {

    init {
        System.loadLibrary("exec")
    }

    /**
     * @return -1 on error, forked child PID otherwise
     */
    private external fun forkExec(argcv: Array<String>): Int

    @Throws(IOException::class)
    fun forkExec(cmd: Command) {
        if (forkExec(cmd.asArray()) == -1)
            throw IOException()
    }

    @Throws(IOException::class)
    fun call(cmd: Command): List<String> {
        val proc = ProcessBuilder(cmd.asList()).start()
        val outputReader = BufferedReader(InputStreamReader(proc.inputStream))

        var line: String? = outputReader.readLine()
        val list = LinkedList<String>()
        while (line != null) {
            line = outputReader.readLine()
            list.add(line)
        }

        return list
    }

}