Skip to content

Commit a0d1a43

Browse files
committed
bugfix #625, cache results of HTMLDocument.getText() and enable 'mutliByte' property to use faster BreakIterator; use spaces instead of tabs to format the text
1 parent 117858e commit a0d1a43

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

visualvm/threaddump/src/com/sun/tools/visualvm/threaddump/impl/ThreadDumpView.java

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@
4646
import javax.swing.JLabel;
4747
import javax.swing.JPanel;
4848
import javax.swing.SwingConstants;
49+
import javax.swing.text.BadLocationException;
50+
import javax.swing.text.Document;
51+
import javax.swing.text.Segment;
52+
import javax.swing.text.html.HTMLDocument;
53+
import javax.swing.text.html.HTMLEditorKit;
54+
import javax.swing.text.html.StyleSheet;
4955
import org.openide.util.NbBundle;
5056
import org.openide.util.RequestProcessor;
5157

@@ -130,7 +136,7 @@ private void initComponents() {
130136
}
131137

132138
private static String htmlize(String value) {
133-
return value.replace("&", "&amp;").replace("<", "&lt;"); // NOI18N
139+
return value.replace("&", "&amp;").replace("<", "&lt;").replace("\t", " "); // NOI18N
134140
}
135141

136142
private static String transform(String value) {
@@ -164,6 +170,7 @@ public void run() {
164170
}
165171
try {
166172
HTMLTextArea area = new HTMLTextArea();
173+
area.setEditorKit(new CustomHtmlEditorKit());
167174
area.setForeground(new Color(0xcc, 0x33, 0));
168175
area.setText("<pre>" + transform(htmlize(new String(data, "UTF-8"))) + "</pre>"); // NOI18N
169176
area.setCaretPosition(0);
@@ -192,5 +199,59 @@ public void run() {
192199
}
193200

194201
}
202+
203+
private static class CustomHtmlEditorKit extends HTMLEditorKit {
204+
205+
@Override
206+
public Document createDefaultDocument() {
207+
StyleSheet styles = getStyleSheet();
208+
StyleSheet ss = new StyleSheet();
209+
210+
ss.addStyleSheet(styles);
211+
212+
HTMLDocument doc = new CustomHTMLDocument(ss);
213+
doc.setParser(getParser());
214+
doc.setAsynchronousLoadPriority(4);
215+
doc.setTokenThreshold(100);
216+
return doc;
217+
}
218+
}
219+
220+
private static class CustomHTMLDocument extends HTMLDocument {
221+
private static final int CACHE_BOUNDARY = 1000;
222+
private char[] segArray;
223+
private int segOffset;
224+
private int segCount;
225+
private boolean segPartialReturn;
226+
private int lastOffset;
227+
private int lastLength;
228+
229+
private CustomHTMLDocument(StyleSheet ss) {
230+
super(ss);
231+
lastOffset = -1;
232+
lastLength = -1;
233+
putProperty("multiByte", Boolean.TRUE); // NOI18N
234+
}
235+
236+
@Override
237+
public void getText(int offset, int length, Segment txt) throws BadLocationException {
238+
if (lastOffset == offset && lastLength == length) {
239+
txt.array = segArray;
240+
txt.offset = segOffset;
241+
txt.count = segCount;
242+
txt.setPartialReturn(segPartialReturn);
243+
return;
244+
}
245+
super.getText(offset, length, txt);
246+
if (length > CACHE_BOUNDARY || lastLength <= CACHE_BOUNDARY) {
247+
segArray = txt.array;
248+
segOffset = txt.offset;
249+
segCount = txt.count;
250+
segPartialReturn = txt.isPartialReturn();
251+
lastOffset = offset;
252+
lastLength = length;
253+
}
254+
}
255+
}
195256

196257
}

0 commit comments

Comments
 (0)