Improve end-of-file detection in DirCache
commit1fcb96952f5ca6c2d9338b1119a44a8d575dfaeb
authorShawn O. Pearce <spearce@spearce.org>
Thu, 9 Apr 2009 15:28:18 +0000 (9 08:28 -0700)
committerRobin Rosenberg <robin.rosenberg@dewire.com>
Fri, 10 Apr 2009 02:33:10 +0000 (10 04:33 +0200)
treee8fa4d4d34f98a8441d4ef9ccdb879044353c272
parent31da82af81c54363442f434378d1dcfa080216d1
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>
org.spearce.jgit/src/org/spearce/jgit/dircache/DirCache.java