BufferedReader and buffer size

J

Jacob

I do this:

r = new BufferedReader (new InputStreamReader (stream));

which gives me a buffered reader with the default
buffer size of 8192 bytes (according to the JDK source).

As I read large files (50-100Mb) I wanted to experiment
with a custom buffer size like this:

r = new BufferedReader (new InputStreamReader (stream), size);

No matter what size I choose (even 8192); Using this call
my program "hangs" for several seconds at the very first
read operation later in the code. This is not the case for
the default version.

Any clues? BTW: I read my files over a network, and I use
JDK 1.4.2 on Linux.

Is messing with the buffer size worthwhile anyway?

Thanks!
 
J

John C. Bollinger

Jacob said:
I do this:

r = new BufferedReader (new InputStreamReader (stream));

ALWAYS specify a charset when converting between bytes and characters.
InputStreamReader has a constructor that allows you to do so.
which gives me a buffered reader with the default
buffer size of 8192 bytes (according to the JDK source).

The default size not being guaranteed from one version to another.
Quite frankly, I think it is rarely a good idea to look at the platform
API sources, as you are thereby delving into implementation details that
are subject to change. The only exceptions I can think of at the moment
are when you are troubleshooting and when you want programming examples
(but don't assume that the sources are always good model code).
As I read large files (50-100Mb) I wanted to experiment
with a custom buffer size like this:

r = new BufferedReader (new InputStreamReader (stream), size);

Have you considered doing your own buffering? It's not that hard if all
you need to do is achieve good reading performance.

Note also that a larger buffer probably doesn't help you any more for
large files than it does for medium files, but it's always good to
verify such assumptions. A smaller buffer might give you comparable
performance, in fact -- I have run tests that showed (in one particular
case) that the performance difference between manually buffering a byte
stream into a 2048-byte buffer and using a default BufferedInputStream
was negligible.

See below, however.
No matter what size I choose (even 8192); Using this call
my program "hangs" for several seconds at the very first
read operation later in the code. This is not the case for
the default version.

There are many reasons why the first read might hang, most of them at
the OS level. Invoking the "troubleshooting" justification for looking
at the [v. 1.4.2] source, I see that the one-arg constructor just fronts
for the two-arg constructor, so you are always using the two-arg
constructor one way or the other. There is thus no reason whatever why
using a BufferedReader with a default buffer size should produce any
difference in behavior at all from using a BufferedReader with an
explicitly-sized buffer of the same size as the default.
Any clues? BTW: I read my files over a network, and I use
JDK 1.4.2 on Linux.

I suspect you aren't seeing what you think you're seeing.
Is messing with the buffer size worthwhile anyway?

Not at first. Get it working first, and if you need to tweak
performance then profile the application to see where optimization will
be most effective.


John Bollinger
(e-mail address removed)
 
A

ak

Note also that a larger buffer probably doesn't help you any more for
large files than it does for medium files, but it's always good to
verify such assumptions. A smaller buffer might give you comparable
performance, in fact -- I have run tests that showed (in one particular
case) that the performance difference between manually buffering a byte
stream into a 2048-byte buffer and using a default BufferedInputStream
was negligible.

for reading from quick medien (HD) it is better to have _bigger_ buffer -
100 kb and more.

____________

http://reader.imagero.com the best java image reader.
 

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,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top