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

java.nio.charset SHIFT_JIS converter canEncode() always returns true

XMLWordPrintable

      Name: rmT116609 Date: 08/27/2003


      FULL PRODUCT VERSION :
      java version "1.4.2"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-b28)
      Java HotSpot(TM) Client VM (build 1.4.2-b28, mixed mode)

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

      FULL OS VERSION :
      Windows 2000 Professional

      A DESCRIPTION OF THE PROBLEM :
      The character is not part of the SJIS character set. Therefore, java.nio.CharSet.canEncode() should return false. It returns true. It seems that this function always returns true, regardless of the character.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the attached java source code.
      $ java -cp . nbspSJISbug


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      canEncode(160) = false
      Encoding character got exception: Input length = 1
      ACTUAL -
      canEncode(160) = true
      Encoding character got exception: Input length = 1

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.nio.charset.Charset;
      import java.nio.charset.CharsetEncoder;
      import java.nio.charset.CharacterCodingException;
      import java.nio.CharBuffer;
      import java.nio.ByteBuffer;
      import java.util.Map;

      public class nbspSJISbug {

          public static void main(String args[])
          {
              Map charsetList = Charset.availableCharsets();
              Charset sjis = (Charset) charsetList.get("Shift_JIS");
              CharsetEncoder enc = sjis.newEncoder();

              // confirm that JVM says it can encode character 160
              boolean canEncode = enc.canEncode( (char)160 );
              System.out.println("canEncode(160) = " + canEncode);

              // but it can't actually encode that character
              char[] array = { (char) 160 };
              CharBuffer c = CharBuffer.wrap( array );

              try {
                ByteBuffer b = enc.encode(c);
             } catch ( CharacterCodingException ex ) {
                System.out.println("Encoding character got exception: " + ex.getMessage() );
             }
          }
      }
      ---------- END SOURCE ----------
      (Incident Review ID: 200705)
      ======================================================================
      ###@###.### 2003-09-26

            ilittlesunw Ian Little (Inactive)
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: