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.
(Varun Saxena via wheat9)
HADOOP-11414. FileBasedIPList#readLines() can leak file descriptors.
(ozawa)
Release 2.6.0 - 2014-11-18
INCOMPATIBLE CHANGES

View File

@ -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<String>(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<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;
}