1 package snow.utils; 2 3 import java.io.*; 4 import java.util.*; 5 6 7 9 public final class ProcessUtils 10 { 11 private ProcessUtils() {} 12 13 17 public static Process executeProcessAndGobble(List<String > processArgs, File execDir, String descr) throws Exception 18 { 19 21 Process proc = null; 22 if(execDir!=null) 23 { 24 proc = Runtime.getRuntime().exec( processArgs.toArray(new String [processArgs.size()]), null, execDir ); 25 } 26 else 27 { 28 proc = Runtime.getRuntime().exec( processArgs.toArray(new String [processArgs.size()]) ); 29 } 30 31 StreamGobbler sg1 = new StreamGobbler(proc.getInputStream(), null); 32 sg1.setName("Gobbler "+descr+" (in)"); 33 sg1.start(); 34 StreamGobbler sg2 = new StreamGobbler(proc.getErrorStream(), null); 35 sg2.start(); 36 sg2.setName("Gobbler "+descr+" (err)"); 37 38 return proc; 39 } 40 41 43 public static Process executeProcessAndGobble(List<String > processArgs, String descr) throws Exception 44 { 45 return executeProcessAndGobble(processArgs, null, descr); 46 } 47 48 52 public static String readWholeProcessStack(List<String > processArgs) throws Exception 53 { 54 Process proc = Runtime.getRuntime().exec( processArgs.toArray(new String [processArgs.size()]) ); 56 57 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 58 StreamGobbler sg1 = new StreamGobbler(proc.getInputStream(), baos); 59 sg1.start(); 60 StreamGobbler sg2 = new StreamGobbler(proc.getErrorStream(), baos); 61 sg2.start(); 62 sg1.join(10000L); 63 sg2.join(10000L); 64 65 int ev = proc.exitValue(); 67 68 return new String ( baos.toByteArray() ) ; 69 70 } 72 73 74 public static String readWholeProcessStack(List<String > processArgs, File execDir) throws Exception 75 { 76 return readWholeProcessStack(processArgs, execDir, null, false); 77 } 78 79 83 public static String readWholeProcessStack(List<String > processArgs, File execDir, List<String > toType, boolean errorIfNotStatusExit0) throws Exception 84 { 85 Process proc = Runtime.getRuntime().exec( processArgs.toArray(new String [processArgs.size()]), 86 null, execDir ); 88 89 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 90 StreamGobbler sg1 = new StreamGobbler(proc.getInputStream(), baos); 91 sg1.start(); 92 StreamGobbler sg2 = new StreamGobbler(proc.getErrorStream(), baos); 93 sg2.start(); 94 95 if(toType!=null) 96 { 97 for(String tt : toType) 98 { 99 proc.getOutputStream().write(tt.getBytes()); 100 proc.getOutputStream().flush(); 101 } 102 } 103 104 sg1.join(10000L); 106 sg2.join(10000L); 107 108 109 int ev = proc.exitValue(); 111 112 String stack = new String ( baos.toByteArray() ); 113 114 if(errorIfNotStatusExit0 && ev!=0) throw new RuntimeException ("Exit value was "+ev+"\nstack: "+stack); 115 116 return stack ; 117 } 118 119 121 public static String readWholeProcessStack(Process proc, long timeout) throws Exception 122 { 123 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 124 StreamGobbler sg1 = new StreamGobbler(proc.getInputStream(), baos); 125 sg1.start(); 126 StreamGobbler sg2 = new StreamGobbler(proc.getErrorStream(), baos); 127 sg2.start(); 128 if(timeout>0) 129 { 130 sg1.join(timeout); 131 sg2.join(timeout); 132 } 133 else 134 { 135 sg1.join(); 136 sg2.join(); 137 } 138 139 return new String ( baos.toByteArray() ) ; 140 } 141 142 143 146 public static void drainProcess(Process proc, boolean waitCompletion) 147 { 148 StreamGobbler sg1 = new StreamGobbler(proc.getInputStream(), null); 149 sg1.start(); 150 StreamGobbler sg2 = new StreamGobbler(proc.getErrorStream(), null); 151 sg2.start(); 152 153 if(waitCompletion) 154 { 155 try 156 { 157 sg1.join(); 158 sg2.join(); 159 } catch(InterruptedException ie) { ie.printStackTrace(); } 160 } 161 } 162 163 164 165 166 167 } | Popular Tags |