1 18 19 package com.Ostermiller.util; 20 21 import java.io.*; 22 23 31 public final class ExecHelper { 32 33 50 public static ExecHelper exec(String [] cmdarray) throws IOException { 51 return new ExecHelper(Runtime.getRuntime().exec(cmdarray), null); 52 } 53 54 72 public static ExecHelper exec(String [] cmdarray, String [] envp) throws IOException { 73 return new ExecHelper(Runtime.getRuntime().exec(cmdarray, envp), null); 74 } 75 76 95 public static ExecHelper exec(String [] cmdarray, String [] envp, File dir) throws IOException { 96 return new ExecHelper(Runtime.getRuntime().exec(cmdarray, envp), null); 97 } 98 99 115 public static ExecHelper exec(String [] cmdarray, String charset) throws IOException { 116 return new ExecHelper(Runtime.getRuntime().exec(cmdarray), charset); 117 } 118 119 136 public static ExecHelper exec(String [] cmdarray, String [] envp, String charset) throws IOException { 137 return new ExecHelper(Runtime.getRuntime().exec(cmdarray, envp), charset); 138 } 139 140 158 public static ExecHelper exec(String [] cmdarray, String [] envp, File dir, String charset) throws IOException { 159 return new ExecHelper(Runtime.getRuntime().exec(cmdarray, envp), charset); 160 } 161 162 181 public static ExecHelper execUsingShell(String command) throws IOException { 182 return execUsingShell(command, null); 183 } 184 185 203 public static ExecHelper execUsingShell(String command, String charset) throws IOException { 204 if (command == null) throw new NullPointerException (); 205 String [] cmdarray; 206 String os = System.getProperty("os.name"); 207 if (os.equals("Windows 95") || os.equals("Windows 98") || os.equals("Windows ME")){ 208 cmdarray = new String []{"command.exe", "/C", command}; 209 } else if (os.startsWith("Windows")){ 210 cmdarray = new String []{"cmd.exe", "/C", command}; 211 } else { 212 cmdarray = new String []{"/bin/sh", "-c", command}; 213 } 214 return new ExecHelper(Runtime.getRuntime().exec(cmdarray), charset); 215 } 216 217 228 private ExecHelper(Process process, String charset) throws IOException { 229 StringBuffer output = new StringBuffer (); 230 StringBuffer error = new StringBuffer (); 231 232 Reader stdout; 233 Reader stderr; 234 235 if (charset == null){ 236 stdout = new InputStreamReader(process.getInputStream()); 239 stderr = new InputStreamReader(process.getErrorStream()); 240 } else { 241 stdout = new InputStreamReader(process.getInputStream(), charset); 242 stderr = new InputStreamReader(process.getErrorStream(), charset); 243 } 244 char[] buffer = new char[1024]; 245 246 boolean done = false; 247 boolean stdoutclosed = false; 248 boolean stderrclosed = false; 249 while (!done){ 250 boolean readSomething = false; 251 if (!stdoutclosed && stdout.ready()){ 253 readSomething = true; 254 int read = stdout.read(buffer, 0, buffer.length); 255 if (read < 0){ 256 readSomething = true; 257 stdoutclosed = true; 258 } else if (read > 0){ 259 readSomething = true; 260 output.append(buffer, 0, read); 261 } 262 } 263 if (!stderrclosed && stderr.ready()){ 265 int read = stderr.read(buffer, 0, buffer.length); 266 if (read < 0){ 267 readSomething = true; 268 stderrclosed = true; 269 } else if (read > 0){ 270 readSomething = true; 271 error.append(buffer, 0, read); 272 } 273 } 274 if (!readSomething){ 277 try { 278 this.status = process.exitValue(); 279 done = true; 280 } catch (IllegalThreadStateException itx){ 281 try { 284 Thread.sleep(100); 285 } catch (InterruptedException ix){ 286 process.destroy(); 287 throw new IOException("Interrupted - processes killed"); 288 } 289 } 290 } 291 } 292 293 this.output = output.toString(); 294 this.error = error.toString(); 295 } 296 297 302 private String output; 303 304 311 public String getOutput(){ 312 return output; 313 } 314 315 320 private String error; 321 322 329 public String getError(){ 330 return error; 331 } 332 333 338 private int status; 339 340 347 public int getStatus(){ 348 return status; 349 } 350 } 351 | Popular Tags |