Commons IO
  1. Commons IO
  2. IO-203

Add skipFully() method for InputStreams

    Details

    • Type: New Feature New Feature
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.0
    • Component/s: Utilities
    • Labels:
      None

      Description

      The skip() method is not guaranteed to skip the requested number of bytes, even if there is more data available. This is particularly true of Buffered input streams.

      It would be useful to have a skip() method that keeps skipping until the required number of bytes have been read, or EOF was reached, in which case it should throw an Exception.

      [I'll add a patch later.]

        Activity

        Hide
        Sebb added a comment -

        Started work on this.

        I'll implement the following methods:

        void skipFully(input, count) - keeps reading and throws EOFException if end reached before count reached

        long skip(input, count) - keeps reading, reports count actually read.

        Show
        Sebb added a comment - Started work on this. I'll implement the following methods: void skipFully(input, count) - keeps reading and throws EOFException if end reached before count reached long skip(input, count) - keeps reading, reports count actually read.
        Hide
        Sebb added a comment -

        Turns out that the basic skip(long) implementations already allow for looping until read() returns -1, so they will skip as much as possible.

        So only the skipFully() methods currently make sense, added.

        URL: http://svn.apache.org/viewvc?rev=920114&view=rev
        Log:
        Add skipFully() methods for InputStream and Reader

        Modified:
        commons/proper/io/trunk/src/java/org/apache/commons/io/IOUtils.java
        commons/proper/io/trunk/src/test/org/apache/commons/io/IOUtilsTestCase.java

        Show
        Sebb added a comment - Turns out that the basic skip(long) implementations already allow for looping until read() returns -1, so they will skip as much as possible. So only the skipFully() methods currently make sense, added. URL: http://svn.apache.org/viewvc?rev=920114&view=rev Log: Add skipFully() methods for InputStream and Reader Modified: commons/proper/io/trunk/src/java/org/apache/commons/io/IOUtils.java commons/proper/io/trunk/src/test/org/apache/commons/io/IOUtilsTestCase.java
        Hide
        Jukka Zitting added a comment -

        I don't think the skipFully() method works as intended the way it's currently implemented. As said in the InputStream.skip() javadocs: "The skip method may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly 0." Thus the skipFully() method should always fall back to read() when the skip() method returns something less than the number of bytes requested.

        As an added complexity, note that a FileInputStream allows skipping any number of bytes past the end of the file! If we want to detect that case, the skipFully() method should first skip() n-1 bytes and then try to read() all the remaining bytes.

        Show
        Jukka Zitting added a comment - I don't think the skipFully() method works as intended the way it's currently implemented. As said in the InputStream.skip() javadocs: "The skip method may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly 0." Thus the skipFully() method should always fall back to read() when the skip() method returns something less than the number of bytes requested. As an added complexity, note that a FileInputStream allows skipping any number of bytes past the end of the file! If we want to detect that case, the skipFully() method should first skip() n-1 bytes and then try to read() all the remaining bytes.
        Hide
        Sebb added a comment -

        I started out doing that, but as far as I could tell the default implementations of skip call read() repeatedly until EOF anyway - so it would just be repeating the code.

        As to FileInputStream, I noticed that when I was trying to set up a test.
        I've just confirmed that the behaviour is due to skip() rather than read() - in other words it overrides the default skip() implementation.

        So yes, it would after all be useful to have a "proper" skip implementation that cannot be overridden.

        How should count < 0 be handled?
        At present Reader throws IAE, but InputStream just returns. I'd prefer to see IAE for both.

        Show
        Sebb added a comment - I started out doing that, but as far as I could tell the default implementations of skip call read() repeatedly until EOF anyway - so it would just be repeating the code. As to FileInputStream, I noticed that when I was trying to set up a test. I've just confirmed that the behaviour is due to skip() rather than read() - in other words it overrides the default skip() implementation. So yes, it would after all be useful to have a "proper" skip implementation that cannot be overridden. How should count < 0 be handled? At present Reader throws IAE, but InputStream just returns. I'd prefer to see IAE for both.
        Hide
        Sebb added a comment -

        Implemented guaranteed skip() methods.

        URL: http://svn.apache.org/viewvc?rev=920483&view=rev
        Log:
        IO-203 - Add skipFully() method for InputStreams
        Implement our own skip() methods; rewrite skipFully() to use them

        Modified:
        commons/proper/io/trunk/src/java/org/apache/commons/io/IOUtils.java
        commons/proper/io/trunk/src/test/org/apache/commons/io/IOUtilsTestCase.java

        Show
        Sebb added a comment - Implemented guaranteed skip() methods. URL: http://svn.apache.org/viewvc?rev=920483&view=rev Log: IO-203 - Add skipFully() method for InputStreams Implement our own skip() methods; rewrite skipFully() to use them Modified: commons/proper/io/trunk/src/java/org/apache/commons/io/IOUtils.java commons/proper/io/trunk/src/test/org/apache/commons/io/IOUtilsTestCase.java

          People

          • Assignee:
            Unassigned
            Reporter:
            Sebb
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development