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

(spec) InputStream.skip() javadoc should note possible IOException

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Fixed
    • Icon: P4 P4
    • 6
    • 1.4.2
    • core-libs
    • b78
    • x86
    • linux

      A DESCRIPTION OF THE REQUEST :
      See the example code below. It seems that skip() is essentially unsupported by some InputStreams in some situations, and will always throw IOException. This is not necessarily the expected behavior given the javadoc, which says:

      "...The skip method may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly 0. This may result from any of a number of conditions; reaching end of file before n bytes have been skipped is only one possibility."

      That would lead me to believe that if the underlying InputStream can't skip, that skip() would simply return 0.

      The example code shows that this does occur with the InputStreams returned from Process.getOutputStream() and Process.getErrorStream(). This is really a BufferedInputStream wrapping a FileInputStream, and the IOException comes from FileInputStream.skip(). I assume this is because stdout/stderr file descriptors cannot be seeked (at least on Linux, no idea about other platforms).

      While you could argue that the InputStreams from Process should be fixed -- they can be -- I don't think the behavior is actually wrong. I just think it needs to be documented. Even if this one case is fixed there are likely several others that are not. Better just make developers aware of this.

      JUSTIFICATION :
      For example, from the javadoc it would seem that the natural way to discard all input from an InputStream would be this:

      do {
        is.skip(Integer.MAX_VALUE);
      } while (is.read() != -1);

      However this doesn't work for certain streams and developers should be aware of that.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Modify javadoc to point out that skip() may always throw an IOException for certain streams (possibly noting this particular case in Process).
      ACTUAL -
      Javadoc is as it is, and program output is:

      4096
      Exception in thread "main" java.io.IOException: Illegal seek
              at java.io.FileInputStream.skip(Native Method)
              at java.io.BufferedInputStream.skip(BufferedInputStream.java:305)
              at Test.main(Test.java:9)


      ---------- BEGIN SOURCE ----------
      public class SkipTest {
        public static void main(String[] args) throws Exception {
          Process p = Runtime.getRuntime().exec("ls -lR /etc");
          InputStream is = p.getInputStream();
          Thread.sleep(1000L); // wait for output, for good measure
          System.out.println(is.available()); // prints 4096 for me
          is.skip(1); // throws IOException: "Illegal seek"
        }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Do not call seek() on InputStreams returned from Process. Instead, repeatedly read from stream into a temporary buffer until enough bytes have been skipped.
      ###@###.### 2005-1-28 11:14:52 GMT

            jhangalsunw Jayalaxmi Hangal (Inactive)
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: