diff --git a/hadoop-common-project/hadoop-common/CHANGES.txt b/hadoop-common-project/hadoop-common/CHANGES.txt index 2c1a51d38c..683e8bf6ac 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 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 8020b7a10f..dfa514a6a0 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 @@ 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.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(Arrays.asList(lines))); } else { @@ -72,36 +76,39 @@ public boolean isIn(String ipAddress) { } /** - * 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 lines = new ArrayList(); - 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 lines = new ArrayList(); + 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; }