|
12 | 12 | import java.awt.event.WindowEvent;
|
13 | 13 | import java.text.SimpleDateFormat;
|
14 | 14 | import java.util.Date;
|
| 15 | +import java.util.StringTokenizer; |
15 | 16 |
|
16 | 17 | import javax.swing.Box;
|
17 | 18 | import javax.swing.BoxLayout;
|
@@ -177,21 +178,43 @@ public void onSerialRateChange(ActionListener listener) {
|
177 | 178 |
|
178 | 179 | public void message(final String s) {
|
179 | 180 | SwingUtilities.invokeLater(new Runnable() {
|
| 181 | + // Pre-allocate all objects used for streaming data |
| 182 | + Date t = new Date(); |
| 183 | + String now; |
| 184 | + StringBuilder out = new StringBuilder(16384); |
| 185 | + |
180 | 186 | public void run() {
|
181 | 187 | if (addTimeStampBox.isSelected()) {
|
182 |
| - String[] lines = s.split("(?<=\\n)"); |
183 |
| - Document doc = textArea.getDocument(); |
184 |
| - for (String currentLine : lines) { |
185 |
| - try { |
186 |
| - if (doc.getLength() == 0 || ((int) doc.getText(doc.getLength() - 1, 1).charAt(0) == 10)) { |
187 |
| - textArea.append(logDateFormat.format(new Date()) + " -> " + currentLine); |
188 |
| - } else { |
189 |
| - textArea.append(currentLine); |
190 |
| - } |
191 |
| - } catch (BadLocationException e) { |
192 |
| - e.printStackTrace(); |
| 188 | + t.setTime(System.currentTimeMillis()); |
| 189 | + now = logDateFormat.format(t); |
| 190 | + out.setLength(0); |
| 191 | + |
| 192 | + boolean isStartingLine; |
| 193 | + try { |
| 194 | + Document doc = textArea.getDocument(); |
| 195 | + isStartingLine = doc.getLength() == 0 || ((int) doc.getText(doc.getLength() - 1, 1).charAt(0) == 10); |
| 196 | + } catch (BadLocationException e) { |
| 197 | + // Should not happen but... |
| 198 | + e.printStackTrace(); |
| 199 | + return; |
| 200 | + } |
| 201 | + |
| 202 | + StringTokenizer tokenizer = new StringTokenizer(s, "\n", true); |
| 203 | + while (tokenizer.hasMoreTokens()) { |
| 204 | + if (isStartingLine) { |
| 205 | + out.append(now); |
| 206 | + out.append(" -> "); |
| 207 | + } |
| 208 | + out.append(tokenizer.nextToken()); |
| 209 | + |
| 210 | + // Check if we have a "\n" token |
| 211 | + if (tokenizer.hasMoreTokens()) { |
| 212 | + out.append(tokenizer.nextToken()); |
| 213 | + isStartingLine = true; |
193 | 214 | }
|
194 | 215 | }
|
| 216 | + |
| 217 | + textArea.append(out.toString()); |
195 | 218 | } else {
|
196 | 219 | textArea.append(s);
|
197 | 220 | }
|
|
0 commit comments