Difference between skipBytes and skip of DataInputStream?
I wonder now, what is the difference between the two methods:
DataInputStream.skipBytes
and DataInputStream.skip
.
I am aware of the fact that skip
must come from InputStream
and skipBytes
from DataInput
, but still, is t开发者_如何学编程here any difference. You know, when using streams in J2ME, things get pretty tricky so I need to know!
Would the Input/DataInput Streams returned from the FileConnection
in JSR-75 be any different in handling than any other such streams?
Thanks!
Also, skip() takes a long as an argument, so you can skip over many more bytes at a time. Useful for large files
from DataInputStream
:
public final int skipBytes(int n) throws IOException {
int total = 0;
int cur = 0;
while ((total<n) && ((cur = (int) in.skip(n-total)) > 0)) {
total += cur;
}
return total;
}
as you can see from the code, skipBytes
uses skip (InputStream.skip)
the only thing that i can say is that if data in your wrapped inputStream (InputStream inside DataInputStream)changes by another thread, then the result of skipBytes and skip may be different. but if your application just working with single thread then skipBytes and skip are the same.
If you try to use .skip() to return back instead of moving forward. You will notice that it will successfully get back in bytes. However, if you try to go back in bytes using skipBytes(), let's say you want to move 10 bytes back dis.skipBytes(n), it won't work, it will stay the same. So, in conclusion this is the main difference between skip and skipBytes. Another difference is that, skip(long i), skipBytes(int i), skip takes a long type which gives you the ability to skip more bytes for larger files.
精彩评论