3

So i'm studying for my upcoming java exam, and on the study guide one of the short answer questions pertains to when to use a Buffer and when not to. I did a pretty comprehensive search on the topic and nothing concrete came up, so I figured I would ask for my own benefit as well as others who may have a similar question.

From what i've gathered, using a buffer is generally preferred as it is less resource expensive than reading from a disk byte-for-byte(which is how files are read from disk without a buffer). Now, my question is, when is it NOT preferred to use a buffer when handling files in java? My best guess would be if the file is extremely small, making the need for a buffer somewhat redundant, but i'm not entirely sure on this.

Also, a quick rundown on what buffering actually is would be awesome(another short answer question). I've read that it is just a space in memory where the data being read/written is stored in large chunks, as apposed to directly on the disk. Is this a correct description? Perhaps too ambiguous? Clarification here would be awesome as well. Thanks :)

FuegoJohnson
  • 372
  • 6
  • 18
  • In a case where the amount of input is consistantly far less than the amount of output, a buffer would not be necessary. Thats really the only logical reason I can think of. After reading into it, I found that although they seem very similar, [a buffer and cache are not the same](http://en.m.wikipedia.org/wiki/Data_buffer#Buffer_versus_cache), so there could be a possibility that they would rather you prefer a file cache instead. This question seems a bit broad; although I'd love to see the answers people have – Vince Nov 17 '14 at 18:27
  • You can go through this **[oracle document](https://docs.oracle.com/javase/7/docs/api/java/nio/Buffer.html)** about Buffer class and concept of buffer. and in **[this question](http://stackoverflow.com/questions/15984789/what-exactly-does-stream-and-buffer-mean-in-java-i-o)** this issue has been discussed. – kriyeta Nov 17 '14 at 18:06
  • I agree the question is pretty broad, potentially allowing for many different types of answers. Aside from the fact that it's a question on my exam, I thought it may allow for a diverse topic on the uses of buffering a file. Thanks for your input though, I appreciate any and all opinions on the topic! – FuegoJohnson Nov 17 '14 at 18:31

1 Answers1

6

Buffering happens between one type media (ie RAM, which is fast but limited in size) and other (hard drive, which is large, but, as all things have 2 sides, a slow one))

For a hard drive controller it doesn't make much difference to write a single byte, or a kilobyte. So, instead of doing, say, 1024 writes of 1 byte each, it is much faster to do a single write of 1024 bytes size.

I think using a buffer may be inefficient (even crucial) when you need to save data persistently as fast as possible, without waiting for the buffer to be filled in, for example to write some sort of log (I know cases where there was a problem to debug Linux kernel panics because the log was not fully written to a file, so developer was not able to see what happened, because of the buffering matter).

Also buffer takes space. Generally buffering is a preferred option. The only thing you need to decide about is the buffer size. Even a non-buffered process can be considered as buffer of size 1 (or shall I say zero?)

You can think about buffering as a concept / pattern to connect medias of different speed. You collect data in a buffer and put it to/from slow media at once. Since it is a slow operation, you can spend this time to collect next bunch of data. Ideally size of your buffer would be consistent with bandwidth you want to archive. Such asynchronized data exchange may significantly increase transmission speed, comparing to non-buffered, blocking operations.

Hope it can add some light on the subject.

ADDED: This post would be inconsistent without a reference to Wikipedia. And here it goes: Data Buffer

Mixaz
  • 4,068
  • 1
  • 29
  • 55
  • Excellent, concise response with an example to boot! Thank you. To recap, not using a buffer may be beneficial when the need to write data persistently as fast as possible? – FuegoJohnson Nov 17 '14 at 18:21
  • I feel like the question on my exam is a little silly quite honestly but it's there, so I felt the discussion was warranted despite this. – FuegoJohnson Nov 17 '14 at 18:22
  • "*When you need to save date persistently as fast as possible*" - After some thought, I'm pretty sure thats the main reason. +1 – Vince Nov 17 '14 at 18:29
  • Yes, I think so. Since, as we know, all things have 2 sides )) - buffer concept has its advantage in speeding up transfer by collecting data in one parcel. From this comes its disadvantage - you need time to collect that data. So, this pattern should not be applied to cases where you can't exchange speed to time. Or when you just do not need speed and do not want to waste time on it, or space in RAM )) Pompously speaking, in this case we can observe one of fundamental rules on which our Universe is being set up - how time can be converted into data transfer speed and vice verse )) – Mixaz Nov 17 '14 at 19:26
  • Well, probably I'm wrong that we can make time from something, at least at this level of technology )) – Mixaz Nov 17 '14 at 19:32