Bug 507657 - Refactor AbstractRepositoryManager's loadIndexFile(URI,
String, IProgressMonitor)

- extract methods to load local and remote p2.index file
- rename method isURL to isInMemoryRepository and drop explanation on
  the caller's side

Change-Id: I9558d1746b4f4433a63040ab8c9cae18da4a6bdd
Signed-off-by: Mykola Nikishov <mn@mn.com.ua>
diff --git a/bundles/org.eclipse.equinox.p2.repository/src/org/eclipse/equinox/internal/p2/repository/helpers/AbstractRepositoryManager.java b/bundles/org.eclipse.equinox.p2.repository/src/org/eclipse/equinox/internal/p2/repository/helpers/AbstractRepositoryManager.java
index 0e190c9..b744934 100644
--- a/bundles/org.eclipse.equinox.p2.repository/src/org/eclipse/equinox/internal/p2/repository/helpers/AbstractRepositoryManager.java
+++ b/bundles/org.eclipse.equinox.p2.repository/src/org/eclipse/equinox/internal/p2/repository/helpers/AbstractRepositoryManager.java
@@ -705,38 +705,37 @@
 	 * a NullSafe version is returned.
 	 */
 	private LocationProperties loadIndexFile(URI location, IProgressMonitor monitor) {
-		LocationProperties locationProperties = LocationProperties.createEmptyIndexFile();
-		//Handle the case of in-memory repos
-		if (!isURL(location))
-			return locationProperties;
+		if (!isInMemoryRepository(location))
+			return LocationProperties.createEmptyIndexFile();
 
-		if ("file".equals(location.getScheme())) { //$NON-NLS-1$ 
-			InputStream localStream = null;
-			try {
-				try {
-					File indexFile = URIUtil.toFile(getIndexFileURI(location));
-					if (indexFile != null && indexFile.exists() && indexFile.canRead()) {
-						localStream = new FileInputStream(indexFile);
-						locationProperties = LocationProperties.create(localStream);
-					}
-				} finally {
-					if (localStream != null)
-						localStream.close();
-				}
-			} catch (IOException e) {
-				//do nothing.
-			}
-			return locationProperties;
+		URI indexFile = getIndexFileURI(location);
+		if ("file".equals(indexFile.getScheme())) { //$NON-NLS-1$
+			return handleLocalIndexFile(indexFile);
 		}
+		return handleRemoteIndexFile(indexFile, monitor);
+	}
 
-		//Handle non local repos (i.e. not file:)
+	private LocationProperties handleRemoteIndexFile(URI indexFileURI, IProgressMonitor monitor) {
 		ByteArrayOutputStream index = new ByteArrayOutputStream();
 		IStatus indexFileStatus = null;
-		indexFileStatus = getTransport().download(getIndexFileURI(location), index, monitor);
+		indexFileStatus = getTransport().download(indexFileURI, index, monitor);
 		if (indexFileStatus != null && indexFileStatus.isOK())
-			locationProperties = LocationProperties.create(new ByteArrayInputStream(index.toByteArray()));
+			return LocationProperties.create(new ByteArrayInputStream(index.toByteArray()));
+		return LocationProperties.createEmptyIndexFile();
+	}
 
-		return locationProperties;
+	private LocationProperties handleLocalIndexFile(URI indexFileURI) {
+		try {
+			File indexFile = URIUtil.toFile(indexFileURI);
+			if (indexFile != null && indexFile.exists() && indexFile.canRead()) {
+				try (InputStream localStream = new FileInputStream(indexFile)) {
+					return LocationProperties.create(localStream);
+				}
+			}
+		} catch (IOException e) {
+			//do nothing.
+		}
+		return LocationProperties.createEmptyIndexFile();
 	}
 
 	/**
@@ -750,7 +749,7 @@
 		return location;
 	}
 
-	private static boolean isURL(URI location) {
+	private static boolean isInMemoryRepository(URI location) {
 		try {
 			new URL(location.toASCIIString());
 		} catch (MalformedURLException e) {