Search code examples
javabufferedreader

If my buffered reader is reading a line greater than 2 Mb, will it buffer the line or will the buffer size increase?


I know that the default buffer size is 2 Mb, and that's what I'm working with.


Solution

  • Neither. The size of the buffer doesn't change the external behavior of the stream (except indirectly, in terms of how often it will read from the underlying stream). If you try to read more data than is in the buffer, it will drain the buffer and then read the rest directly from the underlying stream. If you readLine and the newline isn't contained within the buffer, it will do the same thing, copying data to the output until it does find the newline, but without any effect on the size of the stream's own buffer.

    Incidentally, the default buffer size isn't 2MB in any implementation I've spotted; classpath uses 8kB, and Oracle seems to use 4kB or 8kB.