JDK-4786884 : (cs) Charset methods and constructors should reject empty names
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 1.4.0,1.4.2,5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: generic,solaris_2.6,solaris_8
  • CPU: generic,sparc
  • Submitted: 2002-12-02
  • Updated: 2017-06-29
  • Resolved: 2003-12-19
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
5.0 b32Fixed
Related Reports
Duplicate :  
Duplicate :  
Duplicate :  
Duplicate :  
Relates :  
Description
Name: auR10023			Date: 12/02/2002

Javadoc for java.nio.charset.Charset says:
...
 The empty string is not a legal charset name.
...
So, java.nio.charset.Charset.forName(String)  should throw 
IllegalCharsetNameException with empty string.

Here is the example:

-------test.java---------
import java.nio.charset.*;

public class test {
    public static void main (String [] args) {
        try {
            Charset.forName("");
            System.out.println("IllegalCharsetNameException expected");
            return;
        } catch(IllegalCharsetNameException e) {                                        } catch (UnsupportedCharsetException e) {
            System.out.println("Unexpected " + e);
            return; 
        }       
        System.out.println("OKAY");
    }
}

Here is the result
#java -version

java version "1.4.2-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-beta-b07)
Java HotSpot(TM) Client VM (build 1.4.2-beta-b07, mixed mode)

#java test

Unexpected java.nio.charset.UnsupportedCharsetException:

======================================================================

See also  4786886, which reports the same problem for the isSupported() method.

======================================================================

Name: auR10023 Date: 03/14/2003
 
java.nio.charset.Charset(String canonicalName, String[] aliases) should
throw IllegalCharsetNameException with empty canonicalName.
 
Here is the example:
 
-------test.java---------
 
import java.nio.charset.*;
 
public class test {
    static class TestCharset extends Charset {
        public TestCharset(String canonicalName, String[] aliases) {
            super(canonicalName, aliases);
        }
        public CharsetDecoder newDecoder() {
           return null;
        }
 
        public CharsetEncoder newEncoder() {
           return null;
        }
 
        public boolean contains(Charset cs) {
            return false;
        }
 
    }
 
    public static void main (String [] args) {
        try {
            new TestCharset("", null);
            System.out.println("IllegalCharsetNameException should be thrown");
        } catch (IllegalCharsetNameException e) {
            System.out.println( "OKAY");
        }
    }
}
 
Here is the result
#java -version
 
java version "1.4.2-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-beta-b16)
Java HotSpot(TM) Client VM (build 1.4.2-beta-b16, mixed mode)
 
#java test
 
IllegalCharsetNameException should be thrown
 
======================================================================

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: tiger-beta FIXED IN: tiger-beta INTEGRATED IN: tiger-b32 tiger-beta VERIFIED IN: tiger-b41
14-06-2004

SUGGESTED FIX *** /tmp/geta25335 Mon Dec 2 12:02:26 2002 --- Charset.java Mon Dec 2 11:51:07 2002 *************** *** 375,380 **** --- 375,382 ---- private static Charset lookup(String charsetName) { if (charsetName == null) throw new IllegalArgumentException("Null charset name"); + if (charsetName.equals("")) + throw new IllegalArgumentException("Empty charset name"); Object[] ca = cache; if ((ca != null) && ca[0].equals(charsetName)) return (Charset)ca[1];
11-06-2004

EVALUATION The submitter is correct. -- ###@###.### 2002/12/2
12-10-0168