JDK-4424300 : (cs) jdk1.4.0beta-b54 java.lang.String: getBytes("ASCII") returns wrong array of
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 1.4.0
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 2001-03-12
  • Updated: 2001-07-19
  • Resolved: 2001-07-19
Related Reports
Duplicate :  
Description

Name: ooR10006			Date: 03/11/2001


jdk1.4.0beta-b54 API implementation, class java.lang.String, method getBytes("ASCII")
returns wrong array of bytes for the string which contains non-mappable according to 
ASCII charset characters,
for the string "str\u00EA\u00FF\u00DD\u00BF\u00C1\u00A0" it returns array of
length 6: 
(byte)115 (byte)116 (byte)114 (byte)63 (byte)63 (byte)63

jdk1.3.0fcsC returns array of length 9 with all non-mappable chars substituted
with '?' (int decimal value 63).
(byte)115 (byte)116 (byte)114 (byte)63 (byte)63 (byte)63 (byte)63 (byte)63 (byte)63

The following example shows this:


import java.io.UnsupportedEncodingException;

public class Test {
    public static void main(String[] args){
        String str = "";
        try {
            byte[] bytes = "str\u00EA\u00FF\u00DD\u00BF\u00C1\u00A0".getBytes("ASCII");
            for (int i = 0; i < bytes.length; ++i){
                str += Byte.toString(bytes[i]) + " ";
            }
            System.out.println(bytes.length);
            System.out.println(str);
        } catch (UnsupportedEncodingException ue){
            System.out.println(ue);
        }
    }
}
novo1% jdk1.3.0fcsC/solaris/bin/java Test
9
115 116 114 63 63 63 63 63 63 
novo1% jdk1.4.0beta-b54/solsparc/bin/java Test
6
115 116 114 63 63 63 
novo1%


Due to this Merlin JCK test api/java_lang/String/i18n.html#i18nCtorEncodings[String2211]
fails on the platforms with "ASCII" default charset.


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

Comments
EVALUATION The substring "\u00EA\u00FF\u00DD\u00BF\u00C1\u00A0" contains characters which are unmappable in ASCII. As the submitter indicates, substitution should occur for each character. java.nio.charset.CharsetEncoder is skipping a character after a non-mapped one is found. Duplicate of bug 4457851. iag@eng 2001-07-19
19-07-2001