commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From grobme...@apache.org
Subject svn commit: r769066 - in /commons/proper/compress/trunk/src: main/java/org/apache/commons/compress/archivers/ar/ main/java/org/apache/commons/compress/archivers/cpio/ main/java/org/apache/commons/compress/archivers/tar/ main/java/org/apache/commons/com...
Date Mon, 27 Apr 2009 17:43:59 GMT
Author: grobmeier
Date: Mon Apr 27 17:43:58 2009
New Revision: 769066

URL: http://svn.apache.org/viewvc?rev=769066&view=rev
Log:
call of finish() is optional
calling finish() twice causes an exception

Modified:
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveOutputStream.java
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStream.java
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/ArchiveOutputStreamTest.java

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveOutputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveOutputStream.java?rev=769066&r1=769065&r2=769066&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveOutputStream.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveOutputStream.java
Mon Apr 27 17:43:58 2009
@@ -38,6 +38,7 @@
     private long entryOffset = 0;
     private ArArchiveEntry prevEntry;
     private boolean haveUnclosedEntry = true;
+    private boolean finished = false;
 
     public ArArchiveOutputStream( final OutputStream pOut ) {
         this.out = pOut;
@@ -154,6 +155,9 @@
     }
 
     public void close() throws IOException {
+        if(!finished) {
+            finish();
+        }
         out.close();
         prevEntry = null;
     }
@@ -169,6 +173,9 @@
     public void finish() throws IOException {
         if(haveUnclosedEntry) {
             throw new IOException("This archives contains unclosed entries.");
+        } else if(finished) {
+            throw new IOException("This archive has already been finished");
         }
+        finished = true;
     }
 }

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java?rev=769066&r1=769065&r2=769066&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java
Mon Apr 27 17:43:58 2009
@@ -311,10 +311,10 @@
      */
     public void finish() throws IOException {
         ensureOpen();
-
-        if (this.finished) {
-            return;
+        if (finished) {
+            throw new IOException("This archive has already been finished");
         }
+        
         if (this.entry != null) {
             throw new IOException("This archives contains unclosed entries.");
         }
@@ -323,6 +323,8 @@
         this.entry.setNumberOfLinks(1);
         writeHeader(this.entry);
         closeArchiveEntry();
+        
+        finished = true;
     }
 
     /**
@@ -333,6 +335,10 @@
      *             occurred
      */
     public void close() throws IOException {
+        if(!finished) {
+            finish();
+        }
+        
         if (!this.closed) {
             out.close();
             this.closed = true;

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStream.java?rev=769066&r1=769065&r2=769066&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStream.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStream.java
Mon Apr 27 17:43:58 2009
@@ -55,6 +55,8 @@
     /* Indicates if putArchiveEntry has been called without closeArchiveEntry */
     private boolean haveUnclosedEntry = false;
     
+    private boolean finished = false;
+    
     private final OutputStream out;
 
     /**
@@ -111,11 +113,16 @@
      * @throws IOException on error
      */
     public void finish() throws IOException {
+        if (finished) {
+            throw new IOException("This archive has already been finished");
+        }
+        
         if(haveUnclosedEntry) {
             throw new IOException("This archives contains unclosed entries.");
         }
         writeEOFRecord();
         writeEOFRecord();
+        finished = true;
     }
 
     /**
@@ -123,6 +130,10 @@
      * @throws IOException on error
      */
     public void close() throws IOException {
+        if(!finished) {
+            finish();
+        }
+        
         if (!closed) {
             buffer.close();
             out.close();

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java?rev=769066&r1=769065&r2=769066&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java
Mon Apr 27 17:43:58 2009
@@ -61,6 +61,9 @@
     private static final int SHORT = 2;
     private static final int WORD = 4;
     static final int BUFFER_SIZE = 512;
+    
+    private boolean finished = false;
+    
     /* 
      * Apparently Deflater.setInput gets slowed down a lot on Sun JVMs
      * when it gets handed a really big buffer.  See
@@ -332,6 +335,10 @@
      * @see org.apache.commons.compress.archivers.ArchiveOutputStream#finish()
      */
     public void finish() throws IOException {
+        if (finished) {
+            throw new IOException("This archive has already been finished");
+        }
+        
         if(entry != null) {
             throw new IOException("This archives contains unclosed entries.");
         }
@@ -344,6 +351,7 @@
         writeCentralDirectoryEnd();
         offsets.clear();
         entries.clear();
+        finished = true;
     }
 
     /**
@@ -528,6 +536,10 @@
      * @exception  IOException  if an I/O error occurs.
      */
     public void close() throws IOException {
+        if(!finished) {
+            finish();
+        }
+        
         if (raf != null) {
             raf.close();
         }

Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/ArchiveOutputStreamTest.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/ArchiveOutputStreamTest.java?rev=769066&r1=769065&r2=769066&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/ArchiveOutputStreamTest.java
(original)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/ArchiveOutputStreamTest.java
Mon Apr 27 17:43:58 2009
@@ -88,4 +88,24 @@
         }
     }
 
+    public void testOptionalFinish() throws Exception {
+        OutputStream out1 = new ByteArrayOutputStream();
+        
+        ArchiveOutputStream aos1 = factory.createArchiveOutputStream("zip", out1);
+        aos1.putArchiveEntry(new ZipArchiveEntry("dummy"));
+        aos1.closeArchiveEntry();
+        aos1.close();
+        
+        aos1 = factory.createArchiveOutputStream("jar", out1);
+        aos1.putArchiveEntry(new JarArchiveEntry("dummy"));
+        aos1.closeArchiveEntry();
+        aos1.close();
+        try {
+            aos1.finish();
+            aos1.finish();
+            fail("After putArchive should follow closeArchive");
+        } catch (IOException io) {
+            // Exception expected
+        }
+    }
 }



Mime
View raw message