11

According to the documentation, BufferedReader(Reader) uses a default buffer size, while the second constructor, BufferedReader(Reader, int) allows the buffer size to be set.

public BufferedReader(Reader in)

Creates a buffering character-input stream that uses a default-sized input buffer.

However, the docs do not not mention what the default buffer size is.

What is the default buffer size of a BufferedReader?

4
  • BufferedReader might interest you Commented Jun 6, 2013 at 23:30
  • @Vulcan EJP gave you a wrong answer then. as said I've extraced the actual .jar containing BufferedReader and took a look at it. maybe it was other way for some years, or will be other way in future, but now it is stricctly specified to 8192 in the java.io.BufferedReader!!! Commented Jun 7, 2013 at 8:10
  • @Vulcan android was jsut an example of documentation. actual proof followed later. but since android also uses rt.jar, transitively the documentation is enough. Commented Jun 7, 2013 at 8:12
  • @IAM I did not give a wrong answer. I stated that it (a) is unspecified and (b) had been 4096 in the Oracle/Sun code for many years. They are free to change it at any time because it is unspecified. Commented Sep 24, 2016 at 10:47

4 Answers 4

11

The default buffer size is 8192 characters

http://developer.android.com/reference/java/io/BufferedReader.html

 BufferedReader(Reader in)
Constructs a new BufferedReader, providing in with a buffer of 8192 characters.

Besides this documentation, I've extraced the rt.jar archive, and decompiled the BufferedReader.class from java.io.* using JD-GUI, this is what I found in the class definition:

private static int defaultCharBufferSize = 8192;
Sign up to request clarification or add additional context in comments.

9 Comments

You may be right, but the official docs don't say that: docs.oracle.com/javase/7/docs/api
@7stud Take a look at link given in my comment.
@Smit, Back at ya: take a look at the oracle link in my comment.
@7stud I already looked at Javadoc API, and so my search ended me at that link. Anyhow lots of other forums says the same size and I ran your program and gave me 8192
While the official docs do not mention this, I just took a look through the source code, and it is indeed 8192. Thanks.
|
2

It isn't specified. On purpose. It's been 4096 for some years in the Sun/Oracle Java JDKs but don't rely on it.

2 Comments

It might be platform dependent, but it's 8192 on current Android.
@Pevik Your link is to BufferedInputStream, and the question is about BufferedReader.
1

I'm sure I think it may be system/jvm dependent. Run this program:

What are the default buffer size for java.io.BufferedInputStream on old and exotic JVMs?

import java.io.BufferedInputStream;
import java.io.InputStream;

public class BufferSizeDetector extends BufferedInputStream {
    public static void main(String[] args) {
        BufferSizeDetector bsd = new BufferSizeDetector(null);

        System.err.println(System.getProperty("java.version"));
        System.err.println(bsd.getBufferSize());
    }

    public BufferSizeDetector(InputStream in) {
        super(in);
    }

    public int getBufferSize() {
        return super.buf.length;
    }
}

I get:

1.6.0_45
8192

2 Comments

Interesting, but what assures that BufferedReader and BufferedInputStream have the same default buffer size?
@Vulcan, Good point. (Stupid BufferedReader class doesn't have a buf field).
1

Ans is 8 KB.

1024 bytes * 8 = 8192 (8 KB)
refer class : java.io.BufferedReader

private static int defaultCharBufferSize = 8192;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.