Skip to content

Commit

Permalink
8167252: Some of Charset.availableCharsets() does not contain itself
Browse files Browse the repository at this point in the history
Reviewed-by: bpb, alanb, iris, lancea, jpai
  • Loading branch information
naotoj committed Jun 15, 2023
1 parent 653a8d0 commit 3eeb681
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
8 changes: 7 additions & 1 deletion src/java.base/share/classes/sun/nio/cs/Unicode.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -45,6 +45,12 @@ public boolean contains(Charset cs) {
|| (cs instanceof UTF_16BE)
|| (cs instanceof UTF_16LE)
|| (cs instanceof UTF_16LE_BOM)
|| (cs instanceof CESU_8)
|| (cs instanceof UTF_32)
|| (cs instanceof UTF_32BE)
|| (cs instanceof UTF_32BE_BOM)
|| (cs instanceof UTF_32LE)
|| (cs instanceof UTF_32LE_BOM)
|| (cs.name().equals("GBK"))
|| (cs.name().equals("GB18030"))
|| (cs.name().equals("ISO-8859-2"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -51,7 +51,8 @@ public class EUC_JP_Open
public boolean contains(Charset cs) {
return ((cs.name().equals("US-ASCII"))
|| (cs instanceof JIS_X_0201)
|| (cs instanceof EUC_JP));
|| (cs instanceof EUC_JP)
|| (cs instanceof EUC_JP_Open));
}

public CharsetDecoder newDecoder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public boolean contains(Charset cs) {
return ((cs.name().equals("US-ASCII"))
|| (cs instanceof SJIS)
|| (cs instanceof EUC_JP)
|| (cs instanceof ISO2022_JP));
|| (cs instanceof ISO2022_JP)
|| (cs instanceof JISAutoDetect));
}

public boolean canEncode() {
Expand Down
27 changes: 25 additions & 2 deletions test/jdk/java/nio/charset/Charset/Contains.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -23,7 +23,7 @@

/* @test
* @summary Unit test for charset containment
* @bug 6798572
* @bug 6798572 8167252
* @modules jdk.charsets
*/

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

checkUTF();

containsSelfTest();
}

static void checkUTF() throws Exception {
Expand All @@ -103,6 +105,27 @@ static void checkUTF() throws Exception {
true);
}

/**
* Tests the assertion in the contains() method: "Every charset contains itself."
*/
static void containsSelfTest() {
boolean failed = false;

for (var entry : Charset.availableCharsets().entrySet()) {
Charset charset = entry.getValue();
boolean contains = charset.contains(charset);

System.out.println("Charset(" + charset.name() + ").contains(Charset(" + charset.name()
+ ")) returns " + contains);
if (!contains) {
failed = true;
}
}
if (failed) {
throw new RuntimeException("Charset.contains(itself) returns false for some charsets");
}
}

static String[] utfNames = {"utf-16",
"utf-8",
"utf-16le",
Expand Down

1 comment on commit 3eeb681

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.