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

sun.io.ByteToCharEUC_JP#flush() dose not throw MalformedInputException correctl

XMLWordPrintable

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

      Name: boT120536 Date: 03/07/2001


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



      I think, sun.io.ByteToCharEUC_JP#flush() dose not throw
      MalformedInputException correctly.

      For example:


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

      public class t {
        public static void main(String arg[]) {
          try {
            byte b[] = {(byte)0xb0, (byte)0xa5};
            ByteToCharConverter btc = ByteToCharConverter.getConverter("EUC_JP");
            int len = 2;
            int estCount = btc.getMaxCharsPerByte() * len;
            char[] value = new char[estCount];
            int count = 0;
            System.out.println( "(" + btc + ")");
            // this routine dose not throw any exception. It is OK.
            try {
              count = btc.convert(b, 0, len, value, 0, estCount);
              count += btc.flush(value, btc.nextCharIndex(), estCount);
            } catch (CharConversionException e2) {
              System.out.println(e2);
            }
            System.out.println( "(" + Integer.toString(value[0], 16) + ")" + count);
            len = 1;
            // this routine should throw MalformedInputException, because
            // {0xb0, 0xa5} is a double byte character, and len==1.
            // But flush() dose not throw any exception. It is NG.
            try {
              count = btc.convert(b, 0, len, value, 0, estCount);
              count += btc.flush(value, btc.nextCharIndex(), estCount);
            } catch (CharConversionException e2) {
              System.out.println(e2);
            }
            System.out.println( "(" + Integer.toString(value[0], 16) + ")" + count);
          } catch (Exception e) {
            System.out.println(e);
          }
          try {
            byte b[] = {(byte)0x88, (byte)0xa3};
            ByteToCharConverter btc = ByteToCharConverter.getConverter("MS932");
            int len = 2;
            int estCount = btc.getMaxCharsPerByte() * len;
            char[] value = new char[estCount];
            int count = 0;
            System.out.println( "(" + btc + ")");
            // this routine dose not throw any exception. It is OK.
            try {
              count = btc.convert(b, 0, len, value, 0, estCount);
              count += btc.flush(value, btc.nextCharIndex(), estCount);
            } catch (CharConversionException e2) {
              System.out.println(e2);
            }
            System.out.println( "(" + Integer.toString(value[0], 16) + ")" + count);
            len = 1;
            // this routine throws MalformedInputException, because
            // {0x88, 0xa3} is a double byte character, and len==1.
            // And flush() throws MalformedInputException. It s OK.
            try {
              count = btc.convert(b, 0, len, value, 0, estCount);
              count += btc.flush(value, btc.nextCharIndex(), estCount);
            } catch (CharConversionException e2) {
              System.out.println(e2);
            }
            System.out.println( "(" + Integer.toString(value[0], 16) + ")" + count);
          } catch (Exception e) {
            System.out.println(e);
          }
        }
      }


      Result:

      (ByteToCharConverter: EUC_JP)
      (54c0)1
      (54c0)0
      (ByteToCharConverter: MS932)
      (54c0)1
      sun.io.MalformedInputException
      (54c0)0


      My expection is:

      (ByteToCharConverter: EUC_JP)
      (54c0)1
      sun.io.MalformedInputException
      (54c0)0
      (ByteToCharConverter: MS932)
      (54c0)1
      sun.io.MalformedInputException
      (54c0)0


      ByteToCharMS932#flush() detects MalformedInputException correctly.
      But ByteToCharEUC#flush() dose not detect it. Because ByteToCharEUC
      use 2 variables 'savedByte' and 'savedSecond'. But flush() checks
      only 'savedSecond'.

      I think the following part should be changed.

        Current code:

          public int flush(char ac[], int i, int j)
              throws MalformedInputException
          {
              if(savedSecond != 0)
              {
                  reset();
                  throw new MalformedInputException();
              } else
              {
                  reset();
                  return 0;
              }
          }

          if (savedSecond != 0) {
            reset();
            throw new MalformedInputException();
          }

        Correct code:

          public int flush(char ac[], int i, int j)
              throws MalformedInputException
          {
              if (savedByte != 0 || savedSecond != 0)
              {
                  reset();
                  throw new MalformedInputException();
              } else
              {
                  reset();
                  return 0;
              }
          }
      (Review ID: 118155)
      ======================================================================

            ilittlesunw Ian Little (Inactive)
            bonealsunw Bret O'neal (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: