0

I'm reading about Buffer Streams. I searched about it and found many answers that clear my concepts but still have little more questions.

After searching, I have come to know that, Buffer is temporary memory(RAM) which helps program to read data quickly instead hard disk. and when Buffers empty then native input API is called.

After reading little more I got answer from here that is.

Reading data from disk byte-by-byte is very inefficient. One way to speed it up is to use a buffer: instead of reading one byte at a time, you read a few thousand bytes at once, and put them in a buffer, in memory. Then you can look at the bytes in the buffer one by one.

I have two confusion,

1: How/Who data filled in Buffers? (native API how?) as quote above, who filled thousand bytes at once? and it will consume same time. Suppose I have 5MB data, and 5MB loaded once in Buffer in 5 Seconds. and then program use this data from buffer in 5 seconds. Total 10 seconds. But if I skip buffering, then program get direct data from hard disk in 1MB/2sec same as 10Sec total. Please clear my this confusion.

2: The second one how this line works

BufferedReader inputStream = new BufferedReader(new FileReader("xanadu.txt"));

As I'm thinking FileReader write data to buffer, then BufferedReader read data from buffer memory? Also explain this.

Thanks.

Community
  • 1
  • 1

2 Answers2

1

As for the performance of using buffering during read/write, it's probably minimal in impact since the OS will cache too, however buffering will reduce the number of calls to the OS, which will have an impact.

When you add other operations on top, such as character encoding/decoding or compression/decompression, the impact is greater as those operations are more efficient when done in blocks.


You second question said:

As I'm thinking FileReader write data to buffer, then BufferedReader read data from buffer memory? Also explain this.

I believe your thinking is wrong. Yes, technically the FileReader will write data to a buffer, but the buffer is not defined by the FileReader, it's defined by the caller of the FileReader.read(buffer) method.

The operation is initiated from outside, when some code calls BufferedReader.read() (any of the overloads). BufferedReader will then check it's buffer, and if enough data is available in the buffer, it will return the data without involving the FileReader. If more data is needed, the BufferedReader will call the FileReader.read(buffer) method to get the next chunk of data.

It's a pull operation, not a push, meaning the data is pulled out of the readers by the caller.

Andreas
  • 154,647
  • 11
  • 152
  • 247
0

All the stuff is done by a private method named fill() i give you for educational purpose, but all java IDE let you see the source code yourself :

private void fill() throws IOException {
    int dst;
    if (markedChar <= UNMARKED) {
        /* No mark */
        dst = 0;
    } else {
        /* Marked */
        int delta = nextChar - markedChar;
        if (delta >= readAheadLimit) {
            /* Gone past read-ahead limit: Invalidate mark */
            markedChar = INVALIDATED;
            readAheadLimit = 0;
            dst = 0;
        } else {
            if (readAheadLimit <= cb.length) {
                /* Shuffle in the current buffer */
  // here copy the read chars in a memory buffer named cb
                System.arraycopy(cb, markedChar, cb, 0, delta);
                markedChar = 0;
                dst = delta;
            } else {
                /* Reallocate buffer to accommodate read-ahead limit */
                char ncb[] = new char[readAheadLimit];
                System.arraycopy(cb, markedChar, ncb, 0, delta);
                cb = ncb;
                markedChar = 0;
                dst = delta;
            }
            nextChar = nChars = delta;
        }
    }

    int n;
    do {
        n = in.read(cb, dst, cb.length - dst);
    } while (n == 0);
    if (n > 0) {
        nChars = dst + n;
        nextChar = dst;
    }
}
fabien t
  • 358
  • 1
  • 9
  • Sorry I'm unable to clear my confusions from your answer. –  Sep 26 '15 at 17:41
  • Sorry too ! You need to know, that the real stuff do by the computer itself, are native method that are code in C language, and depend of each Operating System, maybe it will removed some confusions ? a simple illustration : public static native void java.lang.System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length); – fabien t Sep 26 '15 at 17:49
  • But what's about my time confusion? –  Sep 26 '15 at 17:50
  • you can see, just one Operation is native and deal with all the data. After each implementation on each Operating System could deal it, so you need to see what it's really do in C. We can be quiet sure that efficiency is done correctly no ? – fabien t Sep 26 '15 at 17:58
  • when fill method called and who called it? –  Sep 26 '15 at 18:04
  • when you call : ´new BufferedReader(new FileReader("xanadu.txt"));´ do nothing, but when you call the read method the private fill() method is called – fabien t Sep 26 '15 at 18:07