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

convertAny() of CharToByteConverter has "off-by_one" problem in some cases

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.3.0
    • 1.2.2
    • core-libs
    • None
    • kestrel
    • generic
    • generic

      In case the "malformed" character is the only character in the input buffer
      or the last character in the input buffer, the return value from convertAny()
      will be off-by-one. The following sample showes the problem. The expected
      return value is "3" not the current "2"

      import java.io.*;
      import sun.io.*;

      public class test_c2b {
        public static void main(String args[]) {
          char cc[] = new char[10];
          byte bb[] = new byte[20];
          CharToByteConverter c2b = null;
          int i;

          try {
            c2b = CharToByteConverter.getConverter("ASCII");
            cc[0] = (char)0xdc00;
            cc[1] = (char)0xdc00;
            cc[2] = (char)0xdc00;
            int ret = c2b.convertAny(cc, 0, 3, bb, 0, bb.length);
            System.out.println("ret=" + ret);
            System.out.println("byte[0]=" + bb[0]);
            System.out.println("byte[1]=" + bb[1]);
            if (ret == 3);
            System.out.println("byte[2]=" + bb[2]);

          }
          catch (Exception e){
            System.out.println("c2b failed exception: " + e);
          }
          
        }
      }

            sherman Xueming Shen
            sherman Xueming Shen
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: