Skip to content

Commit dc831aa

Browse files
committedJun 3, 2024
8328953: JEditorPane.read throws ChangedCharSetException
Backport-of: 245514da51ef77757f530317e079a3f58370a0be
1 parent 4b3df73 commit dc831aa

File tree

2 files changed

+102
-26
lines changed

2 files changed

+102
-26
lines changed
 

‎src/java.desktop/share/classes/javax/swing/JEditorPane.java

+28-26
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -619,35 +619,37 @@ void read(InputStream in, Document doc) throws IOException {
619619
String charset = (String) getClientProperty("charset");
620620
try(Reader r = (charset != null) ? new InputStreamReader(in, charset) :
621621
new InputStreamReader(in)) {
622-
kit.read(r, doc, 0);
623-
} catch (BadLocationException e) {
624-
throw new IOException(e.getMessage());
625-
} catch (ChangedCharSetException changedCharSetException) {
626-
String charSetSpec = changedCharSetException.getCharSetSpec();
627-
if (changedCharSetException.keyEqualsCharSet()) {
628-
putClientProperty("charset", charSetSpec);
629-
} else {
630-
setCharsetFromContentTypeParameters(charSetSpec);
631-
}
632622
try {
633-
in.reset();
634-
} catch (IOException exception) {
635-
//mark was invalidated
636-
in.close();
637-
URL url = (URL)doc.getProperty(Document.StreamDescriptionProperty);
638-
if (url != null) {
639-
URLConnection conn = url.openConnection();
640-
in = conn.getInputStream();
623+
kit.read(r, doc, 0);
624+
} catch (BadLocationException e) {
625+
throw new IOException(e.getMessage());
626+
} catch (ChangedCharSetException changedCharSetException) {
627+
String charSetSpec = changedCharSetException.getCharSetSpec();
628+
if (changedCharSetException.keyEqualsCharSet()) {
629+
putClientProperty("charset", charSetSpec);
641630
} else {
642-
//there is nothing we can do to recover stream
643-
throw changedCharSetException;
631+
setCharsetFromContentTypeParameters(charSetSpec);
644632
}
633+
try {
634+
in.reset();
635+
} catch (IOException exception) {
636+
//mark was invalidated
637+
in.close();
638+
URL url = (URL)doc.getProperty(Document.StreamDescriptionProperty);
639+
if (url != null) {
640+
URLConnection conn = url.openConnection();
641+
in = conn.getInputStream();
642+
} else {
643+
//there is nothing we can do to recover stream
644+
throw changedCharSetException;
645+
}
646+
}
647+
try {
648+
doc.remove(0, doc.getLength());
649+
} catch (BadLocationException e) {}
650+
doc.putProperty("IgnoreCharsetDirective", Boolean.valueOf(true));
651+
read(in, doc);
645652
}
646-
try {
647-
doc.remove(0, doc.getLength());
648-
} catch (BadLocationException e) {}
649-
doc.putProperty("IgnoreCharsetDirective", Boolean.valueOf(true));
650-
read(in, doc);
651653
}
652654
}
653655

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import java.io.ByteArrayInputStream;
25+
import java.io.IOException;
26+
import java.nio.charset.Charset;
27+
28+
import javax.swing.JEditorPane;
29+
import javax.swing.text.BadLocationException;
30+
import javax.swing.text.Document;
31+
import javax.swing.text.Element;
32+
33+
/*
34+
* @test
35+
* @bug 8328953
36+
* @summary Verifies JEditorPane.read doesn't throw ChangedCharSetException
37+
but handles it and reads HTML in the specified encoding
38+
* @run main EditorPaneCharset
39+
*/
40+
41+
public final class EditorPaneCharset {
42+
private static final String CYRILLIC_TEXT =
43+
"\u041F\u0440\u0438\u0432\u0435\u0442, \u043C\u0438\u0440!";
44+
private static final String HTML_CYRILLIC =
45+
"<html lang=\"ru\">\n" +
46+
"<head>\n" +
47+
" <meta http-equiv=\"Content-Type\" " +
48+
" content=\"text/html; charset=windows-1251\">\n" +
49+
"</head><body>\n" +
50+
"<p>" + CYRILLIC_TEXT + "</p>\n" +
51+
"</body></html>\n";
52+
53+
public static void main(String[] args) throws IOException, BadLocationException {
54+
JEditorPane editorPane = new JEditorPane();
55+
editorPane.setContentType("text/html");
56+
Document document = editorPane.getDocument();
57+
58+
// Shouldn't throw ChangedCharSetException
59+
editorPane.read(
60+
new ByteArrayInputStream(
61+
HTML_CYRILLIC.getBytes(
62+
Charset.forName("windows-1251"))),
63+
document);
64+
65+
Element root = document.getDefaultRootElement();
66+
Element body = root.getElement(1);
67+
Element p = body.getElement(0);
68+
String pText = document.getText(p.getStartOffset(),
69+
p.getEndOffset() - p.getStartOffset() - 1);
70+
if (!CYRILLIC_TEXT.equals(pText)) {
71+
throw new RuntimeException("Text doesn't match");
72+
}
73+
}
74+
}

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on Jun 3, 2024

@openjdk-notifier[bot]
Please sign in to comment.