Imprecision in Reader/BufferedReader skip() method

E

Eric Capps

So I've noticed that in the BufferedReader class, the skip(long n)
method, which is supposed to skip n characters, returns a long
corresponding the number of characters actually skipped. I have tested
this, and indeed, the skip() method can return a value that is NOT equal
to n, that is, the number of characters it is supposed to skip. This
happens more often than not in my uses of it, often by a very large amount.

Why would skip do anything other than skip the exact amount of
characters it is supposed to? Is there any implementation of the Reader
class that offers some sort of guarantee on this?

More generally: I'm using BufferedReader to read files over HTTP and may
want to skip to a particular byte of a file, is using BufferedReader a
good approach? Is there perhaps instead a way I could manipulate the
HTTP header to do this?
 
C

Chris Uppal

Eric said:
Why would skip do anything other than skip the exact amount of
characters it is supposed to?

If you look at the source then you'll see that the handling of CR+LF serquences
can cause this effect -- and only that can, as far as I can see.

More generally: I'm using BufferedReader to read files over HTTP and may
want to skip to a particular byte of a file, is using BufferedReader a
good approach?

Are you working with bytes or characters ? You can't mix the two. I suspect
this is the real cause of your problems.

Is there perhaps instead a way I could manipulate the
HTTP header to do this?

There is an HTTP mechanism to ask only for part of a file with a GET. Servers
are not required to understand it. See the headers "Accept Ranges: bytes" and
"Range: <number>-<number>". The HTTP client library you are using may or may
not expose that feature.

But note -- if you do use it then it works in /bytes/.

-- chris
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top