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?

Community
  • 1
  • 1
FThompson
  • 28,352
  • 13
  • 60
  • 93
  • [BufferedReader](http://ulibgcj.sourceforge.net/javadoc/java/io/BufferedReader.html#BufferedReader(java.io.Reader)) might interest you – Smit Jun 06 '13 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!!! – IAM Jun 07 '13 at 08: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. – IAM Jun 07 '13 at 08: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.* – user207421 Sep 24 '16 at 10:47

4 Answers4

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;
IAM
  • 895
  • 2
  • 13
  • 30
  • 1
    You may be right, but the official docs don't say that: http://docs.oracle.com/javase/7/docs/api/ – 7stud Jun 06 '13 at 23:35
  • @7stud Take a look at link given in my comment. – Smit Jun 06 '13 at 23:37
  • 1
    @Smit, Back at ya: take a look at the oracle link in my comment. – 7stud Jun 06 '13 at 23:39
  • @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` – Smit Jun 06 '13 at 23:42
  • 1
    While the official docs do not mention this, I just took a look through the source code, and it is indeed 8192. Thanks. – FThompson Jun 07 '13 at 02:02
  • This is the default in Android. OP didn't mention Android. It's different in the Oracle JDK. And not documented in either. – user207421 Jun 07 '13 at 02:20
  • @EJP really? this was just an example of documentation. the real proof follows in the answer. as said, I've extracted the rt.jar, which contains default classes for the runtime, not jsut for the android. it contains the actual class for buffered reader. and it is 8192 by default there. may be it was different sometime, or will be different, but now it is specefied! – IAM Jun 07 '13 at 08:09
  • @IAM Yes really. It was different in Sun/Oracle for many years, and you cannot rely on what it may be in any specific release *because it is unspecified.* It can change tomorrow, which would invalidate your answer. – user207421 Sep 24 '16 at 10:49
  • @IAM And 'present in the source code' does not mean 'specified'. *Of course* it is present in the source code. That's the *implementation.* The *Javadoc* is the specification, and it isn't present there. And the part in your quotation about 'a buffer of 8192 characters' **DOES NOT APPEAR IN YOUR LINK**. It is a fabrication. – user207421 Oct 03 '16 at 03:02
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.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • It might be platform dependent, but it's [**8192** on current Android](https://github.com/google/j2objc/blob/master/jre_emul/android/libcore/luni/src/main/java/java/io/BufferedInputStream.java#L44). – pevik Feb 23 '16 at 15:08
  • @Pevik Your link is to `BufferedInputStream`, and the question is about `BufferedReader`. – user207421 Sep 24 '16 at 10:44
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
Community
  • 1
  • 1
7stud
  • 46,922
  • 14
  • 101
  • 127
1

Ans is 8 KB.

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

private static int defaultCharBufferSize = 8192;
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103