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 412bd04e6..fab86025d 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 {