You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-commits@hadoop.apache.org by oz...@apache.org on 2014/12/22 05:07:42 UTC

hadoop git commit: HADOOP-11414. FileBasedIPList#readLines() can leak file descriptors. (ozawa)

Repository: hadoop
Updated Branches:
  refs/heads/trunk 7bc0a6d5c -> ecf1469fa


HADOOP-11414. FileBasedIPList#readLines() can leak file descriptors. (ozawa)


Project: http://git-wip-us.apache.org/repos/asf/hadoop/repo
Commit: http://git-wip-us.apache.org/repos/asf/hadoop/commit/ecf1469f
Tree: http://git-wip-us.apache.org/repos/asf/hadoop/tree/ecf1469f
Diff: http://git-wip-us.apache.org/repos/asf/hadoop/diff/ecf1469f

Branch: refs/heads/trunk
Commit: ecf1469fa51f9977f70652fac313d4a9ec1eb86f
Parents: 7bc0a6d
Author: Tsuyoshi Ozawa <oz...@apache.org>
Authored: Mon Dec 22 13:05:13 2014 +0900
Committer: Tsuyoshi Ozawa <oz...@apache.org>
Committed: Mon Dec 22 13:05:13 2014 +0900

----------------------------------------------------------------------
 hadoop-common-project/hadoop-common/CHANGES.txt |  3 ++
 .../org/apache/hadoop/util/FileBasedIPList.java | 53 +++++++++++---------
 2 files changed, 33 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hadoop/blob/ecf1469f/hadoop-common-project/hadoop-common/CHANGES.txt
----------------------------------------------------------------------
diff --git a/hadoop-common-project/hadoop-common/CHANGES.txt b/hadoop-common-project/hadoop-common/CHANGES.txt
index 2c1a51d..683e8bf 100644
--- a/hadoop-common-project/hadoop-common/CHANGES.txt
+++ b/hadoop-common-project/hadoop-common/CHANGES.txt
@@ -644,6 +644,9 @@ Release 2.7.0 - UNRELEASED
     HADOOP-11429. Findbugs warnings in hadoop extras.
     (Varun Saxena via wheat9)
 
+    HADOOP-11414. FileBasedIPList#readLines() can leak file descriptors.
+    (ozawa)
+
 Release 2.6.0 - 2014-11-18
 
   INCOMPATIBLE CHANGES

http://git-wip-us.apache.org/repos/asf/hadoop/blob/ecf1469f/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/FileBasedIPList.java
----------------------------------------------------------------------
diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/FileBasedIPList.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/FileBasedIPList.java
index 8020b7a..dfa514a 100644
--- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/FileBasedIPList.java
+++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/FileBasedIPList.java
@@ -20,7 +20,6 @@ package org.apache.hadoop.util;
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileInputStream;
-import java.io.FileReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.Reader;
@@ -30,18 +29,18 @@ import java.util.HashSet;
 import java.util.List;
 
 import org.apache.commons.io.Charsets;
-import org.apache.commons.io.IOUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
 /**
- * FileBasedIPList loads a list of subnets in CIDR format and ip addresses from a file.
+ * FileBasedIPList loads a list of subnets in CIDR format and ip addresses from
+ * a file.
  *
- * Given an ip address, isIn  method returns true if ip belongs to one of the subnets.
+ * Given an ip address, isIn  method returns true if ip belongs to one of the
+ * subnets.
  *
  * Thread safe.
  */
-
 public class FileBasedIPList implements IPList {
 
   private static final Log LOG = LogFactory.getLog(FileBasedIPList.class);
@@ -51,7 +50,12 @@ public class FileBasedIPList implements IPList {
 
   public FileBasedIPList(String fileName) {
     this.fileName = fileName;
-    String[] lines = readLines(fileName);
+    String[] lines = new String[0];
+    try {
+      lines = readLines(fileName);
+    } catch (IOException e) {
+      lines = null;
+    }
     if (lines != null) {
       addressList = new MachineList(new HashSet<String>(Arrays.asList(lines)));
     } else {
@@ -72,36 +76,39 @@ public class FileBasedIPList implements IPList {
   }
 
   /**
-   * reads the lines in a file.
+   * Reads the lines in a file.
    * @param fileName
    * @return lines in a String array; null if the file does not exist or if the
    * file name is null
    * @throws IOException
    */
-  private static String[] readLines(String fileName) {
+  private static String[] readLines(String fileName) throws IOException {
     try {
       if (fileName != null) {
         File file = new File (fileName);
         if (file.exists()) {
-          Reader fileReader = new InputStreamReader(
-              new FileInputStream(file), Charsets.UTF_8);
-          BufferedReader bufferedReader = new BufferedReader(fileReader);
-          List<String> lines = new ArrayList<String>();
-          String line = null;
-          while ((line = bufferedReader.readLine()) != null) {
-            lines.add(line);
+          try (
+              Reader fileReader = new InputStreamReader(
+                  new FileInputStream(file), Charsets.UTF_8);
+              BufferedReader bufferedReader = new BufferedReader(fileReader)) {
+            List<String> lines = new ArrayList<String>();
+            String line = null;
+            while ((line = bufferedReader.readLine()) != null) {
+              lines.add(line);
+            }
+            if (LOG.isDebugEnabled()) {
+              LOG.debug("Loaded IP list of size = " + lines.size() +
+                  " from file = " + fileName);
+            }
+            return (lines.toArray(new String[lines.size()]));
           }
-          bufferedReader.close();
-          LOG.debug("Loaded IP list of size = " + lines.size() +" from file = " + fileName);
-          return(lines.toArray(new String[lines.size()]));
-        }
-        else {
+        } else {
           LOG.debug("Missing ip list file : "+ fileName);
         }
       }
-    }
-    catch (Throwable t) {
-      LOG.error(t);
+    } catch (IOException ioe) {
+      LOG.error(ioe);
+      throw ioe;
     }
     return null;
   }