Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4964175

cannot decode US-ASCII using x-ISO-2022-CN-CNS and x-ISO-2022-CN-GB

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 5.0
    • core-libs

      libs/src/java_nio/Charsets/CharTest

      Charset x-ISO-2022-CN-CNS and x-ISO-2022-CN-GB
      contais Charset US-ASCII returns true for both.

      One can encode the Characters in US-ASCII range but while decoding the
      same exception is being thrown.
      Using the Tiger Build-29 on Solaris-9

      Please find the code mentioned below.

      Code:-
      import java.io.*;
      import java.nio.*;
      import java.nio.charset.*;
      import java.util.*;


      public class CharTest1 {
           
               String str1 = "\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008\u0009";
               String str2 = "\u0020\u0021\u0024\u0025\u0026\u0027\u0028\u0029";
               String str3 = "\u004A\u004B\u004C\u004D\u004E\u004F\u002B\u002C\u002D\u002E\u002F\u001A\u001B\u001C\u001D\u001E\u001F";
               String str = str1 + str2 + str3 ;
               
           public void checkEncoding(String encoding) {
               try {
                    CharBuffer chbuff = CharBuffer.wrap(str);
                    Charset cset1 = Charset.forName(encoding);
                    System.out.println("Can encode :"+encoding+" "+cset1.canEncode());
                    ByteBuffer bb = cset1.newEncoder().encode(chbuff);
                    chbuff.position(0);
                    CharBuffer chbuff1 = cset1.newDecoder().decode(bb);
                    System.out.println("They are equal for: "+encoding+": "+chbuff1.equals(chbuff));
               } catch(Exception e) {
                    e.printStackTrace();
               }
           
           }
           
           public void checkContains(String encod1, String encod2) {
                  Charset cset1 = Charset.forName(encod1);
                  Charset cset2 = Charset.forName(encod2);
                  System.out.println(encod1+" contains "+encod2+" "+cset1.contains(cset2));
           
           }
                  
           
         
           public static void main(String args[]) {
                CharTest1 ref = new CharTest1();
                ref.checkContains("x-ISO-2022-CN-CNS", "US-ASCII");
                ref.checkContains("x-ISO-2022-CN-GB", "US-ASCII");
                ref.checkEncoding("x-ISO-2022-CN-CNS");
                ref.checkEncoding("x-ISO-2022-CN-GB");
                       
           }

      }



      /*
      x-ISO-2022-CN-CNS contains US-ASCII true
      x-ISO-2022-CN-GB contains US-ASCII true
      Can encode :x-ISO-2022-CN-CNS true
      java.nio.charset.MalformedInputException: Input length = 2
              at java.nio.charset.CoderResult.throwException(CoderResult.java:260)
              at java.nio.charset.CharsetDecoder.decode(CharsetDecoder.java:763)
              at CharTest1.checkEncoding(CharTest1.java:22)
              at CharTest1.main(CharTest1.java:43)
      Can encode :x-ISO-2022-CN-GB true
      java.nio.charset.MalformedInputException: Input length = 2
              at java.nio.charset.CoderResult.throwException(CoderResult.java:260)
              at java.nio.charset.CharsetDecoder.decode(CharsetDecoder.java:763)
              at CharTest1.checkEncoding(CharTest1.java:22)
              at CharTest1.main(CharTest1.java:44)
      */

            sherman Xueming Shen
            jsinghsunw Jit Singh (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: