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

UnicodeEncoder Error When using special characters in "UTF-16" or "UTF-16BE"

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Icon: P4 P4
    • None
    • 5.0
    • core-libs

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

      java version "1.5.0_06"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
      Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Windows XP

      A DESCRIPTION OF THE PROBLEM :
      When using special characters in "UTF-16" no compile error is thrown on 1.4.2_09 but using "UTF-16BE" throws an UnicodeEncoder Error on 5.0_06

      ---------- BEGIN SOURCE ----------

      The test case below works on JDK 1.4.2_09. However changing the the encoding into "UTF-16" or "UTF-16BE", the case can not work neither in 1.4.2 nor in 1.5.0.

      /import junit.framework.TestCase;

      /**
       * UnicodeEncoder Error When using special characters in "UTF-16" or
      "UTF-16BE"
       *
       * @author fengyun
       * @version 1.00 Mar 22, 2006 3:59:35 PM
       */
      public class UTF16Test extends TestCase
      {
          public void testConvert() throws Exception
          {
              char[] chars = new char[]{0xDC3E};
              //Create a new String
              String str = new String(chars);
              //Convert to byte array
              byte[] bytes2 = str.getBytes("UTF16");
              //Create a new String from the byte array
              String newString = new String(bytes2, "UTF16");
              //The string must be same as before, But the isn't equal here
              assertTrue(newString.equals(str));
          }
      }

      /////////////////////////////////////////////////////////////////////////////////

      You must add the junit jar. No configuration is need. The test case below running with JDK 5.0 will throw a Exception:

      import junit.framework.TestCase;
      /**
       * UnicodeEncoder Error When using special characters in "UTF-16" or
      "UTF-16BE"
       *
       * @author fengyun
       * @version 1.00 Mar 22, 2006 3:59:35 PM
       */
      public class UTF16Test extends TestCase
      {
          public void testConvert() throws Exception
          {
              char[] chars = new char[]{0xDC3E};
              //Create a new String
              String str = new String(chars);
              //Convert to byte array
              byte[] bytes2 = str.getBytes("UTF-16BE");
              //Create a new String from the byte array
              String newString = new String(bytes2, "UTF-16BE");
              //The string must be same as before, But the isn't equal here
              assertTrue(newString.equals(str));
          }
      }
      ---------- END SOURCE ----------

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      --------------------------------------------------------------------------------
      junit.framework.AssertionFailedError
       at UTF16Test.testConvert(UTF16Test.java:21)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at
      sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at
      sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at com.intellij.rt.execution.junit2.JUnitStarter.main(JUnitStarter.java:32)
      --------------------------------------------------------------------------------

            Unassigned Unassigned
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: