Skip to content

Commit 5899f1e

Browse files
committed
[IO-547] Throw a IllegalArgumentException instead of
NullPointerException in FileSystemUtils.freeSpaceWindows().
1 parent 92a07f9 commit 5899f1e

2 files changed

Lines changed: 12 additions & 6 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ The <action> type attribute can be add,update,fix,remove.
5050
<action issue="IO-542" dev="pschumacher" type="update" due-to="Ilmars Poikans">
5151
FileUtils#readFileToByteArray: optimize reading of files with known size
5252
</action>
53+
<action issue="IO-547" dev="ggregory" type="update" due-to="Nikhil Shinde, Michael Ernst, Gary Greory">
54+
Throw a IllegalArgumentException instead of NullPointerException in FileSystemUtils.freeSpaceWindows().
55+
</action>
5356
<action issue="IO-367" dev="pschumacher" type="add" due-to="James Sawle">
5457
Add convenience methods for copyToDirectory
5558
</action>

src/main/java/org/apache/commons/io/FileSystemUtils.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -294,13 +294,16 @@ long freeSpaceOS(final String path, final int os, final boolean kb, final long t
294294
* @throws IOException if an error occurs
295295
*/
296296
long freeSpaceWindows(String path, final long timeout) throws IOException {
297-
path = FilenameUtils.normalize(path, false);
298-
if (path.length() > 0 && path.charAt(0) != '"') {
299-
path = "\"" + path + "\"";
297+
String normPath = FilenameUtils.normalize(path, false);
298+
if (normPath == null) {
299+
throw new IllegalArgumentException(path);
300+
}
301+
if (normPath.length() > 0 && normPath.charAt(0) != '"') {
302+
normPath = "\"" + normPath + "\"";
300303
}
301304

302305
// build and run the 'dir' command
303-
final String[] cmdAttribs = new String[] {"cmd.exe", "/C", "dir /a /-c " + path};
306+
final String[] cmdAttribs = new String[] {"cmd.exe", "/C", "dir /a /-c " + normPath};
304307

305308
// read in the output of the command to an ArrayList
306309
final List<String> lines = performCommand(cmdAttribs, Integer.MAX_VALUE, timeout);
@@ -312,13 +315,13 @@ long freeSpaceWindows(String path, final long timeout) throws IOException {
312315
for (int i = lines.size() - 1; i >= 0; i--) {
313316
final String line = lines.get(i);
314317
if (line.length() > 0) {
315-
return parseDir(line, path);
318+
return parseDir(line, normPath);
316319
}
317320
}
318321
// all lines are blank
319322
throw new IOException(
320323
"Command line 'dir /-c' did not return any info " +
321-
"for path '" + path + "'");
324+
"for path '" + normPath + "'");
322325
}
323326

324327
/**

0 commit comments

Comments
 (0)