aapt2: new method to execAndReturn to collect stdout from a process

This commit is contained in:
Connor Tumbleson 2018-02-15 18:05:06 -05:00
parent f5caccbc2e
commit 574de8db71

View File

@ -19,6 +19,8 @@ package brut.util;
import brut.common.BrutException;
import java.io.*;
import java.util.Arrays;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Logger;
import org.apache.commons.io.IOUtils;
@ -100,6 +102,23 @@ public class OS {
}
}
public static String execAndReturn(String[] cmd) {
ExecutorService executor = Executors.newCachedThreadPool();
try {
ProcessBuilder builder = new ProcessBuilder(cmd);
builder.redirectErrorStream(true);
Process process = builder.start();
StreamCollector collector = new StreamCollector(process.getInputStream());
executor.execute(collector);
process.waitFor();
return collector.get();
} catch (IOException | InterruptedException e) {
return null;
}
}
public static File createTempDirectory() throws BrutException {
try {
File tmp = File.createTempFile("BRUT", null);
@ -142,4 +161,37 @@ public class OS {
private final InputStream mIn;
private final String mType;
}
static class StreamCollector implements Runnable {
private final StringBuffer buffer = new StringBuffer();
private final InputStream inputStream;
public StreamCollector(InputStream inputStream) {
super();
this.inputStream = inputStream;
}
@Override
public void run() {
BufferedReader reader = null;
String line;
try {
reader = new BufferedReader(new InputStreamReader(inputStream));
while ((line = reader.readLine()) != null) {
buffer.append(line).append('\n');
}
} catch (Exception e) {
System.err.println(e);
} finally {
try {
reader.close();
} catch (IOException e) {}
}
}
public String get() {
return buffer.toString();
}
}
}