Skip to content

Commit afc2cc4

Browse files
Federico Fissorelmihalkovic
Federico Fissore
authored andcommitted
Stored some regexps in static finals, given names to threads, and slightly
optimized ConsoleOutputStream
1 parent f23a3be commit afc2cc4

File tree

10 files changed

+47
-43
lines changed

10 files changed

+47
-43
lines changed

app/src/cc/arduino/ConsoleOutputStream.java

+13-28
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,20 @@ public class ConsoleOutputStream extends ByteArrayOutputStream {
5454

5555
private final SimpleAttributeSet attributes;
5656
private final PrintStream printStream;
57-
private final StringBuilder buffer;
5857
private final Timer timer;
58+
5959
private volatile EditorConsole editorConsole;
60+
private volatile boolean newLinePrinted;
6061

6162
public ConsoleOutputStream(SimpleAttributeSet attributes, PrintStream printStream) {
6263
this.attributes = attributes;
6364
this.printStream = printStream;
64-
this.buffer = new StringBuilder();
65+
this.newLinePrinted = false;
6566

66-
this.timer = new Timer(100, (e) -> {
67-
if (editorConsole != null) {
67+
this.timer = new Timer(500, (e) -> {
68+
if (editorConsole != null && newLinePrinted) {
6869
editorConsole.scrollDown();
70+
newLinePrinted = false;
6971
}
7072
});
7173
timer.setRepeats(false);
@@ -76,41 +78,24 @@ public void setCurrentEditorConsole(EditorConsole console) {
7678
}
7779

7880
public synchronized void flush() {
79-
String message = toString();
81+
String text = toString();
8082

81-
if (message.length() == 0) {
83+
if (text.length() == 0) {
8284
return;
8385
}
8486

85-
handleAppend(message);
87+
printStream.print(text);
88+
printInConsole(text);
8689

8790
reset();
8891
}
8992

90-
private void handleAppend(String message) {
91-
resetBufferIfDocumentEmpty();
92-
93-
buffer.append(message);
94-
95-
clearBuffer();
96-
}
97-
98-
private void resetBufferIfDocumentEmpty() {
99-
if (editorConsole != null && editorConsole.isEmpty()) {
100-
buffer.setLength(0);
101-
}
102-
}
103-
104-
private void clearBuffer() {
105-
String line = buffer.toString();
106-
buffer.setLength(0);
107-
108-
printStream.print(line);
109-
93+
private void printInConsole(String text) {
94+
newLinePrinted = newLinePrinted || text.contains("\n");
11095
if (editorConsole != null) {
11196
SwingUtilities.invokeLater(() -> {
11297
try {
113-
editorConsole.insertString(line, attributes);
98+
editorConsole.insertString(text, attributes);
11499
} catch (BadLocationException ble) {
115100
//ignore
116101
}

app/src/cc/arduino/contributions/libraries/ui/LibraryManagerUI.java

+3
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ protected void onUpdatePressed() {
207207
setProgressVisible(false, "");
208208
}
209209
});
210+
installerThread.setName("LibraryManager Update Thread");
210211
installerThread.setUncaughtExceptionHandler(new InstallerJDialogUncaughtExceptionHandler(this, noConnectionErrorMessage));
211212
installerThread.start();
212213
}
@@ -225,6 +226,7 @@ public void onInstallPressed(final ContributedLibrary lib, final ContributedLibr
225226
setProgressVisible(false, "");
226227
}
227228
});
229+
installerThread.setName("LibraryManager Installer Thread");
228230
installerThread.setUncaughtExceptionHandler(new InstallerJDialogUncaughtExceptionHandler(this, noConnectionErrorMessage));
229231
installerThread.start();
230232
}
@@ -252,6 +254,7 @@ public void onRemovePressed(final ContributedLibrary lib) {
252254
setProgressVisible(false, "");
253255
}
254256
});
257+
installerThread.setName("LibraryManager Remove Thread");
255258
installerThread.setUncaughtExceptionHandler(new InstallerJDialogUncaughtExceptionHandler(this, noConnectionErrorMessage));
256259
installerThread.start();
257260
}

app/src/cc/arduino/contributions/packages/ui/ContributionManagerUI.java

+3
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ public void onUpdatePressed() {
147147
setProgressVisible(false, "");
148148
}
149149
});
150+
installerThread.setName("ContributionManager Update Thread");
150151
installerThread.setUncaughtExceptionHandler(new InstallerJDialogUncaughtExceptionHandler(this, noConnectionErrorMessage));
151152
installerThread.start();
152153
}
@@ -171,6 +172,7 @@ public void onInstallPressed(final ContributedPlatform platformToInstall, final
171172
}
172173
}
173174
});
175+
installerThread.setName("ContributionManager Install Thread");
174176
installerThread.setUncaughtExceptionHandler(new InstallerJDialogUncaughtExceptionHandler(this, noConnectionErrorMessage));
175177
installerThread.start();
176178
}
@@ -196,6 +198,7 @@ public void onRemovePressed(final ContributedPlatform platform, boolean showWarn
196198
setProgressVisible(false, "");
197199
}
198200
});
201+
installerThread.setName("ContributionManager Remove Thread");
199202
installerThread.setUncaughtExceptionHandler(new InstallerJDialogUncaughtExceptionHandler(this, noConnectionErrorMessage));
200203
installerThread.start();
201204
}

app/src/processing/app/Base.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ static public void main(String args[]) throws Exception {
141141
}
142142

143143
static public void guardedMain(String args[]) throws Exception {
144-
Runtime.getRuntime().addShutdownHook(new Thread(DeleteFilesOnShutdown.INSTANCE));
144+
Thread deleteFilesOnShutdownThread = new Thread(DeleteFilesOnShutdown.INSTANCE);
145+
deleteFilesOnShutdownThread.setName("DeleteFilesOnShutdown");
146+
Runtime.getRuntime().addShutdownHook(deleteFilesOnShutdownThread);
145147

146148
BaseNoGui.initLogger();
147149

arduino-core/src/cc/arduino/Compiler.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import java.util.ArrayList;
5252
import java.util.Collections;
5353
import java.util.List;
54+
import java.util.regex.Pattern;
5455
import java.util.stream.Collectors;
5556
import java.util.stream.Stream;
5657

@@ -105,6 +106,8 @@ enum BuilderAction {
105106
}
106107
}
107108

109+
private static final Pattern ERROR_FORMAT = Pattern.compile("(.+\\.\\w+):(\\d+)(:\\d+)*:\\s*error:\\s*(.*)\\s*", Pattern.MULTILINE | Pattern.DOTALL);
110+
108111
private final String pathToSketch;
109112
private final SketchData sketch;
110113
private final String buildPath;
@@ -427,6 +430,7 @@ private void exec(String[] command) throws RunnerException {
427430
@Override
428431
protected Thread createPump(InputStream is, OutputStream os, boolean closeWhenExhausted) {
429432
final Thread result = new Thread(new MyStreamPumper(is, Compiler.this));
433+
result.setName("MyStreamPumper Thread");
430434
result.setDaemon(true);
431435
return result;
432436

@@ -501,8 +505,7 @@ public void message(String s) {
501505
}
502506
}
503507

504-
String errorFormat = "(.+\\.\\w+):(\\d+)(:\\d+)*:\\s*error:\\s*(.*)\\s*";
505-
String[] pieces = PApplet.match(s, errorFormat);
508+
String[] pieces = PApplet.match(s, ERROR_FORMAT);
506509

507510
if (pieces != null) {
508511
String error = pieces[pieces.length - 1], msg = "";

arduino-core/src/cc/arduino/i18n/ExternalProcessOutputParser.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,13 @@
3232
import java.io.UnsupportedEncodingException;
3333
import java.net.URLDecoder;
3434
import java.util.*;
35+
import java.util.regex.Pattern;
3536
import java.util.stream.Collectors;
3637

3738
public class ExternalProcessOutputParser {
3839

40+
private static final Pattern SPLIT = Pattern.compile(" \\|\\|\\| ");
41+
3942
public Map<String, Object> parse(String s) {
4043
if (!s.startsWith("===")) {
4144
throw new IllegalArgumentException(s);
@@ -45,7 +48,7 @@ public Map<String, Object> parse(String s) {
4548

4649
Map<String, Object> output = new HashMap<>();
4750

48-
String[] parts = s.split(" \\|\\|\\| ");
51+
String[] parts = SPLIT.split(s);
4952

5053
output.put("msg", parts[0]);
5154
output.put("args", parseArgs(parts[1]));

arduino-core/src/cc/arduino/packages/DiscoveryManager.java

+7-9
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,16 @@ public DiscoveryManager() {
5656
}
5757
}
5858

59-
Thread closeHook = new Thread(new Runnable() {
60-
@Override
61-
public void run() {
62-
for (Discovery d : discoverers) {
63-
try {
64-
d.stop();
65-
} catch (Exception e) {
66-
e.printStackTrace(); //just printing as the JVM is terminating
67-
}
59+
Thread closeHook = new Thread(() -> {
60+
for (Discovery d : discoverers) {
61+
try {
62+
d.stop();
63+
} catch (Exception e) {
64+
e.printStackTrace(); //just printing as the JVM is terminating
6865
}
6966
}
7067
});
68+
closeHook.setName("DiscoveryManager closeHook");
7169
Runtime.getRuntime().addShutdownHook(closeHook);
7270
}
7371

arduino-core/src/processing/app/BaseNoGui.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,9 @@ static public void main(String args[]) throws Exception {
757757
}
758758
System.setProperty("java.net.useSystemProxies", "true");
759759

760-
Runtime.getRuntime().addShutdownHook(new Thread(DeleteFilesOnShutdown.INSTANCE));
760+
Thread deleteFilesOnShutdownThread = new Thread(DeleteFilesOnShutdown.INSTANCE);
761+
deleteFilesOnShutdownThread.setName("DeleteFilesOnShutdown");
762+
Runtime.getRuntime().addShutdownHook(deleteFilesOnShutdownThread);
761763

762764
initPlatform();
763765

arduino-core/src/processing/app/debug/MessageSiphon.java

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public MessageSiphon(InputStream stream, MessageConsumer consumer, int lineTimeo
5454
this.lineTimeout = lineTimeout;
5555

5656
thread = new Thread(this);
57+
thread.setName("MessageSiphon");
5758
// don't set priority too low, otherwise exceptions won't
5859
// bubble up in time (i.e. compile errors have a weird delay)
5960
//thread.setPriority(Thread.MIN_PRIORITY);

arduino-core/src/processing/app/legacy/PApplet.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,11 @@ static public String[][] matchAll(String what, String regexp) {
497497
*/
498498
static public String[] match(String what, String regexp) {
499499
Pattern p = Pattern.compile(regexp, Pattern.MULTILINE | Pattern.DOTALL);
500-
Matcher m = p.matcher(what);
500+
return match(what, p);
501+
}
502+
503+
static public String[] match(String what, Pattern pattern) {
504+
Matcher m = pattern.matcher(what);
501505
if (m.find()) {
502506
int count = m.groupCount() + 1;
503507
String[] groups = new String[count];

0 commit comments

Comments
 (0)