BufferedReader and buffer size

Discussion in 'Java' started by Jacob, Feb 26, 2004.

  1. Jacob

    Jacob Guest

    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!
    Jacob, Feb 26, 2004
    #1
    1. Advertising

  2. Jacob wrote:

    > 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
    John C. Bollinger, Feb 26, 2004
    #2
    1. Advertising

  3. Jacob

    ak Guest

    >
    > 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.
    ak, Feb 26, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -
    Replies:
    5
    Views:
    4,863
    Esmond Pitt
    Apr 8, 2005
  2. Skyhorse
    Replies:
    4
    Views:
    4,225
    EventHelix.com
    Apr 16, 2004
  3. Raja
    Replies:
    12
    Views:
    24,350
    John Harrison
    Jun 21, 2004
  4. Replies:
    2
    Views:
    590
    sergejusz
    Mar 26, 2007
  5. Replies:
    6
    Views:
    6,160
Loading...

Share This Page