Skip to content

Commit a4fe351

Browse files
author
Federico Fissore
committed
external process executor
1 parent b8b4328 commit a4fe351

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package processing.app.tools;
2+
3+
import org.apache.commons.exec.DefaultExecutor;
4+
import org.apache.commons.exec.ExecuteStreamHandler;
5+
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.io.OutputStream;
9+
10+
/**
11+
* Handy process executor, collecting stdout into a given OutputStream
12+
*/
13+
public class ExternalProcessExecutor extends DefaultExecutor {
14+
15+
public ExternalProcessExecutor(final OutputStream os) {
16+
this.setStreamHandler(new ExecuteStreamHandler() {
17+
@Override
18+
public void setProcessInputStream(OutputStream outputStream) throws IOException {
19+
}
20+
21+
@Override
22+
public void setProcessErrorStream(InputStream inputStream) throws IOException {
23+
}
24+
25+
@Override
26+
public void setProcessOutputStream(InputStream inputStream) throws IOException {
27+
byte[] buf = new byte[4096];
28+
int bytes = -1;
29+
while ((bytes = inputStream.read(buf)) != -1) {
30+
os.write(buf, 0, bytes);
31+
}
32+
}
33+
34+
@Override
35+
public void start() throws IOException {
36+
}
37+
38+
@Override
39+
public void stop() {
40+
}
41+
});
42+
43+
}
44+
}

0 commit comments

Comments
 (0)