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

API documentation about read() of class DataInputStream seems not to be good.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.1.5
    • docs
    • x86
    • windows_nt



      Name: dgC58589 Date: 02/19/98


      API documentation about read() of class DataInputStream seems to be different from
      the implementation of JDK1.1.4 for NT.

      API Vol1(p206) says:
      |The <read> method of <DataInputStream> calls the <read> method(I-s2.13.8) of
      |its underlying input stream(I-s2.11.1) with the three arguments <b>, 0, and
      |<b.length> and returns whatever value that method returns.

      Here, call of in.read(b, 0, b.length) is required.

      JDK1.1.5 for NT acts as below:

      Here, in.read(b, 1, b.length - 1) is called.

      I impletated read() according to API Vol1 documentation.
      However, when read() is called after calling of readLine ended by bare CR
      (\r not followed by \n), bytes that read ahead by readLine is ignored.

      I want to make sure that API documentation is no good, and the implementation
      of JDK 1.1.5 is good.

      ------- X.java -------------------------------------------------------
      import java.io.*;

      class X extends InputStream {
      int index;

      static void println(String s) {
      System.out.println(s);
      }

      public int read() {
      int ret;
      if (index++ == 3) {
      ret = '\r';
      println("X.read() returns \\r");
      } else {
      ret = 'A' + index;
      println("X.read() returns " + (char)ret);
      }
      return (byte)ret;
      }

      public int read(byte[] b) {
      println("X.read(byte[])");
      return -1;
      }

      public int read(byte[] b, int off, int len) {
      println("X.read(byte[], " + off + ", " + len + ")");
      return -1;
      }

      static public void main(String[] args) throws IOException {
      InputStream in = new X();
      DataInputStream din = new DataInputStream(in);
      String s = din.readLine();
      println("readLine() returns " + s);
      byte[] b = new byte[5];
      int len = din.read(b);
      println("din.read(byte[5]) returns " + new String(b, 0, len));
      }
      }
      -- actual result by JDK1.1.5 -----------------------------------------
      X.read() returns B
      X.read() returns C
      X.read() returns D
      X.read() returns \r
      X.read() returns F
      readLine() returns BCD
      X.read(byte[], 1, 4)
      din.read(byte[5]) returns F
      (Review ID: 25319)
      ======================================================================

            jhosunw Jamie Ho (Inactive)
            dgrahamcsunw David Graham-cumming (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: