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

        Sebb created issue -
        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.
        Sebb made changes -
        Field Original Value New Value
        Comment [ Note that Reader.skip(n) and InputStream.skip(n) behave differently according to the Javadoc.

        Reader.skip(n) throws IllegalArgumentException for n < 0, whereas InputStread.skip(n) does nothing.
        ]
        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
        Sebb made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Resolution Fixed [ 1 ]
        Sebb made changes -
        Fix Version/s 2.0 [ 12312961 ]
        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.
        Jukka Zitting made changes -
        Resolution Fixed [ 1 ]
        Status Resolved [ 5 ] Reopened [ 4 ]
        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
        Sebb made changes -
        Status Reopened [ 4 ] Resolved [ 5 ]
        Resolution Fixed [ 1 ]
        Henri Yandell made changes -
        Status Resolved [ 5 ] Closed [ 6 ]
        Mark Thomas made changes -
        Workflow jira [ 12461842 ] Default workflow, editable Closed status [ 12601834 ]
        Transition Time In Source Status Execution Times Last Executer Last Execution Date
        Open Open Resolved Resolved
        314d 9h 44m 1 Sebb 07/Mar/10 21:35
        Resolved Resolved Reopened Reopened
        14h 59m 1 Jukka Zitting 08/Mar/10 12:34
        Reopened Reopened Resolved Resolved
        7h 46m 1 Sebb 08/Mar/10 20:20
        Resolved Resolved Closed Closed
        134d 20h 39m 1 Henri Yandell 21/Jul/10 18:00

          People

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

            Dates

            • Created:
              Updated:
              Resolved:

              Development