commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r920800 - in /commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip: ZipArchiveInputStream.java ZipArchiveOutputStream.java ZipFile.java ZipUtil.java
Date Tue, 09 Mar 2010 11:15:10 GMT
Author: bodewig
Date: Tue Mar  9 11:15:10 2010
New Revision: 920800

URL: http://svn.apache.org/viewvc?rev=920800&view=rev
Log:
extract common code

Modified:
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipUtil.java

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java?rev=920800&r1=920799&r2=920800&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
Tue Mar  9 11:15:10 2010
@@ -198,10 +198,7 @@ public class ZipArchiveInputStream exten
      */
     public boolean canReadEntryData(ArchiveEntry ae) {
         if (ae instanceof ZipArchiveEntry) {
-            ZipArchiveEntry ze = (ZipArchiveEntry) ae;
-            return !ze.isEncrypted() &&
-                (ze.getMethod() == ZipArchiveEntry.STORED
-                 || ze.getMethod() == ZipArchiveEntry.DEFLATED);
+            return ZipUtil.canHandleEntryData((ZipArchiveEntry) ae);
         }
         return false;
     }
@@ -217,16 +214,7 @@ public class ZipArchiveInputStream exten
         // avoid int overflow, check null buffer
         if (start <= buffer.length && length >= 0 && start >= 0
             && buffer.length - start >= length) {
-            if (current.isEncrypted()) {
-                throw new IOException("Encryption is not supported, used in "
-                                      + "entry " + current.getName());
-            }
-            if (current.getMethod() != ZipArchiveEntry.STORED
-                && current.getMethod() != ZipArchiveEntry.DEFLATED) {
-                throw new IOException(
-                        "Unsupported compression method " + current.getMethod()
-                        + " in ZIP archive entry " + current.getName());
-            }
+            ZipUtil.checkRequestedFeatures(current);
 
             if (current.getMethod() == ZipArchiveOutputStream.STORED) {
                 int csize = (int) current.getSize();

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=920800&r1=920799&r2=920800&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
Tue Mar  9 11:15:10 2010
@@ -513,9 +513,7 @@ public class ZipArchiveOutputStream exte
      */
     public boolean canWriteEntryData(ArchiveEntry ae) {
         if (ae instanceof ZipArchiveEntry) {
-            ZipArchiveEntry ze = (ZipArchiveEntry) ae;
-            return !ze.isEncrypted() &&
-                (ze.getMethod() == STORED || ze.getMethod() == DEFLATED);
+            return ZipUtil.canHandleEntryData((ZipArchiveEntry) ae);
         }
         return false;
     }
@@ -528,15 +526,7 @@ public class ZipArchiveOutputStream exte
      * @throws IOException on error
      */
     public void write(byte[] b, int offset, int length) throws IOException {
-        if (entry.isEncrypted()) {
-            throw new IOException("Encryption is not supported, used in entry "
-                                  + entry.getName());
-        }
-        if (entry.getMethod() != STORED && entry.getMethod() != DEFLATED) {
-            throw new IOException(
-                    "Unsupported compression method " + entry.getMethod()
-                    + " in ZIP archive entry " + entry.getName());
-        }
+        ZipUtil.checkRequestedFeatures(entry);
         if (entry.getMethod() == DEFLATED) {
             if (length > 0) {
                 if (!def.finished()) {

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=920800&r1=920799&r2=920800&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
Tue Mar  9 11:15:10 2010
@@ -274,18 +274,16 @@ public class ZipFile {
      * @since Apache Commons Compress 1.1
      */
     public boolean canReadEntryData(ZipArchiveEntry ze) {
-        return !ze.isEncrypted() &&
-            (ze.getMethod() == ZipArchiveEntry.STORED
-             || ze.getMethod() == ZipArchiveEntry.DEFLATED);
+        return ZipUtil.canHandleEntryData(ze);
     }
 
     /**
      * Returns an InputStream for reading the contents of the given entry.
+     *
      * @param ze the entry to get the stream for.
      * @return a stream to read the entry from.
      * @throws IOException if unable to create an input stream from the zipenty
-     * @throws ZipException if the zipentry has an unsupported
-     * compression method
+     * @throws ZipException if the zipentry uses an unsupported feature
      */
     public InputStream getInputStream(ZipArchiveEntry ze)
         throws IOException, ZipException {
@@ -293,16 +291,7 @@ public class ZipFile {
         if (offsetEntry == null) {
             return null;
         }
-        if (ze.isEncrypted()) {
-            throw new IOException("Encryption is not supported, used in "
-                                  + "entry " + ze.getName());
-        }
-        if (ze.getMethod() != ZipArchiveEntry.STORED
-            && ze.getMethod() != ZipArchiveEntry.DEFLATED) {
-            throw new IOException("Unsupported compression method "
-                                  + ze.getMethod() + " in ZIP archive entry "
-                                  + ze.getName());
-        }
+        ZipUtil.checkRequestedFeatures(ze);
         long start = offsetEntry.dataOffset;
         BoundedInputStream bis =
             new BoundedInputStream(start, ze.getCompressedSize());

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipUtil.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipUtil.java?rev=920800&r1=920799&r2=920800&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipUtil.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipUtil.java
Tue Mar  9 11:15:10 2010
@@ -180,4 +180,47 @@ public abstract class ZipUtil {
         return null;
     }
 
+    /**
+     * Whether this library is able to read or write the given entry.
+     */
+    static boolean canHandleEntryData(ZipArchiveEntry entry) {
+        return supportsEncryptionOf(entry) && supportsMethodOf(entry);
+    }
+
+    /**
+     * Whether this library supports the encryption used by the given
+     * entry.
+     *
+     * @return true if the entry isn't encrypted at all
+     */
+    private static boolean supportsEncryptionOf(ZipArchiveEntry entry) {
+        return !entry.isEncrypted();
+    }
+
+    /**
+     * Whether this library supports the compression method used by
+     * the given entry.
+     *
+     * @return true if the compression method is STORED or DEFLATED
+     */
+    private static boolean supportsMethodOf(ZipArchiveEntry entry) {
+        return entry.getMethod() == ZipArchiveEntry.STORED
+            || entry.getMethod() == ZipArchiveEntry.DEFLATED;
+    }
+
+    /**
+     * Checks whether the entry requires features not (yet) supported
+     * by the library and throws an exception if it does.
+     */
+    static void checkRequestedFeatures(ZipArchiveEntry ze) throws IOException {
+        if (!supportsEncryptionOf(ze)) {
+            throw new IOException("Encryption is not supported, used in "
+                                  + "entry " + ze.getName());
+        }
+        if (!supportsMethodOf(ze)) {
+            throw new IOException("Unsupported compression method "
+                                  + ze.getMethod() + " in ZIP archive entry "
+                                  + ze.getName());
+        }
+    }
 }
\ No newline at end of file



Mime
View raw message