This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-vfs.git


The following commit(s) were added to refs/heads/master by this push:
     new 2e1c7cd9 Refactor AbstractFileObject#getOutputStream() #151
2e1c7cd9 is described below

commit 2e1c7cd998861ea797bbbd0ca3c8f6c1367f768d
Author: Gary Gregory <[email protected]>
AuthorDate: Wed Apr 20 11:20:33 2022 -0400

    Refactor AbstractFileObject#getOutputStream() #151
    
    Update patch to avoid some possible if unlikely NPEs.
---
 .../org/apache/commons/vfs2/provider/AbstractFileObject.java | 12 ++++++++++--
 .../commons/vfs2/provider/ram/RamFileOutputStream.java       | 10 +---------
 src/changes/changes.xml                                      |  3 +++
 3 files changed, 14 insertions(+), 11 deletions(-)

diff --git 
a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/AbstractFileObject.java
 
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/AbstractFileObject.java
index 6f665435..497acd22 100644
--- 
a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/AbstractFileObject.java
+++ 
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/AbstractFileObject.java
@@ -319,8 +319,13 @@ public abstract class AbstractFileObject<AFS extends 
AbstractFileSystem> impleme
                 }
 
                 if (!exists()) {
-                    getOutputStream().close();
-                    endOutput();
+                    try (FileContent content = getContent()) {
+                        if (content != null) {
+                            try (OutputStream outputStream = 
content.getOutputStream()) {
+                                // Avoids NPE on OutputStream#close()
+                            }
+                        }
+                    }
                 }
             } catch (final RuntimeException re) {
                 throw re;
@@ -1227,6 +1232,7 @@ public abstract class AbstractFileObject<AFS extends 
AbstractFileSystem> impleme
         return fileName;
     }
 
+    // TODO: remove this method for the next major version as it is unused
     /**
      * Prepares this file for writing. Makes sure it is either a file, or its 
parent folder exists. Returns an output
      * stream to use to write the content of the file to.
@@ -1238,6 +1244,8 @@ public abstract class AbstractFileObject<AFS extends 
AbstractFileSystem> impleme
         return getOutputStream(false);
     }
 
+    // TODO: mark this method as `final` and package-private for the next 
major version because
+    // it shouldn't be used from anywhere other than `DefaultFileContent`
     /**
      * Prepares this file for writing. Makes sure it is either a file, or its 
parent folder exists. Returns an output
      * stream to use to write the content of the file to.
diff --git 
a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ram/RamFileOutputStream.java
 
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ram/RamFileOutputStream.java
index ea23e7eb..3860e663 100644
--- 
a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ram/RamFileOutputStream.java
+++ 
b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ram/RamFileOutputStream.java
@@ -19,8 +19,6 @@ package org.apache.commons.vfs2.provider.ram;
 import java.io.IOException;
 import java.io.OutputStream;
 
-import org.apache.commons.vfs2.FileSystemException;
-
 /**
  * OutputStream to a RamFile.
  */
@@ -57,13 +55,7 @@ public class RamFileOutputStream extends OutputStream {
         if (exception != null) {
             throw exception;
         }
-        try {
-            this.closed = true;
-            // Close the
-            this.file.endOutput();
-        } catch (final Exception e) {
-            throw new FileSystemException(e);
-        }
+        this.closed = true;
     }
 
     @Override
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 586d4d64..77cfbaf9 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -89,6 +89,9 @@ The <action> type attribute can be add,update,fix,remove.
       <action type="fix" dev="ggregory" due-to="Arturo Bernal">
         Simplify conditions and avoid extra checks #253.
       </action>
+      <action type="fix" dev="ggregory" due-to="Boris Petrov, Gary Gregory">
+        Refactor AbstractFileObject#getOutputStream() #151.
+      </action>
       <!-- ADD -->
       <action type="add" dev="ggregory" due-to="Seth Falco">
         Add vscode files to gitignore #205.

Reply via email to