Skip to content

Commit 403949d

Browse files
committed
[IO-112] NPE in FileUtils.openOutputStream(File) when file has no parent in path.
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@503497 13f79535-47bb-0310-9956-ffa450edef68
1 parent d981b95 commit 403949d

3 files changed

Lines changed: 34 additions & 6 deletions

File tree

src/java/org/apache/commons/io/FileUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public static FileOutputStream openOutputStream(File file) throws IOException {
169169
}
170170
} else {
171171
File parent = file.getParentFile();
172-
if (parent.exists() == false) {
172+
if (parent != null && parent.exists() == false) {
173173
if (parent.mkdirs() == false) {
174174
throw new IOException("File '" + file + "' could not be created");
175175
}

src/test/org/apache/commons/io/FileUtilsTestCase.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,36 @@ public void test_openInputStream_notExists() throws Exception {
143143
}
144144

145145
//-----------------------------------------------------------------------
146+
void openOutputStream_noParent(boolean createFile) throws Exception {
147+
File file = new File("test.txt");
148+
assertNull(file.getParentFile());
149+
try {
150+
if (createFile) {
151+
createLineBasedFile(file, new String[]{"Hello"});}
152+
FileOutputStream out = null;
153+
try {
154+
out = FileUtils.openOutputStream(file);
155+
out.write(0);
156+
} finally {
157+
IOUtils.closeQuietly(out);
158+
}
159+
assertEquals(true, file.exists());
160+
} finally {
161+
if (file.delete() == false) {
162+
file.deleteOnExit();
163+
}
164+
}
165+
}
166+
167+
public void test_openOutputStream_noParentCreateFile() throws Exception {
168+
openOutputStream_noParent(true);
169+
}
170+
171+
public void test_openOutputStream_noParentNoFile() throws Exception {
172+
openOutputStream_noParent(false);
173+
}
174+
175+
146176
public void test_openOutputStream_exists() throws Exception {
147177
File file = new File(getTestDirectory(), "test.txt");
148178
createLineBasedFile(file, new String[] {"Hello"});

src/test/org/apache/commons/io/testtools/FileBasedTestCase.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,10 @@ protected void generateTestData(OutputStream out, long size)
9393
}
9494

9595
protected void createLineBasedFile(File file, String[] data) throws IOException {
96-
if (!file.getParentFile().exists()) {
97-
throw new IOException("Cannot create file " + file
98-
+ " as the parent directory does not exist");
96+
if (file.getParentFile() != null && !file.getParentFile().exists()) {
97+
throw new IOException("Cannot create file " + file + " as the parent directory does not exist");
9998
}
100-
PrintWriter output = new PrintWriter(
101-
new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
99+
PrintWriter output = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
102100
try {
103101
for (int i = 0; i < data.length; i++) {
104102
output.println(data[i]);

0 commit comments

Comments
 (0)