Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jgit-dev] nio exception

On Tue, Mar 6, 2012 at 23:17, Markus Duft <markus.duft@xxxxxxxxxx> wrote:
> On 03/07/2012 03:32 AM, Shawn Pearce wrote:
>> On Tue, Mar 6, 2012 at 05:16, Markus Duft <markus.duft@xxxxxxxxxx> wrote:
> [snip]
>>> any thoughts? i happened to get this for reads of the .git/config file previously sporadically since 1.1,
>> I didn't realize the config file reader used NIO either. I thought
>> that was a simple FileInputStream.
> Yeah, it does use the stream. the code looks like this:
> final FileInputStream in = new FileInputStream(path);
> try {
>        final long sz = in.getChannel().size();
> problematic here is the in.getChannel().size()... this uses a NIO channel to determine the size of the file (lol). This is the step that can fail. I'm not really sure what would be the best solution here, but i think a simple "final long sz = path.length();" would do instead. or is File.length() somehow bad? or more bad than Channel.size()?

Whoops. The problem with path.length() is the inode could have changed
out from under us between when the file was opened, and when the size
was estimated. The only safe way to know is to use the channel to get
at fstat() in the system, or to use the path.length() as an estimate
for the buffer size but be prepared to grow or shrink our buffer if
the actual file length is larger or smaller, as determined by when
read() returns the EOF indicator.  :-)

Back to the top