Skip to content

Commit ad72e41

Browse files
author
Federico Fissore
committed
Introducing CollectStdOut and CollectStdOutStderrExecutor, handy classes for executing external execs and collecting their outputs
1 parent b93bd8e commit ad72e41

File tree

5 files changed

+59
-9
lines changed

5 files changed

+59
-9
lines changed

arduino-core/src/processing/app/linux/Platform.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
import org.apache.commons.exec.Executor;
2727
import processing.app.PreferencesData;
2828
import processing.app.debug.TargetPackage;
29-
import processing.app.tools.ExternalProcessExecutor;
3029
import processing.app.legacy.PConstants;
30+
import processing.app.tools.CollectStdOutExecutor;
3131

3232
import java.io.*;
3333
import java.util.Map;
@@ -131,7 +131,7 @@ public String getName() {
131131
@Override
132132
public Map<String, Object> resolveDeviceAttachedTo(String serial, Map<String, TargetPackage> packages, String devicesListOutput) {
133133
ByteArrayOutputStream baos = new ByteArrayOutputStream();
134-
Executor executor = new ExternalProcessExecutor(baos);
134+
Executor executor = new CollectStdOutExecutor(baos);
135135

136136
try {
137137
CommandLine toDevicePath = CommandLine.parse("udevadm info -q path -n " + serial);

arduino-core/src/processing/app/macosx/Platform.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
import org.apache.commons.exec.CommandLine;
2828
import org.apache.commons.exec.Executor;
2929
import processing.app.debug.TargetPackage;
30-
import processing.app.tools.ExternalProcessExecutor;
3130
import processing.app.legacy.PApplet;
3231
import processing.app.legacy.PConstants;
32+
import processing.app.tools.CollectStdOutExecutor;
3333

3434
import javax.swing.*;
3535
import java.awt.*;
@@ -231,7 +231,7 @@ public Map<String, Object> resolveDeviceAttachedTo(String serial, Map<String, Ta
231231
@Override
232232
public String preListAllCandidateDevices() {
233233
ByteArrayOutputStream baos = new ByteArrayOutputStream();
234-
Executor executor = new ExternalProcessExecutor(baos);
234+
Executor executor = new CollectStdOutExecutor(baos);
235235

236236
try {
237237
CommandLine toDevicePath = CommandLine.parse("/usr/sbin/system_profiler SPUSBDataType");

arduino-core/src/processing/app/tools/ExternalProcessExecutor.java renamed to arduino-core/src/processing/app/tools/CollectStdOutExecutor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
/**
1111
* Handy process executor, collecting stdout into a given OutputStream
1212
*/
13-
public class ExternalProcessExecutor extends DefaultExecutor {
13+
public class CollectStdOutExecutor extends DefaultExecutor {
1414

15-
public ExternalProcessExecutor(final OutputStream os) {
15+
public CollectStdOutExecutor(final OutputStream stdout) {
1616
this.setStreamHandler(new ExecuteStreamHandler() {
1717
@Override
1818
public void setProcessInputStream(OutputStream outputStream) throws IOException {
@@ -27,7 +27,7 @@ public void setProcessOutputStream(InputStream inputStream) throws IOException {
2727
byte[] buf = new byte[4096];
2828
int bytes = -1;
2929
while ((bytes = inputStream.read(buf)) != -1) {
30-
os.write(buf, 0, bytes);
30+
stdout.write(buf, 0, bytes);
3131
}
3232
}
3333

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 and stderr into given OutputStreams
12+
*/
13+
public class CollectStdOutStdErrExecutor extends DefaultExecutor {
14+
15+
public CollectStdOutStdErrExecutor(final OutputStream stdout, final OutputStream stderr) {
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+
byte[] buf = new byte[4096];
24+
int bytes = -1;
25+
while ((bytes = inputStream.read(buf)) != -1) {
26+
stderr.write(buf, 0, bytes);
27+
}
28+
}
29+
30+
@Override
31+
public void setProcessOutputStream(InputStream inputStream) throws IOException {
32+
byte[] buf = new byte[4096];
33+
int bytes = -1;
34+
while ((bytes = inputStream.read(buf)) != -1) {
35+
stdout.write(buf, 0, bytes);
36+
}
37+
}
38+
39+
@Override
40+
public void start() throws IOException {
41+
}
42+
43+
@Override
44+
public void stop() {
45+
}
46+
});
47+
48+
}
49+
}

arduino-core/src/processing/app/windows/Platform.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
import processing.app.debug.TargetPackage;
3333
import processing.app.legacy.PApplet;
3434
import processing.app.legacy.PConstants;
35-
import processing.app.tools.ExternalProcessExecutor;
35+
import processing.app.tools.CollectStdOutExecutor;
36+
import processing.app.tools.CollectStdOutStdErrExecutor;
3637
import processing.app.windows.Registry.REGISTRY_ROOT_KEY;
3738

3839
import java.io.ByteArrayOutputStream;
@@ -338,7 +339,7 @@ public Map<String, Object> resolveDeviceAttachedTo(String serial, Map<String, Ta
338339
@Override
339340
public String preListAllCandidateDevices() {
340341
ByteArrayOutputStream baos = new ByteArrayOutputStream();
341-
Executor executor = new ExternalProcessExecutor(baos);
342+
Executor executor = new CollectStdOutExecutor(baos);
342343

343344
try {
344345
String listComPorts = new File(System.getProperty("user.dir"), "hardware/tools/listComPorts.exe").getCanonicalPath();

0 commit comments

Comments
 (0)