Skip to content

Commit 2e1c7cd

Browse files
committed
Refactor AbstractFileObject#getOutputStream() #151
Update patch to avoid some possible if unlikely NPEs.
1 parent 54bb2de commit 2e1c7cd

3 files changed

Lines changed: 14 additions & 11 deletions

File tree

commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/AbstractFileObject.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,13 @@ public void createFile() throws FileSystemException {
319319
}
320320

321321
if (!exists()) {
322-
getOutputStream().close();
323-
endOutput();
322+
try (FileContent content = getContent()) {
323+
if (content != null) {
324+
try (OutputStream outputStream = content.getOutputStream()) {
325+
// Avoids NPE on OutputStream#close()
326+
}
327+
}
328+
}
324329
}
325330
} catch (final RuntimeException re) {
326331
throw re;
@@ -1227,6 +1232,7 @@ public FileName getName() {
12271232
return fileName;
12281233
}
12291234

1235+
// TODO: remove this method for the next major version as it is unused
12301236
/**
12311237
* Prepares this file for writing. Makes sure it is either a file, or its parent folder exists. Returns an output
12321238
* stream to use to write the content of the file to.
@@ -1238,6 +1244,8 @@ public OutputStream getOutputStream() throws FileSystemException {
12381244
return getOutputStream(false);
12391245
}
12401246

1247+
// TODO: mark this method as `final` and package-private for the next major version because
1248+
// it shouldn't be used from anywhere other than `DefaultFileContent`
12411249
/**
12421250
* Prepares this file for writing. Makes sure it is either a file, or its parent folder exists. Returns an output
12431251
* stream to use to write the content of the file to.

commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ram/RamFileOutputStream.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
import java.io.IOException;
2020
import java.io.OutputStream;
2121

22-
import org.apache.commons.vfs2.FileSystemException;
23-
2422
/**
2523
* OutputStream to a RamFile.
2624
*/
@@ -57,13 +55,7 @@ public void close() throws IOException {
5755
if (exception != null) {
5856
throw exception;
5957
}
60-
try {
61-
this.closed = true;
62-
// Close the
63-
this.file.endOutput();
64-
} catch (final Exception e) {
65-
throw new FileSystemException(e);
66-
}
58+
this.closed = true;
6759
}
6860

6961
@Override

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ The <action> type attribute can be add,update,fix,remove.
8989
<action type="fix" dev="ggregory" due-to="Arturo Bernal">
9090
Simplify conditions and avoid extra checks #253.
9191
</action>
92+
<action type="fix" dev="ggregory" due-to="Boris Petrov, Gary Gregory">
93+
Refactor AbstractFileObject#getOutputStream() #151.
94+
</action>
9295
<!-- ADD -->
9396
<action type="add" dev="ggregory" due-to="Seth Falco">
9497
Add vscode files to gitignore #205.

0 commit comments

Comments
 (0)