Mercurial Hosting > luan
comparison core/src/luan/modules/Utils.java @ 713:a21e9594307d
add Process handling to Utils.java
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 20 May 2016 17:03:57 -0600 |
parents | cdc70de628b5 |
children | e44e98fe9de8 |
comparison
equal
deleted
inserted
replaced
712:20051fe2a943 | 713:a21e9594307d |
---|---|
4 import java.io.IOException; | 4 import java.io.IOException; |
5 import java.io.ByteArrayOutputStream; | 5 import java.io.ByteArrayOutputStream; |
6 import java.io.InputStream; | 6 import java.io.InputStream; |
7 import java.io.OutputStream; | 7 import java.io.OutputStream; |
8 import java.io.File; | 8 import java.io.File; |
9 import java.io.InputStreamReader; | |
9 import java.net.URL; | 10 import java.net.URL; |
10 import java.net.MalformedURLException; | 11 import java.net.MalformedURLException; |
11 import luan.LuanException; | 12 import luan.LuanException; |
12 import luan.LuanTable; | 13 import luan.LuanTable; |
13 import luan.LuanFunction; | 14 import luan.LuanFunction; |
146 | 147 |
147 static boolean exists(String path) { | 148 static boolean exists(String path) { |
148 return toFile(path)!=null || toUrl(path)!=null; | 149 return toFile(path)!=null || toUrl(path)!=null; |
149 } | 150 } |
150 */ | 151 */ |
152 | |
153 | |
154 | |
155 | |
156 // process | |
157 | |
158 public static class ProcessException extends IOException { | |
159 private ProcessException(String msg) { | |
160 super(msg); | |
161 } | |
162 } | |
163 | |
164 public static void checkProcess(Process proc) | |
165 throws IOException | |
166 { | |
167 try { | |
168 proc.waitFor(); | |
169 } catch(InterruptedException e) { | |
170 throw new RuntimeException(e); | |
171 } | |
172 int exitVal = proc.exitValue(); | |
173 if( exitVal != 0 ) { | |
174 Reader err = new InputStreamReader(proc.getErrorStream()); | |
175 String error = readAll(err); | |
176 err.close(); | |
177 throw new ProcessException(error); | |
178 } | |
179 } | |
180 | |
181 public static String getOutput(Process proc) | |
182 throws IOException | |
183 { | |
184 Reader in = new InputStreamReader(proc.getInputStream()); | |
185 String s = readAll(in); | |
186 in.close(); | |
187 return s; | |
188 } | |
189 | |
190 public static String execProcess(String... cmd) | |
191 throws IOException | |
192 { | |
193 Process proc = Runtime.getRuntime().exec(cmd); | |
194 String s = getOutput(proc); | |
195 checkProcess(proc); | |
196 return s; | |
197 } | |
198 | |
151 } | 199 } |