Merge pull request #1126 from BurgerZ/patch-3

Write output to separate streams (error and output)
This commit is contained in:
Connor Tumbleson 2016-01-06 12:50:44 -06:00
commit a64a031f4f

View File

@ -19,6 +19,7 @@ package brut.util;
import brut.common.BrutException; import brut.common.BrutException;
import java.io.*; import java.io.*;
import java.util.Arrays; import java.util.Arrays;
import java.util.logging.Logger;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
@ -26,6 +27,9 @@ import org.apache.commons.io.IOUtils;
* @author Ryszard Wiśniewski <brut.alll@gmail.com> * @author Ryszard Wiśniewski <brut.alll@gmail.com>
*/ */
public class OS { public class OS {
private static final Logger LOGGER = Logger.getLogger("");
public static void rmdir(File dir) throws BrutException { public static void rmdir(File dir) throws BrutException {
if (! dir.exists()) { if (! dir.exists()) {
return; return;
@ -80,21 +84,19 @@ public class OS {
public static void exec(String[] cmd) throws BrutException { public static void exec(String[] cmd) throws BrutException {
Process ps = null; Process ps = null;
int exitValue = -99;
try { try {
ps = Runtime.getRuntime().exec(cmd); ProcessBuilder builder = new ProcessBuilder(cmd);
ps = builder.start();
new StreamForwarder(ps.getInputStream(), System.err).start(); new StreamForwarder(ps.getErrorStream(), "ERROR").start();
new StreamForwarder(ps.getErrorStream(), System.err).start(); new StreamForwarder(ps.getInputStream(), "OUTPUT").start();
if (ps.waitFor() != 0) { exitValue = ps.waitFor();
throw new BrutException( if (exitValue != 0)
"could not exec command: " + Arrays.toString(cmd)); throw new BrutException("could not exec (exit code = " + exitValue + "): " + Arrays.toString(cmd));
}
} catch (IOException ex) { } catch (IOException ex) {
throw new BrutException( throw new BrutException("could not exec: " + Arrays.toString(cmd), ex);
"could not exec command: " + Arrays.toString(cmd), ex);
} catch (InterruptedException ex) { } catch (InterruptedException ex) {
throw new BrutException( throw new BrutException("could not exec : " + Arrays.toString(cmd), ex);
"could not exec command: " + Arrays.toString(cmd), ex);
} }
} }
@ -115,30 +117,29 @@ public class OS {
static class StreamForwarder extends Thread { static class StreamForwarder extends Thread {
public StreamForwarder(InputStream in, OutputStream out) { StreamForwarder(InputStream is, String type) {
mIn = in; mIn = is;
mOut = out; mType = type;
} }
@Override @Override
public void run() { public void run() {
try { try {
BufferedReader in = new BufferedReader( BufferedReader br = new BufferedReader(new InputStreamReader(mIn));
new InputStreamReader(mIn));
BufferedWriter out = new BufferedWriter(
new OutputStreamWriter(mOut));
String line; String line;
while ((line = in.readLine()) != null) { while ((line = br.readLine()) != null) {
out.write(line); if (mType.equals("OUTPUT")) {
out.newLine(); LOGGER.info(line);
} else {
LOGGER.warning(line);
}
} }
out.flush();
} catch (IOException ex) { } catch (IOException ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
} }
private final InputStream mIn; private final InputStream mIn;
private final OutputStream mOut; private final String mType;
} }
} }