File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes IDEs, Version Control and other tools and the fly likes Java InputStream skip() method doesnt work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » IDEs, Version Control and other tools
Bookmark "Java InputStream skip() method doesnt work" Watch "Java InputStream skip() method doesnt work" New topic
Author

Java InputStream skip() method doesnt work

jay vas
Ranch Hand

Joined: Aug 30, 2005
Posts: 407
This is a major issue in the Java InputStream API that absolutely has to have some kind of explanation .... I'm wondering if anybody can shed some light on this for me.

Since the late nineties, there has been an open bug in Java's InputStream method for skip(int bytesToSkip). Sometimes, it doesn't actually skip the amount of bytes it recieves as input ? Can anyone explain why it is that skip doesn't always work as it should ?

Here's what the javadoc for InputStream.skip sais (http://java.sun.com/j2se/1.4.2/docs/api/java/io/InputStream.html#skip(long)) sais



Skips over and discards n bytes of data from this input stream. 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. The actual number of bytes skipped is returned. If n is negative, no bytes are skipped.
The skip method of InputStream creates a byte array and then repeatedly reads into it until n bytes have been read or the end of the stream has been reached. Subclasses are encouraged to provide a more efficient implementation of this method.



What gives ? Why is it that an inputstream would ever fail to return the amount of data which it skipped ?

Yes, I know its an interface, and that maybe in designing the interface the API developers decided to accomodate inputstream's which may abstract or hide multiple resources, and which thus would have the possibility of being error prone...... But nevertheless, we just found a bug in some software where the Java FileInputStream was actually skipping inconsistently (i.e. send it 28, and it only skips 8 bytes of a file, even though there are still several 100s of bytes left to go).
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60041
    
  65

Please don't use code tags for text. It really screws up the formatting of posts.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39535
    
  27
It would be helpful if you provided a link to the issue in the bug tracker. I'm not aware of anyone here at JavaRanch being intimately involved with the JDK development effort, so I'm not sure anyone could say with any amount of certainty what the reasons for not fixing the bug might be.


Ping & DNS - updated with new look and Ping home screen widget
Gerard Charles
Greenhorn

Joined: Jan 21, 2010
Posts: 11
The really old bug report
jay vas
Ranch Hand

Joined: Aug 30, 2005
Posts: 407
Hmmmmm..... Yes, thats certainly the bug report.

Its quite strange isn't it ? I'm dying for some insight here !
Madalina Mutihac
Greenhorn

Joined: Mar 03, 2008
Posts: 7
A good video explaining this: http://www.infoq.com/presentations/Painful-Java-Puzzlers-and-Bug-Patterns
Starts at minute 35. It offers an explanation and solution to go around the problem.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java InputStream skip() method doesnt work
 
Similar Threads
B&S DB Interface read( int recNo)?
file downloading concept
how to read a file data into byte array
I'm getting a really weird IOException...
ObjectInputStream