BufferedReader default buffer size?

南笙酒味 提交于 2019-11-30 15:06:01

问题


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?


回答1:


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;



回答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.




回答3:


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



回答4:


Ans is 8 KB.

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

private static int defaultCharBufferSize = 8192;


来源:https://stackoverflow.com/questions/16973843/bufferedreader-default-buffer-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!