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

This commit is contained in:
Tsuyoshi Ozawa 2014-12-22 13:05:13 +09:00
parent 7bc0a6d5c2
commit ecf1469fa5
2 changed files with 33 additions and 23 deletions

View File

@ -644,6 +644,9 @@ Release 2.7.0 - UNRELEASED
HADOOP-11429. Findbugs warnings in hadoop extras. HADOOP-11429. Findbugs warnings in hadoop extras.
(Varun Saxena via wheat9) (Varun Saxena via wheat9)
HADOOP-11414. FileBasedIPList#readLines() can leak file descriptors.
(ozawa)
Release 2.6.0 - 2014-11-18 Release 2.6.0 - 2014-11-18
INCOMPATIBLE CHANGES INCOMPATIBLE CHANGES

View File

@ -20,7 +20,6 @@
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.Reader; import java.io.Reader;
@ -30,18 +29,18 @@
import java.util.List; import java.util.List;
import org.apache.commons.io.Charsets; import org.apache.commons.io.Charsets;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; 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. * Thread safe.
*/ */
public class FileBasedIPList implements IPList { public class FileBasedIPList implements IPList {
private static final Log LOG = LogFactory.getLog(FileBasedIPList.class); private static final Log LOG = LogFactory.getLog(FileBasedIPList.class);
@ -51,7 +50,12 @@ public class FileBasedIPList implements IPList {
public FileBasedIPList(String fileName) { public FileBasedIPList(String fileName) {
this.fileName = 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) { if (lines != null) {
addressList = new MachineList(new HashSet<String>(Arrays.asList(lines))); addressList = new MachineList(new HashSet<String>(Arrays.asList(lines)));
} else { } else {
@ -72,36 +76,39 @@ public boolean isIn(String ipAddress) {
} }
/** /**
* reads the lines in a file. * Reads the lines in a file.
* @param fileName * @param fileName
* @return lines in a String array; null if the file does not exist or if the * @return lines in a String array; null if the file does not exist or if the
* file name is null * file name is null
* @throws IOException * @throws IOException
*/ */
private static String[] readLines(String fileName) { private static String[] readLines(String fileName) throws IOException {
try { try {
if (fileName != null) { if (fileName != null) {
File file = new File (fileName); File file = new File (fileName);
if (file.exists()) { if (file.exists()) {
Reader fileReader = new InputStreamReader( try (
new FileInputStream(file), Charsets.UTF_8); Reader fileReader = new InputStreamReader(
BufferedReader bufferedReader = new BufferedReader(fileReader); new FileInputStream(file), Charsets.UTF_8);
List<String> lines = new ArrayList<String>(); BufferedReader bufferedReader = new BufferedReader(fileReader)) {
String line = null; List<String> lines = new ArrayList<String>();
while ((line = bufferedReader.readLine()) != null) { String line = null;
lines.add(line); 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(); } else {
LOG.debug("Loaded IP list of size = " + lines.size() +" from file = " + fileName);
return(lines.toArray(new String[lines.size()]));
}
else {
LOG.debug("Missing ip list file : "+ fileName); LOG.debug("Missing ip list file : "+ fileName);
} }
} }
} } catch (IOException ioe) {
catch (Throwable t) { LOG.error(ioe);
LOG.error(t); throw ioe;
} }
return null; return null;
} }