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 zh...@apache.org on 2014/12/24 20:35:53 UTC

[39/50] hadoop git commit: HADOOP-11414. FileBasedIPList#readLines() can leak file descriptors. (ozawa)

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/abfd58d4
Tree: http://git-wip-us.apache.org/repos/asf/hadoop/tree/abfd58d4
Diff: http://git-wip-us.apache.org/repos/asf/hadoop/diff/abfd58d4

Branch: refs/heads/HDFS-EC
Commit: abfd58d447f0da1aed66384342001e64cbe8a72e
Parents: 2df875b
Author: Tsuyoshi Ozawa <oz...@apache.org>
Authored: Mon Dec 22 13:05:13 2014 +0900
Committer: Zhe Zhang <zh...@cloudera.com>
Committed: Wed Dec 24 11:22:18 2014 -0800

----------------------------------------------------------------------
 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/abfd58d4/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/abfd58d4/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;
   }