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-compress.git

commit aa27676340dacf42526fd3bf6e424442c74985b2
Author: Gary Gregory <garydgreg...@gmail.com>
AuthorDate: Fri Jul 5 08:43:41 2024 -0400

    Pull up `closed` instance variable
---
 .../compress/archivers/ArchiveOutputStream.java    | 33 ++++++++++++++++++++++
 .../archivers/ar/ArArchiveOutputStream.java        |  2 +-
 .../archivers/cpio/CpioArchiveOutputStream.java    | 18 ++----------
 .../archivers/tar/TarArchiveOutputStream.java      |  8 ++----
 4 files changed, 38 insertions(+), 23 deletions(-)

diff --git 
a/src/main/java/org/apache/commons/compress/archivers/ArchiveOutputStream.java 
b/src/main/java/org/apache/commons/compress/archivers/ArchiveOutputStream.java
index c7adb6c15..8d10fb6bb 100644
--- 
a/src/main/java/org/apache/commons/compress/archivers/ArchiveOutputStream.java
+++ 
b/src/main/java/org/apache/commons/compress/archivers/ArchiveOutputStream.java
@@ -59,6 +59,11 @@ public abstract class ArchiveOutputStream<E extends 
ArchiveEntry> extends Filter
     /** Holds the number of bytes written to this stream. */
     private long bytesWritten;
 
+    /**
+     * Whether this instance was successfully closed.
+     */
+    private boolean closed;
+
     /**
      * Whether this instance was successfully finished.
      */
@@ -112,6 +117,12 @@ public abstract class ArchiveOutputStream<E extends 
ArchiveEntry> extends Filter
         }
     }
 
+    @Override
+    public void close() throws IOException {
+        super.close();
+        closed = true;
+    }
+
     /**
      * Closes the archive entry, writing any trailer information that may be 
required.
      *
@@ -206,6 +217,16 @@ public abstract class ArchiveOutputStream<E extends 
ArchiveEntry> extends Filter
         return (int) bytesWritten;
     }
 
+    /**
+     * Tests whether this instance was successfully closed.
+     *
+     * @return whether this instance was successfully closed.
+     * @since 1.27.0
+     */
+    protected boolean isClosed() {
+        return closed;
+    }
+
     /**
      * Tests whether this instance was successfully finished.
      *
@@ -242,4 +263,16 @@ public abstract class ArchiveOutputStream<E extends 
ArchiveEntry> extends Filter
         oneByte[0] = (byte) (b & BYTE_MASK);
         write(oneByte, 0, 1);
     }
+
+    /**
+     * Check to make sure that this stream has not been closed
+     *
+     * @throws IOException if the stream is already closed
+     * @since 1.27.0
+     */
+    protected void checkOpen() throws IOException {
+        if (isClosed()) {
+            throw new IOException("Stream closed");
+        }
+    }
 }
diff --git 
a/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveOutputStream.java
 
b/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveOutputStream.java
index 30b813d63..02486adba 100644
--- 
a/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveOutputStream.java
+++ 
b/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveOutputStream.java
@@ -74,7 +74,7 @@ public class ArArchiveOutputStream extends 
ArchiveOutputStream<ArArchiveEntry> {
             }
         } finally {
             prevEntry = null;
-            out.close();
+            super.close();
         }
     }
 
diff --git 
a/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java
 
b/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java
index df4e773de..b554cbcf7 100644
--- 
a/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java
+++ 
b/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java
@@ -71,8 +71,6 @@ public class CpioArchiveOutputStream extends 
ArchiveOutputStream<CpioArchiveEntr
 
     private CpioArchiveEntry entry;
 
-    private boolean closed;
-
     /**
      * See {@link CpioArchiveEntry#CpioArchiveEntry(short)} for possible 
values.
      */
@@ -168,17 +166,6 @@ public class CpioArchiveOutputStream extends 
ArchiveOutputStream<CpioArchiveEntr
         this(out, FORMAT_NEW, BLOCK_SIZE, encoding);
     }
 
-    /**
-     * Check to make sure that this stream has not been closed
-     *
-     * @throws IOException if the stream is already closed
-     */
-    private void checkOpen() throws IOException {
-        if (this.closed) {
-            throw new IOException("Stream closed");
-        }
-    }
-
     /**
      * Closes the CPIO output stream as well as the stream being filtered.
      *
@@ -191,9 +178,8 @@ public class CpioArchiveOutputStream extends 
ArchiveOutputStream<CpioArchiveEntr
                 finish();
             }
         } finally {
-            if (!this.closed) {
-                out.close();
-                closed = true;
+            if (!isClosed()) {
+                super.close();
             }
         }
     }
diff --git 
a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStream.java
 
b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStream.java
index 7692e7578..4f3588d9b 100644
--- 
a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStream.java
+++ 
b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStream.java
@@ -109,12 +109,9 @@ public class TarArchiveOutputStream extends 
ArchiveOutputStream<TarArchiveEntry>
 
     private final int recordsPerBlock;
 
-    private boolean closed;
-
     /**
      * Indicates if putArchiveEntry has been called without closeArchiveEntry
      */
-
     private boolean haveUnclosedEntry;
 
     private final CountingOutputStream countingOut;
@@ -298,9 +295,8 @@ public class TarArchiveOutputStream extends 
ArchiveOutputStream<TarArchiveEntry>
                 finish();
             }
         } finally {
-            if (!closed) {
-                out.close();
-                closed = true;
+            if (!isClosed()) {
+                super.close();
             }
         }
     }

Reply via email to