Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

Why does wrapping a FileInputStream in a BufferedInputStream speed up reading a file byte by byte? What is the default buffer size and when is buffering pointless?

Every read() on a bare FileInputStream is a native call, usually a system call into the operating system, and paying that cost for each single byte is very slow. BufferedInputStream reads a large block, 8,192 bytes by default in current JDKs, into an array in memory and serves single-byte reads from that array, so the expensive call happens once per block instead of once per byte. Buffering adds little when we already read in large arrays ourselves.

Why this question is asked

The difference between buffered and unbuffered reading is one of the easiest performance wins in Java, often a factor of a hundred or more, and one of the easiest to lose by accident when someone removes a wrapper or reads a stream one byte at a time. Interviewers ask this question to see whether we understand where the cost of I/O really lies, not just that buffering is good. In this answer, we will recap what a system call is, see what happens on each read with and without a buffer, look at the default buffer size and how to change it, and then discuss when buffering is pointless.

1A Quick Recap: What a read() Really Costs

FileInputStream is a thin wrapper around an operating system file. When we call read() on it, Java makes a native call, which leaves the JVM, crosses into C code in the JDK, and then makes a system call, a request to the operating system kernel to copy data from the file into our memory. A system call involves switching from user mode to kernel mode and back, checks, and bookkeeping. Each one costs somewhere in the region of hundreds of nanoseconds to a few microseconds, regardless of whether we ask for 1 byte or 8,000.

So the cost of reading is mostly per call, not per byte. Reading a 10 MB file one byte at a time means about ten million system calls. Reading it in 8 KB blocks means about 1,300.

2What BufferedInputStream Does on Each Read

BufferedInputStream wraps another InputStream and keeps a byte array, its buffer. On the first read() it asks the underlying stream for a whole buffer's worth of data in one call. It then returns the first byte from its array. The next 8,191 calls to read() are served straight from the array, which is an ordinary memory access taking a few nanoseconds. Only when the array is used up does it make another call to refill it.

Step throughReading bytes through a buffer
Our codeBufferedInputStreamFileInputStreamOS kernel
read() (byte 1)
read(buf, 0, 8192)
system call: read 8192 bytes
8192 bytes into the buffer
byte 1
read() bytes 2 to 8192: served from the array
read() byte 8193: buffer empty, refill
read(buf, 0, 8192)

8 messages in order. Play them, or step through them one at a time.

One system call per 8 KB block instead of one per byte.

Notice that our code did not change; it still calls read() once per byte. The wrapper changed where those calls land. That is the decorator pattern that runs through java.io: a stream adds behaviour by wrapping another stream with the same interface.

3Seeing the Difference in Code

Here is the same byte-by-byte loop with and without the buffer. The timings are typical for a 10 MB file on a laptop with the file in the operating system's cache; exact numbers vary by machine, but the ratio is what matters.

Java
long count(InputStream in) throws IOException {
long n = 0;
while (in.read() != -1) n++;
return n;
}
try (InputStream in = new FileInputStream("data.bin")) {
count(in); // ~10,000,000 native calls, several seconds
}
try (InputStream in = new BufferedInputStream(new FileInputStream("data.bin"))) {
count(in); // ~1,300 native calls, tens of milliseconds
}
new BufferedInputStream(new FileInputStream("data.bin"), 64 * 1024); // custom 64 KB buffer

The default buffer size is 8,192 bytes in current JDKs, and the constructor takes a size if we want a different one. A larger buffer reduces the number of calls further, but returns diminish quickly: going from 8 KB to 64 KB might help on fast storage or network streams with high per-call overhead, while going to megabytes mostly wastes memory, especially when many streams are open at once. The same idea applies to BufferedOutputStream, BufferedReader and BufferedWriter; BufferedReader adds the convenient readLine() as well.

4When Buffering Is Pointless

Buffering helps because it turns many small reads into a few large ones. If our code already reads in large arrays, there is nothing left to merge. When we call in.read(myArray) with a 64 KB array, a FileInputStream makes one system call for up to 64 KB, and a BufferedInputStream in between would only add an extra copy from its array into ours. In fact, BufferedInputStream recognises this case: when the requested length is at least the size of its buffer and its buffer is empty, it reads directly into our array.

Buffering is also redundant when the stream is already in memory, such as a ByteArrayInputStream, and when the API we use does its own buffering. Files.readAllBytes and Files.readString read the whole file efficiently in large chunks, and InputStream.transferTo copies with its own buffer. Wrapping a stream twice in BufferedInputStream adds a second layer of copying with no benefit.

Java
byte[] chunk = new byte[64 * 1024];
try (InputStream in = new FileInputStream("data.bin")) { // no wrapper needed here
int r;
while ((r = in.read(chunk)) != -1) process(chunk, r);
}

5Using the Files API in Practice

In modern code we rarely build these stacks by hand. Files.newBufferedReader(path, UTF_8) returns a BufferedReader ready for text with an explicit charset. Files.newInputStream(path) returns an unbuffered channel-based stream, which is ideal for reading into our own large arrays and which we wrap in a BufferedInputStream only if we really read single bytes or small pieces, for example when parsing a binary format field by field. Files.lines and Files.readString cover the common text cases.

6Follow-up questions

Does buffering make output faster too?

Yes, for the same reason. Each write() on a FileOutputStream is a system call, so writing small pieces through BufferedOutputStream or BufferedWriter turns them into a few large writes. The catch on the output side is that data sits in the buffer until it fills, until flush() or until close(), so we must close the stream, or the last part of the output is lost.

Is the operating system's page cache not already a buffer?

It is, and it is why repeated reads of the same file are fast. But the page cache sits inside the kernel, so every read() still needs a system call to copy data from it into our process. BufferedInputStream reduces the number of those crossings; the page cache reduces the number of trips to the disk. They solve different costs and work together.

7Conclusion

In this answer, we saw that the cost of reading lies mostly in each native call, so reading byte by byte from a FileInputStream makes millions of system calls. BufferedInputStream reads 8,192 bytes at a time by default and serves single reads from memory, cutting the calls by thousands of times; the size can be set in the constructor. Buffering adds little when we already read into large arrays, when the data is in memory, or when an API such as Files.readAllBytes already buffers for us.

What to say in the interviewAbout 90 seconds aloud

Open with

Each read on a bare FileInputStream is a native call that usually turns into a system call, a switch into the kernel to copy data, and that costs roughly the same whether we ask for one byte or eight thousand.

Then, if they want more

So reading a 10 MB file one byte at a time is about ten million system calls. BufferedInputStream wraps the stream and keeps a byte array.

The first read fills the array with one call, then the following reads are served straight from memory, which is a few nanoseconds each, and it only goes back to the file when the array is empty. So we get one system call per block instead of one per byte, which is easily a hundred times faster.

The default buffer is 8,192 bytes in current JDKs, and the constructor takes a size if I want a bigger one, say 64 KB for a slow or high-latency stream, though returns diminish quickly.

Buffering is pointless when I already read into large arrays myself, because then each call already moves a large block and the buffer only adds a copy; BufferedInputStream even bypasses its buffer for large reads. It is also pointless on in-memory streams or with APIs that buffer internally, like Files.readAllBytes.

In practice I use Files.newBufferedReader with a charset for text and Files.newInputStream for bulk binary reads.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Why does wrapping a FileInputStream in a BufferedInputStream speed up reading a file...": https://scrivio-interview-library.netlify.app/java/io-and-nio/d1e21bcdfd/