diff --git a/arduino-core/src/processing/app/linux/Platform.java b/arduino-core/src/processing/app/linux/Platform.java index 57af15d15..59e5f50d8 100644 --- a/arduino-core/src/processing/app/linux/Platform.java +++ b/arduino-core/src/processing/app/linux/Platform.java @@ -26,8 +26,8 @@ import org.apache.commons.exec.CommandLine; import org.apache.commons.exec.Executor; import processing.app.PreferencesData; import processing.app.debug.TargetPackage; -import processing.app.tools.ExternalProcessExecutor; import processing.app.legacy.PConstants; +import processing.app.tools.CollectStdOutExecutor; import java.io.*; import java.util.Map; @@ -131,7 +131,7 @@ public class Platform extends processing.app.Platform { @Override public Map resolveDeviceAttachedTo(String serial, Map packages, String devicesListOutput) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - Executor executor = new ExternalProcessExecutor(baos); + Executor executor = new CollectStdOutExecutor(baos); try { CommandLine toDevicePath = CommandLine.parse("udevadm info -q path -n " + serial); diff --git a/arduino-core/src/processing/app/macosx/Platform.java b/arduino-core/src/processing/app/macosx/Platform.java index 433c7b9e7..e52f86cf2 100644 --- a/arduino-core/src/processing/app/macosx/Platform.java +++ b/arduino-core/src/processing/app/macosx/Platform.java @@ -27,9 +27,9 @@ import com.apple.eio.FileManager; import org.apache.commons.exec.CommandLine; import org.apache.commons.exec.Executor; import processing.app.debug.TargetPackage; -import processing.app.tools.ExternalProcessExecutor; import processing.app.legacy.PApplet; import processing.app.legacy.PConstants; +import processing.app.tools.CollectStdOutExecutor; import javax.swing.*; import java.awt.*; @@ -231,7 +231,7 @@ public class Platform extends processing.app.Platform { @Override public String preListAllCandidateDevices() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - Executor executor = new ExternalProcessExecutor(baos); + Executor executor = new CollectStdOutExecutor(baos); try { CommandLine toDevicePath = CommandLine.parse("/usr/sbin/system_profiler SPUSBDataType"); diff --git a/arduino-core/src/processing/app/tools/ExternalProcessExecutor.java b/arduino-core/src/processing/app/tools/CollectStdOutExecutor.java similarity index 85% rename from arduino-core/src/processing/app/tools/ExternalProcessExecutor.java rename to arduino-core/src/processing/app/tools/CollectStdOutExecutor.java index 98c2d0695..b2b5f3559 100644 --- a/arduino-core/src/processing/app/tools/ExternalProcessExecutor.java +++ b/arduino-core/src/processing/app/tools/CollectStdOutExecutor.java @@ -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); } } diff --git a/arduino-core/src/processing/app/tools/CollectStdOutStdErrExecutor.java b/arduino-core/src/processing/app/tools/CollectStdOutStdErrExecutor.java new file mode 100644 index 000000000..65bc3efaa --- /dev/null +++ b/arduino-core/src/processing/app/tools/CollectStdOutStdErrExecutor.java @@ -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() { + } + }); + + } +} diff --git a/arduino-core/src/processing/app/windows/Platform.java b/arduino-core/src/processing/app/windows/Platform.java index e7274bfb0..d74b51209 100644 --- a/arduino-core/src/processing/app/windows/Platform.java +++ b/arduino-core/src/processing/app/windows/Platform.java @@ -32,7 +32,8 @@ import processing.app.PreferencesData; import processing.app.debug.TargetPackage; import processing.app.legacy.PApplet; import processing.app.legacy.PConstants; -import processing.app.tools.ExternalProcessExecutor; +import processing.app.tools.CollectStdOutExecutor; +import processing.app.tools.CollectStdOutStdErrExecutor; import processing.app.windows.Registry.REGISTRY_ROOT_KEY; import java.io.ByteArrayOutputStream; @@ -338,7 +339,7 @@ public class Platform extends processing.app.Platform { @Override public String preListAllCandidateDevices() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - Executor executor = new ExternalProcessExecutor(baos); + Executor executor = new CollectStdOutExecutor(baos); try { String listComPorts = new File(System.getProperty("user.dir"), "hardware/tools/listComPorts.exe").getCanonicalPath();