Skip to content

Commit 59ea8b0

Browse files
committed
8167252: Some of Charset.availableCharsets() does not contain itself
Backport-of: 3eeb681a0de87baa12b6eac5966e7f707b76c8bf
1 parent 512d6f9 commit 59ea8b0

File tree

4 files changed

+37
-6
lines changed

4 files changed

+37
-6
lines changed

src/java.base/share/classes/sun/nio/cs/Unicode.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2023, 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
@@ -45,6 +45,12 @@ public boolean contains(Charset cs) {
4545
|| (cs instanceof UTF_16BE)
4646
|| (cs instanceof UTF_16LE)
4747
|| (cs instanceof UTF_16LE_BOM)
48+
|| (cs instanceof CESU_8)
49+
|| (cs instanceof UTF_32)
50+
|| (cs instanceof UTF_32BE)
51+
|| (cs instanceof UTF_32BE_BOM)
52+
|| (cs instanceof UTF_32LE)
53+
|| (cs instanceof UTF_32LE_BOM)
4854
|| (cs.name().equals("GBK"))
4955
|| (cs.name().equals("GB18030"))
5056
|| (cs.name().equals("ISO-8859-2"))

src/jdk.charsets/share/classes/sun/nio/cs/ext/EUC_JP_Open.java.template

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2023, 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
@@ -51,7 +51,8 @@ public class EUC_JP_Open
5151
public boolean contains(Charset cs) {
5252
return ((cs.name().equals("US-ASCII"))
5353
|| (cs instanceof JIS_X_0201)
54-
|| (cs instanceof EUC_JP));
54+
|| (cs instanceof EUC_JP)
55+
|| (cs instanceof EUC_JP_Open));
5556
}
5657

5758
public CharsetDecoder newDecoder() {

src/jdk.charsets/share/classes/sun/nio/cs/ext/JISAutoDetect.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ public boolean contains(Charset cs) {
5959
return ((cs.name().equals("US-ASCII"))
6060
|| (cs instanceof SJIS)
6161
|| (cs instanceof EUC_JP)
62-
|| (cs instanceof ISO2022_JP));
62+
|| (cs instanceof ISO2022_JP)
63+
|| (cs instanceof JISAutoDetect));
6364
}
6465

6566
public boolean canEncode() {

test/jdk/java/nio/charset/Charset/Contains.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2023, 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
@@ -23,7 +23,7 @@
2323

2424
/* @test
2525
* @summary Unit test for charset containment
26-
* @bug 6798572
26+
* @bug 6798572 8167252
2727
* @modules jdk.charsets
2828
*/
2929

@@ -93,6 +93,8 @@ public static void main(String[] args) throws Exception {
9393
ck(cp1252, cp1252, true);
9494

9595
checkUTF();
96+
97+
containsSelfTest();
9698
}
9799

98100
static void checkUTF() throws Exception {
@@ -103,6 +105,27 @@ static void checkUTF() throws Exception {
103105
true);
104106
}
105107

108+
/**
109+
* Tests the assertion in the contains() method: "Every charset contains itself."
110+
*/
111+
static void containsSelfTest() {
112+
boolean failed = false;
113+
114+
for (var entry : Charset.availableCharsets().entrySet()) {
115+
Charset charset = entry.getValue();
116+
boolean contains = charset.contains(charset);
117+
118+
System.out.println("Charset(" + charset.name() + ").contains(Charset(" + charset.name()
119+
+ ")) returns " + contains);
120+
if (!contains) {
121+
failed = true;
122+
}
123+
}
124+
if (failed) {
125+
throw new RuntimeException("Charset.contains(itself) returns false for some charsets");
126+
}
127+
}
128+
106129
static String[] utfNames = {"utf-16",
107130
"utf-8",
108131
"utf-16le",

0 commit comments

Comments
 (0)