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

contradiction between writeUTF(String) and readUTF()

    XMLWordPrintable

    Details

    • Subcomponent:
    • Resolved In Build:
      beta2
    • CPU:
      generic
    • OS:
      generic
    • Verification:
      Not verified

      Description



      Name: vpR10011 Date: 05/24/2001



      Specification of the class javax.imageio.stream.ImageOutputStreamImpl says:

      "public void writeUTF(String str) throws IOException
      ...
      The current byte order setting is ignored.
      ...".

      Specification of the method readUTF() which is inherited by
      the class javax.imageio.stream.ImageOutputStreamImpl
      from the class javax.imageio.stream.ImageInputStreamImpl says:

      "public String readUTF() throws IOException
      ...
      First, two bytes are read and used to construct an unsigned 16-bit integer
      in exactly the manner of the readUnsignedShort method. This integer value
      is called the UTF length and specifies the number of additional bytes to be read.
      ..."

      And specification of the readUnsignedShort() says:

      "public int readUnsignedShort() throws IOException
      ...
      Reads two bytes from the stream, and (concpetually) concatenates them according
                                               ^^^ typo copied from the spec.
      to the current byte order
      ..."

      In my opinion there is contradiction between writeUTF(String) and readUTF() methods
      because writeUTF(String) writes UTF string in only one way
      but readUTF() returns different UTF strings dependent on if the current
      byte order is network or reverse.



      To reproduce this bug run the following test:
      ---------------------test.java--------------
      import java.io.*;
      import javax.imageio.stream.*;

      public class test {
          public static void main(String[] argv) {
              OutputStream os = (OutputStream) new ByteArrayOutputStream();
              MemoryCacheImageOutputStream iosI =
                      new MemoryCacheImageOutputStream(os);
              try {
                  int len = 256;
                  char [] cc = new char[len];
                  for (int i = 0; i < len; i++) {
                      cc[i] = '\u0001';
                  }
                  String s = new String(cc);
                  String res1;
                  String res2;

                  iosI.setByteOrder(true);
                  iosI.seek(0);
                  iosI.writeUTF(s);
                  iosI.seek(0);
                  res1 = iosI.readUTF();

                  iosI.setByteOrder(false);
                  iosI.seek(0);
                  iosI.writeUTF(s);
                  iosI.seek(0);
                  res2 = iosI.readUTF();
       
                  if ( res1.equals(res2)) {
                      System.out.println("OKAY");
                  } else {
                      System.out.println("Failed: res1.length() = " + res1.length()
                              + " res2.length() = " + res2.length());
                  }
              } catch (Exception e) {
                  System.out.println("Failed: unexpected exception " + e);
              }
          }
      }
      -----------------------------------------
      % javac -d . test.java
      % java -version
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta-b65)
      Java HotSpot(TM) Client VM (build 1.4.0-beta-b65, mixed mode)
      % java test
      Failed: res1.length() = 256 res2.length() = 1
      ----------------------------------------


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

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              dricesunw Daniel Rice (Inactive)
              Reporter:
              verapsunw Verap Verap (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: