mirror of
https://github.com/esp8266/Arduino.git
synced 2025-08-14 08:03:09 +03:00
Introducing CollectStdOut and CollectStdOutStderrExecutor, handy classes for executing external execs and collecting their outputs
This commit is contained in:
@@ -10,9 +10,9 @@ import java.io.OutputStream;
|
||||
/**
|
||||
* Handy process executor, collecting stdout into a given OutputStream
|
||||
*/
|
||||
public class ExternalProcessExecutor extends DefaultExecutor {
|
||||
public class CollectStdOutExecutor extends DefaultExecutor {
|
||||
|
||||
public ExternalProcessExecutor(final OutputStream os) {
|
||||
public CollectStdOutExecutor(final OutputStream stdout) {
|
||||
this.setStreamHandler(new ExecuteStreamHandler() {
|
||||
@Override
|
||||
public void setProcessInputStream(OutputStream outputStream) throws IOException {
|
||||
@@ -27,7 +27,7 @@ public class ExternalProcessExecutor extends DefaultExecutor {
|
||||
byte[] buf = new byte[4096];
|
||||
int bytes = -1;
|
||||
while ((bytes = inputStream.read(buf)) != -1) {
|
||||
os.write(buf, 0, bytes);
|
||||
stdout.write(buf, 0, bytes);
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,49 @@
|
||||
package processing.app.tools;
|
||||
|
||||
import org.apache.commons.exec.DefaultExecutor;
|
||||
import org.apache.commons.exec.ExecuteStreamHandler;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
/**
|
||||
* Handy process executor, collecting stdout and stderr into given OutputStreams
|
||||
*/
|
||||
public class CollectStdOutStdErrExecutor extends DefaultExecutor {
|
||||
|
||||
public CollectStdOutStdErrExecutor(final OutputStream stdout, final OutputStream stderr) {
|
||||
this.setStreamHandler(new ExecuteStreamHandler() {
|
||||
@Override
|
||||
public void setProcessInputStream(OutputStream outputStream) throws IOException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setProcessErrorStream(InputStream inputStream) throws IOException {
|
||||
byte[] buf = new byte[4096];
|
||||
int bytes = -1;
|
||||
while ((bytes = inputStream.read(buf)) != -1) {
|
||||
stderr.write(buf, 0, bytes);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setProcessOutputStream(InputStream inputStream) throws IOException {
|
||||
byte[] buf = new byte[4096];
|
||||
int bytes = -1;
|
||||
while ((bytes = inputStream.read(buf)) != -1) {
|
||||
stdout.write(buf, 0, bytes);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() throws IOException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user