Mercurial Hosting > luan
changeset 713:a21e9594307d
add Process handling to Utils.java
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 20 May 2016 17:03:57 -0600 |
parents | 20051fe2a943 |
children | 7a322e942c15 |
files | core/src/luan/modules/Utils.java |
diffstat | 1 files changed, 48 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
diff -r 20051fe2a943 -r a21e9594307d core/src/luan/modules/Utils.java --- a/core/src/luan/modules/Utils.java Thu May 19 22:19:25 2016 -0600 +++ b/core/src/luan/modules/Utils.java Fri May 20 17:03:57 2016 -0600 @@ -6,6 +6,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.io.File; +import java.io.InputStreamReader; import java.net.URL; import java.net.MalformedURLException; import luan.LuanException; @@ -148,4 +149,51 @@ return toFile(path)!=null || toUrl(path)!=null; } */ + + + + + // process + + public static class ProcessException extends IOException { + private ProcessException(String msg) { + super(msg); + } + } + + public static void checkProcess(Process proc) + throws IOException + { + try { + proc.waitFor(); + } catch(InterruptedException e) { + throw new RuntimeException(e); + } + int exitVal = proc.exitValue(); + if( exitVal != 0 ) { + Reader err = new InputStreamReader(proc.getErrorStream()); + String error = readAll(err); + err.close(); + throw new ProcessException(error); + } + } + + public static String getOutput(Process proc) + throws IOException + { + Reader in = new InputStreamReader(proc.getInputStream()); + String s = readAll(in); + in.close(); + return s; + } + + public static String execProcess(String... cmd) + throws IOException + { + Process proc = Runtime.getRuntime().exec(cmd); + String s = getOutput(proc); + checkProcess(proc); + return s; + } + }