diff options
author | Markus Duft <markus.duft@salomon.at> | 2012-04-02 07:27:19 +0200 |
---|---|---|
committer | Markus Duft <markus.duft@salomon.at> | 2012-04-02 07:27:25 +0200 |
commit | 2ba67bedaebc1df9e891d701aa46bde753924a64 (patch) | |
tree | 7d40944d8dffd23697533a8d64cf9d8dc2dd279c /org.eclipse.jgit/src/org/eclipse/jgit/util | |
parent | 6189a68d1d48e38779380cea81efa530405ff762 (diff) | |
download | jgit-2ba67bedaebc1df9e891d701aa46bde753924a64.tar.gz jgit-2ba67bedaebc1df9e891d701aa46bde753924a64.zip |
Don't use java.nio channel for file size determination
Java NIO has some problems (like files closing unexpectedly because the
thread was interrupted). To avoid those problems, don't use a NIO
channel to determine the size of a file, but rather ask the File itself.
We have to be prepared to handle wrong/outdated information in this case
too, as the inode of the File may change between opening and determining
file size.
Change-Id: Ic7aa6c3337480879efcce4a3058b548cd0e2cef0
Diffstat (limited to 'org.eclipse.jgit/src/org/eclipse/jgit/util')
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/util/IO.java | 32 |
1 files changed, 29 insertions, 3 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/IO.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/IO.java index 412bd04e67..fab86025dc 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/util/IO.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/IO.java @@ -137,12 +137,38 @@ public class IO { throws FileNotFoundException, IOException { final FileInputStream in = new FileInputStream(path); try { - final long sz = in.getChannel().size(); + long sz = Math.max(path.length(), 1); if (sz > max) throw new IOException(MessageFormat.format( JGitText.get().fileIsTooLarge, path)); - final byte[] buf = new byte[(int) sz]; - IO.readFully(in, buf, 0); + + byte[] buf = new byte[(int) sz]; + int valid = 0; + for (;;) { + if (buf.length == valid) { + if (buf.length == max) { + int next = in.read(); + if (next < 0) + break; + + throw new IOException(MessageFormat.format( + JGitText.get().fileIsTooLarge, path)); + } + + byte[] nb = new byte[Math.min(buf.length * 2, max)]; + System.arraycopy(buf, 0, nb, 0, valid); + buf = nb; + } + int n = in.read(buf, valid, buf.length - valid); + if (n < 0) + break; + valid += n; + } + if (valid < buf.length) { + byte[] nb = new byte[valid]; + System.arraycopy(buf, 0, nb, 0, valid); + buf = nb; + } return buf; } finally { try { |