You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2021/12/22 15:38:02 UTC

[commons-vfs] branch master updated: [VFS-812] Don't throw FileSystemException when closing file content output stream using a BufferedOutputStream #228.

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 2a1d6b8  [VFS-812] Don't throw FileSystemException when closing file content output stream using a BufferedOutputStream #228.
2a1d6b8 is described below

commit 2a1d6b89eb0cad49eaba613d503b02e04f0fd2b7
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Wed Dec 22 10:37:57 2021 -0500

    [VFS-812] Don't throw FileSystemException when closing file content
    output stream using a BufferedOutputStream #228.
    
    Applied changes based on PR #228 from XenoAmess:
    - Remove cruft from unit test that was not used.
    - Changed internal instance variable name from 'finished' to 'closed'
    - Extract 'closed' usage into a private method.
---
 .../commons/vfs2/util/MonitorOutputStream.java     | 14 +++++--
 .../FileObjectContentOutputStreamCloseTest.java    | 44 ++++++++++++++++++++++
 src/changes/changes.xml                            |  3 ++
 3 files changed, 57 insertions(+), 4 deletions(-)

diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/util/MonitorOutputStream.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/util/MonitorOutputStream.java
index af6bece..934eaa9 100644
--- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/util/MonitorOutputStream.java
+++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/util/MonitorOutputStream.java
@@ -28,7 +28,7 @@ import org.apache.commons.vfs2.FileSystemException;
  */
 public class MonitorOutputStream extends BufferedOutputStream {
 
-    private final AtomicBoolean finished = new AtomicBoolean(false);
+    private final AtomicBoolean closed = new AtomicBoolean(false);
 
     /**
      * Constructs a MonitorOutputStream from the passed OutputStream.
@@ -61,7 +61,7 @@ public class MonitorOutputStream extends BufferedOutputStream {
      * @since 2.0
      */
     protected void assertOpen() throws FileSystemException {
-        if (finished.get()) {
+        if (isClosed()) {
             throw new FileSystemException("vfs.provider/closed.error");
         }
     }
@@ -83,7 +83,7 @@ public class MonitorOutputStream extends BufferedOutputStream {
         // do not use super.close()
         // on Java 8 it might throw self suppression, see JDK-8042377
         // in older Java it silently ignores flush() errors
-        if (finished.getAndSet(true)) {
+        if (closed.getAndSet(true)) {
             return;
         }
 
@@ -121,10 +121,16 @@ public class MonitorOutputStream extends BufferedOutputStream {
      */
     @Override
     public synchronized void flush() throws IOException {
-        assertOpen();
+        if (isClosed()) {
+            return;
+        }
         super.flush();
     }
 
+    private boolean isClosed() {
+        return closed.get();
+    }
+
     /**
      * Called after this stream is closed.
      * <p>
diff --git a/commons-vfs2/src/test/java/org/apache/commons/vfs2/FileObjectContentOutputStreamCloseTest.java b/commons-vfs2/src/test/java/org/apache/commons/vfs2/FileObjectContentOutputStreamCloseTest.java
new file mode 100644
index 0000000..47392c3
--- /dev/null
+++ b/commons-vfs2/src/test/java/org/apache/commons/vfs2/FileObjectContentOutputStreamCloseTest.java
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.vfs2;
+
+import java.io.BufferedOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+import org.junit.Test;
+
+public class FileObjectContentOutputStreamCloseTest {
+
+    @Test
+    public void test() throws IOException {
+        Path tempFilePath = Files.createTempFile("org.apache.commons.vfs2", ".txt");
+        try (@SuppressWarnings("resource") // VFS.getManager() is a constant.
+        FileObject fileObject = VFS.getManager().resolveFile(tempFilePath.toUri());
+                final FileContent content = fileObject.getContent();
+                OutputStream outputStream = content.getOutputStream();
+                BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
+                OutputStreamWriter outputStreamWriter = new OutputStreamWriter(bufferedOutputStream)) {
+            outputStreamWriter.write("org.apache.commons.vfs2");
+        }
+    }
+
+}
\ No newline at end of file
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 1d9d088..44cd852 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -80,6 +80,9 @@ The <action> type attribute can be add,update,fix,remove.
       <action type="fix" issue="VFS-811" dev="ggregory" due-to="Woonsan Ko, Gary Gregory">
         Javadoc API links are broken in the commons-vfs project site #227.
       </action>
+      <action type="fix" issue="VFS-812" dev="ggregory" due-to="XenoAmess, Gary Gregory">
+        Don't throw FileSystemException when closing file content output stream using a BufferedOutputStream #228.
+      </action>
       <!-- ADD -->
       <action type="add" dev="ggregory" due-to="Seth Falco">
         Add vscode files to gitignore #205.