views:

317

answers:

2

If I have the following example file where each number is represents a byte (123 has bytes 1, 2, and 3):

123456789

Let's say I create a FileInputStream. This reads in the binary byte by byte. So .read() returns 1, then 2, etc. Now let's say I create a buffer. The initial chunk it reads in (if I understand buffers correctly) is 1-5. This allows it to not only read in byte by byte, but in the case of characters whole lines, etc. But if I hit .read() again, I start at 6, and NOT where the BufferedReader stopped (so if 3 is a line break, and I told the BufferedReader to print the first line, it prints 1-2, and then using .read() from the FileInputStream gives me 6, and not 3.)

In order to be able to parse data by the delimiter, does a Scanner implicitly create a buffer like how a BufferedReader creates a buffer so that it can find line breaks, etc? And if I pass a separate FileInputStream into the Scanner, using .read() will NOT print the first byte following the first delimiter the scanner found, but rather at the end of the "chunk" the scanner took?

+2  A: 

Yes.

From the java.util.Scanner code:

// Internal buffer used to hold input
private CharBuffer buf;

// Size of internal character buffer
private static final int BUFFER_SIZE = 1024; // change to 1024;
Yuval A
+1  A: 

It's not obvious, but if you look at the API doc of Scanner carefully, you see that it's based on the Readable interface - and the only method in that interface is based on a buffer. So yes, Scanner implicitly creates a buffer.

Michael Borgwardt