Patch to fix BufferedInputStream.skip and bug 4254082

Bill Pugh pugh at
Thu Jul 5 16:25:53 UTC 2007

I'd like to submit a patch to fix BufferedInputStream.skip.

This will fix the bad behavior described in
although it doesn't fix the spec problem for InputStream.skip.

As described in our Java Puzzlers talk at JavaOne,  
BufferedInputStream.skip has a particularly unexpected behavior: if  
skipping a sequence of bytes requires both skipping some buffered  
bytes and some bytes in the underlying input stream, it will only  
skip the buffered bytes, returning a value that indicates that the  
method call only skipped those bytes.

Unfortunately, that return value is usually ignored, including most  
of the times skip is invoked in Sun's code base.

I propose the following:

* Rename the existing skip method to be a private method named skip1.
* Define a new public synchronized skip method that uses a loop to  
invoke skip1 until either all the requested bytes are skipped, or
   skip1 returns 0, and returns the total of all the calls to skip1.

I could factor out some of the simple cases into the new skip method,  
but I'm not sure that would buy any performance improvements except  
for microbenchmarks, and it would avoid duplicating any logic.

Sound good? Let me know and if this sounds acceptable I'll submit the  


More information about the core-libs-dev mailing list