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

InputStream.skipNBytes could be implemented more efficiently

    XMLWordPrintable

Details

    • b27
    • Verified

    Description

      A DESCRIPTION OF THE PROBLEM :
      The default implementation of java.io.InputStream.skipNBytes (as described in the javadoc) first calls skip(long) once and then repeatedly calls read().
      This can probably be implemented more efficiently by repeatedly calling skip(long) and only calling read() if 0 is returned by skip(long):

      public void skipNBytes(long n) throws IOException {
          while (n > 0) {
              long ns = skip(n);
              if (ns < 0 || (n -= ns) < 0) { // skipped negative or too many bytes
                  throw new IOException("Unable to skip exactly");
              }
              
              // Don't know if EOF, have to read one byte to be sure
              if (ns == 0) {
                  if (read() == -1) {
                      throw new EOFException();
                  }
                  n--; // skipped one byte by read()
              }
          }
      }


      Attachments

        Issue Links

          Activity

            People

              bpb Brian Burkhalter
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: