[oli]: What I meant by this:
if the whole file is not read, -1 (or EOF) will never be reached.
is that if you allocate the size of the file to the buffer and then loop until the read method finds -1, then this will never happen because the EOF marker is in fact fc.size() + EOFMarkerLength.
But you were talking about the second code sample. In this case (a) the buffer size is
one tenth of the file size, and (b) regardless of the file size,
you're in a loop and you will eventually reach the -1
if you empty the buffer on each iteration, but (c) I can't tell if this will happen because your comment "// Fill the buffer" does not make any sense to me. The buffer is already full; if you want to use it again on the next iteration of the loop, it needs to be emptied. It's like you've got a big pile of dirt that you want to move somewhere, and you've filled up a wheelbarrow with dirt. You can't put any more dirt in the wheelbarrow unless you first do something with the dirt already in it.
Your comments about a large also worry me a bit and make me want to reconsider the use of the first example. Would it be acceptable then to use this: No, I think the first version is preferable. That last byte will never have any value read into it; it will always be 0. You're effectively adding a zero to the end of the file. Hopefully you'll just ignore this in whatever subsequent processing you do, but there's also a chance it will cause confusion later when you or someone else forgets there's an extra zero. I don't see any benefit in appending the extra zero.
The original code sample if fine. There's no need to actually read the EOF if you're sure you've read the full file size worth of data. EOF is not data; it's a signal that there is no more data. If you already know how much data there should be (thanks to fc.size()) and you've read all that, then you don't really need to do one more read to see the -1.
Hope that helps...
[ February 10, 2004: Message edited by: Jim Yingst ]