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

(cs) jdk regression:InputStreamReader reads unaccepted codes without any excepti

    XMLWordPrintable

    Details

    • Subcomponent:
    • CPU:
      sparc
    • OS:
      solaris_2.6

      Description



      Name: auR10023 Date: 01/16/2001


      Method java.io.InputStreamReader.read(char[],int,int) does not raise IOException for
      unaccepted codes.

      Here is the part of the UTF-8 specification:
      ...
         UCS-4 range (hex.) UTF-8 octet sequence (binary)
         0000 0000-0000 007F 0xxxxxxx
         0000 0080-0000 07FF 110xxxxx 10xxxxxx
         0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx

         0001 0000-001F FFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
         0020 0000-03FF FFFF 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
         0400 0000-7FFF FFFF 1111110x 10xxxxxx ... 10xxxxxx
      ...

      That means that binary octet sequence 10000000 is illegal for UTF-8 encoding.
      In jdk1.3 this method throws IOException in this case.

      Here is the test demonstrating the bug:
      ------------------- t.java -------------
      import java.io.*;

      public class t{
          public static void main(String [] args) {
              byte[] bytes = { -128 };
              char[] readChars = new char[10];

              ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
              InputStreamReader isr = null;
              try {
                  isr = new InputStreamReader(bais, "UTF8");
              } catch(UnsupportedEncodingException e){
                  System.out.println("Unexpected exception");
                  return;
              }
              try {
                  int len = isr.read(readChars,0,readChars.length);
                  System.out.println(len + " chars read: ");
                  for (int i = 0; i < len; i++) {
                      System.out.println("char " + i + ": " + readChars[0]);
                  }
                  System.out.println("Test failed: No exceptions thrown");
              } catch (IOException ioe) {
                  System.out.println("Test passed. Thrown: " + ioe);
                  return;
              }
          }
      }

      ----------- output from the test: --------------------
      #> java -version
      java version "1.4.0-beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta-b46)
      Java HotSpot(TM) Client VM (build 1.4beta-B45, mixed mode)

      #> java t
      10 chars read:
      char 0: u
      char 1: u
      char 2: u
      char 3: u
      char 4: u
      char 5: u
      char 6: u
      char 7: u
      char 8: u
      char 9: u
      Test failed: No exceptions thrown

      #> java -version
      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, interpreted mode)
      #> java t
      Test passed. Thrown: sun.io.MalformedInputException

      ======================================================================

        Attachments

          Activity

            People

            Assignee:
            iris Iris Clark
            Reporter:
            avusunw Avu Avu (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: