From c581672557f0fb014babeb79c15d7f79fb206908 Mon Sep 17 00:00:00 2001
From: "Shawn O. Pearce" <spearce@spearce.org>
Date: Wed, 3 Feb 2010 08:23:34 -0800
Subject: [PATCH] Ensure RawText closes the FileInputStream when read is
 complete

Rather than implementing the file reading logic ourselves, and
wind up leaking the FileInputStream's file descriptor until the
next GC, use IO.readFully(File) which wraps the read loop inside
of a try/finally to ensure the stream is closed before it exits.

Change-Id: I85a3fe87d5eff88fa788962004aebe19d2e91bb4
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Reviewed-by: Roland Grunberg <rgrunber@redhat.com>
---
 .../src/org/eclipse/jgit/diff/RawText.java       | 16 ++--------------
 1 file changed, 2 insertions(+), 14 deletions(-)

diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/diff/RawText.java b/org.eclipse.jgit/src/org/eclipse/jgit/diff/RawText.java
index 9a206af19..c785534fb 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/diff/RawText.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/diff/RawText.java
@@ -45,10 +45,10 @@
 package org.eclipse.jgit.diff;
 
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.OutputStream;
 
+import org.eclipse.jgit.util.IO;
 import org.eclipse.jgit.util.IntList;
 import org.eclipse.jgit.util.RawParseUtils;
 
@@ -99,7 +99,7 @@ public RawText(final byte[] input) {
 	 * @throws IOException if Exceptions occur while reading the file
 	 */
 	public RawText(File file) throws IOException {
-		this(readFile(file));
+		this(IO.readFully(file));
 	}
 
 	public int size() {
@@ -202,16 +202,4 @@ protected int hashLine(final byte[] raw, int ptr, final int end) {
 			hash = (hash << 5) ^ (raw[ptr] & 0xff);
 		return hash;
 	}
-
-	private static byte[] readFile(File file) throws IOException {
-		byte[] result = new byte[(int)file.length()];
-		FileInputStream in = new FileInputStream(file);
-		for (int off = 0; off < result.length; ) {
-			int read = in.read(result, off, result.length - off);
-			if (read < 0)
-				throw new IOException("Early EOF");
-			off += read;
-		}
-		return result;
-	}
 }
-- 
GitLab