diff options
author | Pacien TRAN-GIRARD | 2017-06-30 10:21:51 +0200 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2017-06-30 10:21:51 +0200 |
commit | c89016e7427defd1f8a095d7aeb30292a42fad92 (patch) | |
tree | aec80e6f501824f966ea727d2f6e102a75e9655f /app/src/main/java/org/pacien/tincapp/commands | |
parent | 94d77ce43152a4e144417d8d5cfff363f65c904e (diff) | |
download | tincapp-c89016e7427defd1f8a095d7aeb30292a42fad92.tar.gz |
Conversion to Kotlin
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/commands')
10 files changed, 173 insertions, 233 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/commands/Command.java b/app/src/main/java/org/pacien/tincapp/commands/Command.java deleted file mode 100644 index 28ff226..0000000 --- a/app/src/main/java/org/pacien/tincapp/commands/Command.java +++ /dev/null | |||
@@ -1,67 +0,0 @@ | |||
1 | package org.pacien.tincapp.commands; | ||
2 | |||
3 | import com.annimon.stream.Collectors; | ||
4 | import com.annimon.stream.Optional; | ||
5 | import com.annimon.stream.Stream; | ||
6 | |||
7 | import java.util.Arrays; | ||
8 | import java.util.Collections; | ||
9 | import java.util.LinkedList; | ||
10 | import java.util.List; | ||
11 | |||
12 | /** | ||
13 | * @author pacien | ||
14 | */ | ||
15 | class Command { | ||
16 | |||
17 | static private class Option { | ||
18 | final String key; | ||
19 | final Optional<java.lang.String> val; | ||
20 | |||
21 | Option(String key, String val) { | ||
22 | this.key = key; | ||
23 | this.val = Optional.ofNullable(val); | ||
24 | } | ||
25 | |||
26 | @Override | ||
27 | public String toString() { | ||
28 | return val.isPresent() ? "--" + key + "=" + val.get() : "--" + key; | ||
29 | } | ||
30 | } | ||
31 | |||
32 | final private String cmd; | ||
33 | final private List<Option> opts; | ||
34 | final private List<String> args; | ||
35 | |||
36 | public Command(String cmd) { | ||
37 | this.cmd = cmd; | ||
38 | this.opts = new LinkedList<>(); | ||
39 | this.args = new LinkedList<>(); | ||
40 | } | ||
41 | |||
42 | public Command withOption(String key, String val) { | ||
43 | this.opts.add(new Option(key, val)); | ||
44 | return this; | ||
45 | } | ||
46 | |||
47 | public Command withOption(String key) { | ||
48 | return this.withOption(key, null); | ||
49 | } | ||
50 | |||
51 | public Command withArguments(String... args) { | ||
52 | this.args.addAll(Arrays.asList(args)); | ||
53 | return this; | ||
54 | } | ||
55 | |||
56 | public List<String> asList() { | ||
57 | return Collections.unmodifiableList( | ||
58 | Stream.concat(Stream.of(Collections.singleton(this.cmd)), | ||
59 | Stream.concat(Stream.of(this.opts).map(Option::toString), Stream.of(this.args))) | ||
60 | .collect(Collectors.toList())); | ||
61 | } | ||
62 | |||
63 | public String[] asArray() { | ||
64 | return this.asList().toArray(new String[0]); | ||
65 | } | ||
66 | |||
67 | } | ||
diff --git a/app/src/main/java/org/pacien/tincapp/commands/Command.kt b/app/src/main/java/org/pacien/tincapp/commands/Command.kt new file mode 100644 index 0000000..9b22d5f --- /dev/null +++ b/app/src/main/java/org/pacien/tincapp/commands/Command.kt | |||
@@ -0,0 +1,36 @@ | |||
1 | package org.pacien.tincapp.commands | ||
2 | |||
3 | import java.util.* | ||
4 | |||
5 | /** | ||
6 | * @author pacien | ||
7 | */ | ||
8 | internal class Command(private val cmd: String) { | ||
9 | |||
10 | private data class Option(val key: String, val value: String?) { | ||
11 | override fun toString(): String = if (value != null) "--$key=$value" else "--$key" | ||
12 | } | ||
13 | |||
14 | private val opts: MutableList<Option> | ||
15 | private val args: MutableList<String> | ||
16 | |||
17 | init { | ||
18 | this.opts = LinkedList<Option>() | ||
19 | this.args = LinkedList<String>() | ||
20 | } | ||
21 | |||
22 | fun withOption(key: String, value: String? = null): Command { | ||
23 | this.opts.add(Option(key, value)) | ||
24 | return this | ||
25 | } | ||
26 | |||
27 | fun withArguments(vararg args: String): Command { | ||
28 | this.args.addAll(Arrays.asList(*args)) | ||
29 | return this | ||
30 | } | ||
31 | |||
32 | fun asList(): List<String> = listOf(cmd) + opts.map { it.toString() } + args | ||
33 | |||
34 | fun asArray(): Array<String> = this.asList().toTypedArray() | ||
35 | |||
36 | } | ||
diff --git a/app/src/main/java/org/pacien/tincapp/commands/Executor.java b/app/src/main/java/org/pacien/tincapp/commands/Executor.java deleted file mode 100644 index edf229c..0000000 --- a/app/src/main/java/org/pacien/tincapp/commands/Executor.java +++ /dev/null | |||
@@ -1,44 +0,0 @@ | |||
1 | package org.pacien.tincapp.commands; | ||
2 | |||
3 | import java.io.BufferedReader; | ||
4 | import java.io.IOException; | ||
5 | import java.io.InputStreamReader; | ||
6 | import java.util.Collections; | ||
7 | import java.util.LinkedList; | ||
8 | import java.util.List; | ||
9 | |||
10 | /** | ||
11 | * @author pacien | ||
12 | */ | ||
13 | final 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 | } | ||
diff --git a/app/src/main/java/org/pacien/tincapp/commands/Executor.kt b/app/src/main/java/org/pacien/tincapp/commands/Executor.kt new file mode 100644 index 0000000..160f0cd --- /dev/null +++ b/app/src/main/java/org/pacien/tincapp/commands/Executor.kt | |||
@@ -0,0 +1,43 @@ | |||
1 | package org.pacien.tincapp.commands | ||
2 | |||
3 | import java.io.BufferedReader | ||
4 | import java.io.IOException | ||
5 | import java.io.InputStreamReader | ||
6 | import java.util.* | ||
7 | |||
8 | /** | ||
9 | * @author pacien | ||
10 | */ | ||
11 | internal object Executor { | ||
12 | |||
13 | init { | ||
14 | System.loadLibrary("exec") | ||
15 | } | ||
16 | |||
17 | /** | ||
18 | * @return -1 on error, forked child PID otherwise | ||
19 | */ | ||
20 | private external fun forkExec(argcv: Array<String>): Int | ||
21 | |||
22 | @Throws(IOException::class) | ||
23 | fun forkExec(cmd: Command) { | ||
24 | if (forkExec(cmd.asArray()) == -1) | ||
25 | throw IOException() | ||
26 | } | ||
27 | |||
28 | @Throws(IOException::class) | ||
29 | fun call(cmd: Command): List<String> { | ||
30 | val proc = ProcessBuilder(cmd.asList()).start() | ||
31 | val outputReader = BufferedReader(InputStreamReader(proc.inputStream)) | ||
32 | |||
33 | var line: String? = outputReader.readLine() | ||
34 | val list = LinkedList<String>() | ||
35 | while (line != null) { | ||
36 | line = outputReader.readLine() | ||
37 | list.add(line) | ||
38 | } | ||
39 | |||
40 | return list | ||
41 | } | ||
42 | |||