RandomAccessFile.skipBytes() - no guarantee

A

ak

The method skipBytes in RandomAccessFile is documumented as giving no
guarantee that the bytes requested will be skipped, returning instead the
actual number skipped.
The method description seems to be copied directly from interface DataInput
and I suspect this is why is left so open.

Surely RAF.skipBytes() should document the reasons why the request may not
succeed.

<quote>
This method may skip over some smaller number of bytes, possibly zero. This
may result from any of a number of conditions; reaching end of file before n
bytes have been skipped is only one possibility.
</quote>

So what are the other possibilities?

Perhaps I should just check the return value and throw IOException if it is
not as requested.

no, you should skipBytes in a loop (add skipped bytes till you have needed
value)
and throw EOFException only if return walue was negative.

However, with RandomAccessFile is better to use seek();
 
V

VisionSet

The method skipBytes in RandomAccessFile is documumented as giving no
guarantee that the bytes requested will be skipped, returning instead the
actual number skipped.
The method description seems to be copied directly from interface DataInput
and I suspect this is why is left so open.

Surely RAF.skipBytes() should document the reasons why the request may not
succeed.

<quote>
This method may skip over some smaller number of bytes, possibly zero. This
may result from any of a number of conditions; reaching end of file before n
bytes have been skipped is only one possibility.
</quote>

So what are the other possibilities?

Perhaps I should just check the return value and throw IOException if it is
not as requested.
 
V

VisionSet

ak said:
before

no, you should skipBytes in a loop (add skipped bytes till you have needed
value)
and throw EOFException only if return walue was negative.

However, with RandomAccessFile is better to use seek();

Yes I suppose it is if I have to do that.
Thanks.
 
L

Larry Barowski

...
no, you should skipBytes in a loop (add skipped bytes till you have needed
value)
and throw EOFException only if return walue was negative.

The docs seem to indicate that a negative value is never returned
"Returns: the number of bytes actually skipped", and don't mention
any EOF indication. I assume the docs are wrong, since that would
make the method basically useless.
 
A

ak

The docs seem to indicate that a negative value is never returned
"Returns: the number of bytes actually skipped", and don't mention
any EOF indication. I assume the docs are wrong, since that would
make the method basically useless.

"This method never throws an EOFException. The actual number of bytes
skipped is returned. If n is negative, no bytes are skipped."

hmm, ok I read docs too quickly. Short look at the source make it clear:

public int skipBytes(int n) throws IOException {
long pos;
long len;
long newpos;

if (n <= 0) {
return 0;
}
pos = getFilePointer();
len = length();
newpos = pos + n;
if (newpos > len) {
newpos = len;
}
seek(newpos);

/* return the actual number of bytes skipped */
return (int) (newpos - pos);
}

don't forget that seek() also don't throws EOFException!
EOFException thrown after you trying to read after file end.
 
L

Larry Barowski

ak said:
"This method never throws an EOFException. The actual number of bytes
skipped is returned. If n is negative, no bytes are skipped."

hmm, ok I read docs too quickly. Short look at the source make it clear:
...

So for that implementation, skipBytes() will return the requested number
of bytes unless EOF is reached. But the docs are the spec, and according
to the docs skipBytes() is pointless. If it may not skip the requested
number of bytes, but there is no way to tell if this is because EOF was
reached (in which case we shouldn't try again) or for some other reason
(in which case maybe we should try again), then I can't think of any way
to use it. The same goes for other classes that implement DataInput.
 
V

VisionSet

Larry Barowski said:
So for that implementation, skipBytes() will return the requested number
of bytes unless EOF is reached. But the docs are the spec, and according
to the docs skipBytes() is pointless. If it may not skip the requested
number of bytes, but there is no way to tell if this is because EOF was
reached (in which case we shouldn't try again) or for some other reason
(in which case maybe we should try again), then I can't think of any way
to use it. The same goes for other classes that implement DataInput.

Yep, pretty poor.
I've used it in a method that already throws IOEx, because I looked at the
source, and I know I'm safe assuming EOF won't be reached. And because it
makes my intention clearer. But that isn't the point. I agree it is a
pointless method as the docs stand.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top