From b97f756142dfb7a2a4d24aa19939bfd90bd280e7 Mon Sep 17 00:00:00 2001 From: lorol Date: Mon, 21 Dec 2020 21:57:24 -0500 Subject: [PATCH] Bugfixes Changed sysExec() to be based on: https://www.infoworld.com/article/2071275/when-runtime-exec---won-t.html The original way hangs on long uploading. --- src/ESP32FS.java | 98 +++++++++++++++++++++++++++------------------ src/bin/placeholder | 0 src/make_win.bat | 4 +- 3 files changed, 61 insertions(+), 41 deletions(-) delete mode 100644 src/bin/placeholder diff --git a/src/ESP32FS.java b/src/ESP32FS.java index 86a8f5a..e7d70a1 100644 --- a/src/ESP32FS.java +++ b/src/ESP32FS.java @@ -23,14 +23,10 @@ package com.esp32.mkspiffs; -import java.io.File; -import java.io.FileReader; -import java.io.BufferedReader; -import java.io.InputStreamReader; -import java.io.IOException; +import java.util.*; +import java.io.*; import java.text.SimpleDateFormat; -import java.util.Date; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import javax.swing.JOptionPane; @@ -51,6 +47,32 @@ import cc.arduino.files.DeleteFilesOnShutdown; +/** + * Taken from https://www.infoworld.com/article/2071275/when-runtime-exec---won-t.html?page=3 + */ +class StreamGobbler extends Thread { + InputStream is; + String type; + + StreamGobbler(InputStream is, String type) { + this.is = is; + this.type = type; + } + + public void run() { + try { + InputStreamReader isr = new InputStreamReader(is); + BufferedReader br = new BufferedReader(isr); + String line=null; + while ( (line = br.readLine()) != null) + System.out.println(type + ">" + line); + } catch (IOException ioe) { + ioe.printStackTrace(); + } + } +} + + /** * Example Tools menu entry. */ @@ -71,31 +93,26 @@ public String getMenuTitle() { private int listenOnProcess(String[] arguments){ try { - final Process p = ProcessUtils.exec(arguments); - Thread thread = new Thread() { - public void run() { - try { - InputStreamReader reader = new InputStreamReader(p.getInputStream()); - int c; - while ((c = reader.read()) != -1) - System.out.print((char) c); - reader.close(); - - reader = new InputStreamReader(p.getErrorStream()); - while ((c = reader.read()) != -1) - System.err.print((char) c); - reader.close(); - } catch (Exception e){} - } - }; - thread.start(); - int res = p.waitFor(); - thread.join(); - return res; + Runtime rt = Runtime.getRuntime(); + Process proc = rt.exec(arguments); + // any error message? + StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "E"); + + // any output? + StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "O"); + + // kick them off + errorGobbler.start(); + outputGobbler.start(); + + // any error??? + int exitVal = proc.waitFor(); + + return exitVal; } catch (Exception e){ return -1; } - } + } private void sysExec(final String[] arguments){ Thread thread = new Thread() { @@ -328,9 +345,12 @@ private void createAndUpload(){ isNetwork = true; espota = new File(platform.getFolder()+"/tools", espotaCmd); if(!espota.exists() || !espota.isFile()){ - System.err.println(); - editor.statusError(typefs + " Error: espota not found!"); - return; + espota = new File(platform.getFolder()+"/tools", "espota.py"); //fall-back to .py + if(!espota.exists() || !espota.isFile()){ + System.err.println(); + editor.statusError(typefs + " Error: espota not found!"); + return; + } } System.out.println("espota : "+espota.getAbsolutePath()); System.out.println(); @@ -354,11 +374,10 @@ private void createAndUpload(){ } } } + System.out.println("esptool : "+esptool.getAbsolutePath()); + System.out.println(); } - System.out.println("esptool : "+esptool.getAbsolutePath()); - System.out.println(); - - + //load a list of all files int fileCount = 0; File dataFolder = new File(editor.getSketch().getFolder(), "data"); @@ -426,9 +445,10 @@ private void createAndUpload(){ if(isNetwork){ System.out.println("[" + typefs + "] IP : "+serialPort); - System.out.println(); + System.out.println("Running: " + espota.getAbsolutePath() + " -i " + serialPort + " -p 3232 -s -f " + imagePath); + System.out.println(); if(espota.getAbsolutePath().endsWith(".py")) - sysExec(new String[]{pythonCmd, espota.getAbsolutePath(), "-i", serialPort, "-p", "3232", "-s", "-f", imagePath}); + sysExec(new String[]{pythonCmd, espota.getAbsolutePath(), "-i", serialPort, "-p", "3232", "-s", "-f", imagePath}); // other flags , "-d", "-r", "-t", "50" else sysExec(new String[]{espota.getAbsolutePath(), "-i", serialPort, "-p", "3232", "-s", "-f", imagePath}); } else { @@ -507,9 +527,9 @@ private void eraseFlash(){ } } } + System.out.println("esptool : "+esptool.getAbsolutePath()); + System.out.println(); } - System.out.println("esptool : "+esptool.getAbsolutePath()); - System.out.println(); Object[] options = { "Yes", "No" }; String title = "Erase All Flash"; diff --git a/src/bin/placeholder b/src/bin/placeholder deleted file mode 100644 index e69de29..0000000 diff --git a/src/make_win.bat b/src/make_win.bat index 09a6078..0333c50 100644 --- a/src/make_win.bat +++ b/src/make_win.bat @@ -1,6 +1,6 @@ del bin\*.jar rd /S /Q bin\com -"C:\Program Files (x86)\Java\jdk1.8.0_152\bin\javac.exe" -target 1.8 -cp ".;arduino-core.jar;commons-codec-1.7.jar;pde.jar" -d bin ESP32FS.java +javac.exe -target 1.8 -cp ".;arduino-core.jar;commons-codec-1.7.jar;pde.jar" -d bin ESP32FS.java cd bin -"C:\Program Files (x86)\Java\jdk1.8.0_152\bin\jar.exe" cvfM esp32fs.jar * +jar.exe cvfM esp32fs.jar * pause