Improve end-of-file detection in DirCache
When reading from a BufferInputStream attached to a FileInputStream
the available() method seems to return the number of unread bytes
in the buffer plus the unread number of bytes in the file. There is
no guarantee for this behavior, so we can't rely on it.
Instead we read the expected 20 byte SHA1 trailer into a temporary
buffer, and then poll for a remaining byte. If another byte is
still present in the input stream it indicates that there is more
than 20 bytes worth of data remaining, which means an extension
must be available at the current position. If there is in fact
an extension we put back the 21 bytes we read and proceed to do
an 8 byte read for the extension header.
This relies on BufferInputStream's ability to buffer at least 21
bytes of data, and put them all back in the event that we found
an extension. This is a much more common usage of the stream API
and is something we can rely on working correctly all of the time,
on any standard library implementation.
Found-by: Robin Rosenberg <robin.rosenberg@dewire.com>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>