You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by re...@apache.org on 2021/04/09 12:07:13 UTC

[cxf] 01/04: CXF-8442 Java 16 & CachedOutputStream Fix IOException closed after multiple close() & postClose() calls (#771)

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

reta pushed a commit to branch 3.3.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git

commit 0324562f5ce93fa66b03a7a18eadcc8fb18cee76
Author: Michael Auß <54...@users.noreply.github.com>
AuthorDate: Fri Apr 9 00:10:58 2021 +0200

    CXF-8442 Java 16 & CachedOutputStream Fix IOException closed after multiple close() & postClose() calls  (#771)
    
    * CXF-8442 Fix IOException on multiple close() & postClose() calls in CachedOutputStream
    
    * Revert "CXF-8442 Fix IOException on multiple close() & postClose() calls in CachedOutputStream"
    
    This reverts commit 99a6fc19 in CachedOutputStream
    
    * CXF-8442 Fix IOException on multiple close() & postClose() calls in CachedOutputStream by tracking closed status of flowThroughStream
    
    * CXF-8442 Fix IOException on multiple close() & postClose() calls in CachedOutputStream by tracking closed status of flowThroughStream
---
 .../apache/cxf/io/CacheAndWriteOutputStream.java   | 23 +++++++++++++++++-----
 .../cxf/io/CacheAndWriteOutputStreamTest.java      | 12 +++++++++++
 2 files changed, 30 insertions(+), 5 deletions(-)

diff --git a/core/src/main/java/org/apache/cxf/io/CacheAndWriteOutputStream.java b/core/src/main/java/org/apache/cxf/io/CacheAndWriteOutputStream.java
index 0af9145..bc3df0c 100644
--- a/core/src/main/java/org/apache/cxf/io/CacheAndWriteOutputStream.java
+++ b/core/src/main/java/org/apache/cxf/io/CacheAndWriteOutputStream.java
@@ -29,7 +29,7 @@ import java.io.OutputStream;
  *
  */
 public class CacheAndWriteOutputStream extends CachedOutputStream {
-
+    private boolean isClosed = false;
     OutputStream flowThroughStream;
     long count;
     long limit = Long.MAX_VALUE;
@@ -47,19 +47,32 @@ public class CacheAndWriteOutputStream extends CachedOutputStream {
     }
 
     public void closeFlowthroughStream() throws IOException {
-        flowThroughStream.flush();
-        flowThroughStream.close();
+        postClose();
     }
 
     protected void postClose() throws IOException {
-        flowThroughStream.flush();
-        flowThroughStream.close();
+        if (!isClosed) {
+            flowThroughStream.flush();
+            flowThroughStream.close();
+            isClosed = true;
+        }
     }
 
     public OutputStream getFlowThroughStream() {
         return flowThroughStream;
     }
 
+    @Override
+    protected void doClose() throws IOException {
+        super.doClose();
+        isClosed = true;
+    }
+
+    @Override
+    public void close() throws IOException {
+        super.close();
+        isClosed = true;
+    }
 
     @Override
     protected void onWrite() throws IOException {
diff --git a/core/src/test/java/org/apache/cxf/io/CacheAndWriteOutputStreamTest.java b/core/src/test/java/org/apache/cxf/io/CacheAndWriteOutputStreamTest.java
index 32d3e8f..171b6fd 100644
--- a/core/src/test/java/org/apache/cxf/io/CacheAndWriteOutputStreamTest.java
+++ b/core/src/test/java/org/apache/cxf/io/CacheAndWriteOutputStreamTest.java
@@ -18,6 +18,8 @@
  */
 package org.apache.cxf.io;
 
+import org.junit.Test;
+
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 
@@ -38,4 +40,14 @@ public class CacheAndWriteOutputStreamTest extends CachedOutputStreamTest {
     protected Object createCache() {
         return new CacheAndWriteOutputStream(baos);
     }
+
+    @Test
+    public void testCloseMultipleTimes() throws IOException {
+        CacheAndWriteOutputStream cacheAndWriteOutputStream = new CacheAndWriteOutputStream(baos);
+        cacheAndWriteOutputStream.close();
+        cacheAndWriteOutputStream.close();
+        cacheAndWriteOutputStream.close();
+        cacheAndWriteOutputStream.close();
+
+    }
 }