You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by bo...@apache.org on 2011/04/18 06:28:14 UTC

svn commit: r1094224 - in /commons/proper/compress/trunk/src: changes/changes.xml main/java/org/apache/commons/compress/archivers/zip/ZipFile.java

Author: bodewig
Date: Mon Apr 18 04:28:14 2011
New Revision: 1094224

URL: http://svn.apache.org/viewvc?rev=1094224&view=rev
Log:
add a finalize method to ZipFile as suggested on the user list

Modified:
    commons/proper/compress/trunk/src/changes/changes.xml
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java

Modified: commons/proper/compress/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/changes/changes.xml?rev=1094224&r1=1094223&r2=1094224&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/changes/changes.xml (original)
+++ commons/proper/compress/trunk/src/changes/changes.xml Mon Apr 18 04:28:14 2011
@@ -45,6 +45,10 @@ The <action> type attribute can be add,u
   </properties>
   <body>
     <release version="1.2" date="as in SVN" description="Release 1.2">
+      <action type="update" date="2011-04-18">
+        ZipFile now implements finalize which closes the underlying
+        file.
+      </action>
       <action issue="COMPRESS-117" type="update" date="2011-03-23">
         Certain tar files not recognised by ArchiveStreamFactory.
       </action> 

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java?rev=1094224&r1=1094223&r2=1094224&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java (original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java Mon Apr 18 04:28:14 2011
@@ -112,6 +112,11 @@ public class ZipFile {
     private final boolean useUnicodeExtraFields;
 
     /**
+     * Whether the file is closed.
+     */
+    private boolean closed;
+
+    /**
      * Opens the given file for reading, assuming "UTF8" for file names.
      *
      * @param f the archive.
@@ -208,8 +213,11 @@ public class ZipFile {
      * Closes the archive.
      * @throws IOException if an error occurs closing the archive.
      */
-    public void close() throws IOException {
-        archive.close();
+    public synchronized void close() throws IOException {
+        if (!closed) {
+            closed = true;
+            archive.close();
+        }
     }
 
     /**
@@ -307,6 +315,19 @@ public class ZipFile {
         }
     }
 
+    /**
+     * Ensures that the close method of this zipfile is called when
+     * there are no more references to it.
+     * @see close()
+     */
+    protected void finalize() throws Throwable {
+        try {
+            close();
+        } finally {
+            super.finalize();
+        }
+    }
+
     private static final int CFH_LEN =
         /* version made by                 */ SHORT
         /* version needed to extract       */ + SHORT



Re: svn commit: r1094224 - in /commons/proper/compress/trunk/src: changes/changes.xml main/java/org/apache/commons/compress/archivers/zip/ZipFile.java

Posted by Stefan Bodewig <bo...@apache.org>.
On 2011-04-18, sebb wrote:

> On 18 April 2011 06:22, Stefan Bodewig <bo...@apache.org> wrote:
>> On 2011-04-18, Adrian Crum wrote:

>>> A suggestion: if the library has logging capability, then log a
>>> warning saying that the archive was closed in the finalize
>>> method. That will serve as a clue to the library user that they forgot
>>> to close the archive.

>> You are right, but commons-compress doesn't have any dependency on any
>> logging framework right now and I'd hesitate to do that just for this
>> case.

> Could perhaps just use System.err ?

> It's a bug in the user code, so they cannot really complain if there
> is additional output.

OK.

Done in trunk.

Stefan

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: svn commit: r1094224 - in /commons/proper/compress/trunk/src: changes/changes.xml main/java/org/apache/commons/compress/archivers/zip/ZipFile.java

Posted by sebb <se...@gmail.com>.
On 18 April 2011 06:22, Stefan Bodewig <bo...@apache.org> wrote:
> On 2011-04-18, Adrian Crum wrote:
>
>> A suggestion: if the library has logging capability, then log a
>> warning saying that the archive was closed in the finalize
>> method. That will serve as a clue to the library user that they forgot
>> to close the archive.
>
> You are right, but commons-compress doesn't have any dependency on any
> logging framework right now and I'd hesitate to do that just for this
> case.

Could perhaps just use System.err ?

It's a bug in the user code, so they cannot really complain if there
is additional output.

> Stefan
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: svn commit: r1094224 - in /commons/proper/compress/trunk/src: changes/changes.xml main/java/org/apache/commons/compress/archivers/zip/ZipFile.java

Posted by Stefan Bodewig <bo...@apache.org>.
On 2011-04-18, Adrian Crum wrote:

> A suggestion: if the library has logging capability, then log a
> warning saying that the archive was closed in the finalize
> method. That will serve as a clue to the library user that they forgot
> to close the archive.

You are right, but commons-compress doesn't have any dependency on any
logging framework right now and I'd hesitate to do that just for this
case.

Stefan

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: svn commit: r1094224 - in /commons/proper/compress/trunk/src: changes/changes.xml main/java/org/apache/commons/compress/archivers/zip/ZipFile.java

Posted by Adrian Crum <ad...@sandglass-software.com>.
A suggestion: if the library has logging capability, then log a warning 
saying that the archive was closed in the finalize method. That will 
serve as a clue to the library user that they forgot to close the archive.

-Adrian

On 4/17/2011 9:28 PM, bodewig@apache.org wrote:
> Author: bodewig
> Date: Mon Apr 18 04:28:14 2011
> New Revision: 1094224
>
> URL: http://svn.apache.org/viewvc?rev=1094224&view=rev
> Log:
> add a finalize method to ZipFile as suggested on the user list
>
> Modified:
>      commons/proper/compress/trunk/src/changes/changes.xml
>      commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
>
> Modified: commons/proper/compress/trunk/src/changes/changes.xml
> URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/changes/changes.xml?rev=1094224&r1=1094223&r2=1094224&view=diff
> ==============================================================================
> --- commons/proper/compress/trunk/src/changes/changes.xml (original)
> +++ commons/proper/compress/trunk/src/changes/changes.xml Mon Apr 18 04:28:14 2011
> @@ -45,6 +45,10 @@ The<action>  type attribute can be add,u
>     </properties>
>     <body>
>       <release version="1.2" date="as in SVN" description="Release 1.2">
> +<action type="update" date="2011-04-18">
> +        ZipFile now implements finalize which closes the underlying
> +        file.
> +</action>
>         <action issue="COMPRESS-117" type="update" date="2011-03-23">
>           Certain tar files not recognised by ArchiveStreamFactory.
>         </action>
>
> Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
> URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java?rev=1094224&r1=1094223&r2=1094224&view=diff
> ==============================================================================
> --- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java (original)
> +++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java Mon Apr 18 04:28:14 2011
> @@ -112,6 +112,11 @@ public class ZipFile {
>       private final boolean useUnicodeExtraFields;
>
>       /**
> +     * Whether the file is closed.
> +     */
> +    private boolean closed;
> +
> +    /**
>        * Opens the given file for reading, assuming "UTF8" for file names.
>        *
>        * @param f the archive.
> @@ -208,8 +213,11 @@ public class ZipFile {
>        * Closes the archive.
>        * @throws IOException if an error occurs closing the archive.
>        */
> -    public void close() throws IOException {
> -        archive.close();
> +    public synchronized void close() throws IOException {
> +        if (!closed) {
> +            closed = true;
> +            archive.close();
> +        }
>       }
>
>       /**
> @@ -307,6 +315,19 @@ public class ZipFile {
>           }
>       }
>
> +    /**
> +     * Ensures that the close method of this zipfile is called when
> +     * there are no more references to it.
> +     * @see close()
> +     */
> +    protected void finalize() throws Throwable {
> +        try {
> +            close();
> +        } finally {
> +            super.finalize();
> +        }
> +    }
> +
>       private static final int CFH_LEN =
>           /* version made by                 */ SHORT
>           /* version needed to extract       */ + SHORT
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org