HADOOP-7342. Add an utility API in FileUtil for JDK File.list avoid NPEs on File.list(). Contributed by Bharath Mundlapudi.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1131330 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Matthew Foley 2011-06-04 06:50:44 +00:00
parent babd19de33
commit a94b6a0529
3 changed files with 49 additions and 0 deletions

View File

@ -12,6 +12,9 @@ Trunk (unreleased changes)
NEW FEATURES
HADOOP-7342. Add an utility API in FileUtil for JDK File.list
avoid NPEs on File.list() (Bharath Mundlapudi via mattf)
HADOOP-7322. Adding a util method in FileUtil for directory listing,
avoid NPEs on File.listFiles() (Bharath Mundlapudi via mattf)

View File

@ -728,4 +728,23 @@ public class FileUtil {
}
return files;
}
/**
* A wrapper for {@link File#list()}. This java.io API returns null
* when a dir is not a directory or for any I/O error. Instead of having
* null check everywhere File#list() is used, we will add utility API
* to get around this problem. For the majority of cases where we prefer
* an IOException to be thrown.
* @param dir directory for which listing should be performed
* @return list of file names or empty string list
* @exception IOException for invalid directory or for a bad disk.
*/
public static String[] list(File dir) throws IOException {
String[] fileNames = dir.list();
if(fileNames == null) {
throw new IOException("Invalid directory or I/O error occurred for dir: "
+ dir.toString());
}
return fileNames;
}
}

View File

@ -143,6 +143,33 @@ public class TestFileUtil {
//Expected an IOException
}
}
@Test
public void testListAPI() throws IOException {
setupDirs();
//Test existing files case
String[] files = FileUtil.list(partitioned);
Assert.assertEquals("Unexpected number of pre-existing files", 2, files.length);
//Test existing directory with no files case
File newDir = new File(tmp.getPath(),"test");
newDir.mkdir();
Assert.assertTrue("Failed to create test dir", newDir.exists());
files = FileUtil.list(newDir);
Assert.assertEquals("New directory unexpectedly contains files", 0, files.length);
newDir.delete();
Assert.assertFalse("Failed to delete test dir", newDir.exists());
//Test non-existing directory case, this throws
//IOException
try {
files = FileUtil.list(newDir);
Assert.fail("IOException expected on list() for non-existent dir "
+ newDir.toString());
} catch(IOException ioe) {
//Expected an IOException
}
}
@After
public void tearDown() throws IOException {