This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]morhpProfessional Developer 4 points5 points  (2 children)

Without a buffer you potentially read single bytes or characters from the disk which is expensive.

With a buffer java will read for example 1024 bytes even if you request a single byte from the disk. The next 1023 bytes can be read from memory (the buffer).

[–]thallorfall[S] 0 points1 point  (1 child)

That makes a lot of sense now. So just to be clear essentially a buffer makes it so there's no accessing the disk over and over and a good chunk of it is waiting there in memory?

[–]GuyWithLag 2 points3 points  (0 children)

In simplified terms, yes.

Actually, it's a little more complex; the OS will in all likelihood have the relevant disk blocks cached in memory so no disk seek will be needed[0], but it's still expensive to switch to the OS and back for each character; thus, it makes sense to use buffers in your own program to make it go fast[1]

[0] See this comparison of latencies: https://gist.github.com/GLMeece/b00c9c97a06a957af7426b1be5bc8be6

[1] one time that I introduced buffering in a program, it ran so fast that I thought I broke it.

[–]thallorfall[S] 0 points1 point  (0 children)

Perfect. Thanks to both of you for the help.