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

InputStream.skipFully(int k) to skip exactly k bytes

    Details

    • Type: Enhancement
    • Status: Resolved
    • Priority: P3
    • Resolution: Fixed
    • Affects Version/s: 7
    • Fix Version/s: 12
    • Component/s: core-libs
    • Labels:
      None
    • Subcomponent:
    • Resolved In Build:
      b24
    • CPU:
      generic
    • OS:
      generic

      Description

      Bill Pugh writes:

      ----------------------------------------------
      One of the bugs I keep on seeing is calling the skip method on an InputStream and ignoring
      the return value. This occurs 13 times in jdk1.6.0-b105.

      The contract for skip is that is returns the number of bytes skipped, and that it can skip anywhere from 0
      to the number of bytes requested.

      Most of the time, a developer wants to skip exactly k bytes. What code would he have to write?

      I think the appropriate code would be:

              while (bytes > 0) {
                  long skipped = in.skip(bytes);
                  if (skipped == 0) throw new EOFException("Tried to skip " + bytes + " but reached EOF");
                  bytes -= skipped;
              }

      Two things here: First, it seems that the specification for skip should be clarified to say that skip can only
      return 0 if a nonpositive value was passed as the argument to skip or if EOF is reached in the file before
      any bytes were skipped.

      Secondly, I'd really love to have an method provided as part of the JDK to tell people to use, rather
      than telling them to write those 5 lines of code every time they want to skip some bytes.

      We could add skipFully to InputStream, but that could introduce incompatibilities if someone else has
      defined a skipFully method in some subclass of InputStream.

      An alternative would be to define a static method for this somewhere. More awkward, but guaranteed to
      not introduce any incompatibilities.

      By the way, the skip method for Reader doesn't explicitly say that it might return a number of skipped
      characters smaller than requested. However, it does say that it will block until at least one character
      can be skipped. The javadoc for Reader.skip and InputStream.skip should probably be made to align.

      Any updates should be done to the spec for all three of InputStream.skip, DataInputStream.skipBytes,
      and Reader.skip.
      ----------------------------------------------

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                bpb Brian Burkhalter
                Reporter:
                martin Martin Buchholz
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: