make code a tiny bit less redundant

git-svn-id: https://svn.apache.org/repos/asf/ant/antlibs/compress/trunk@1154230 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/org/apache/ant/compress/resources/CommonsCompressArchiveResource.java b/src/main/org/apache/ant/compress/resources/CommonsCompressArchiveResource.java
index 81556ff..1fde739 100644
--- a/src/main/org/apache/ant/compress/resources/CommonsCompressArchiveResource.java
+++ b/src/main/org/apache/ant/compress/resources/CommonsCompressArchiveResource.java
@@ -28,11 +28,10 @@
 import org.apache.tools.ant.types.Reference;
 import org.apache.tools.ant.types.Resource;
 import org.apache.tools.ant.types.resources.ArchiveResource;
-import org.apache.tools.ant.types.resources.FileProvider;
 import org.apache.tools.ant.util.FileUtils;
-import org.apache.ant.compress.util.FileAwareArchiveStreamFactory;
 import org.apache.ant.compress.util.ArchiveStreamFactory;
 import org.apache.ant.compress.util.EntryHelper;
+import org.apache.ant.compress.util.StreamHelper;
 import org.apache.commons.compress.archivers.ArchiveEntry;
 import org.apache.commons.compress.archivers.ArchiveInputStream;
 
@@ -220,14 +219,9 @@
 
     private ArchiveInputStream getStream() throws IOException {
         Resource archive = getArchive();
-        if (factory instanceof FileAwareArchiveStreamFactory
-            && archive.as(FileProvider.class) != null) {
-            FileProvider p = (FileProvider) archive.as(FileProvider.class);
-            FileAwareArchiveStreamFactory f =
-                (FileAwareArchiveStreamFactory) factory;
-            return f.getArchiveInputStream(p.getFile(), getEncoding());
-        }
-        return 
+        ArchiveInputStream s = StreamHelper.getInputStream(factory, archive,
+                                                           getEncoding());
+        return s != null ? s :
             factory.getArchiveStream(new BufferedInputStream(archive
                                                              .getInputStream()),
                                      getEncoding());
diff --git a/src/main/org/apache/ant/compress/resources/CommonsCompressArchiveScanner.java b/src/main/org/apache/ant/compress/resources/CommonsCompressArchiveScanner.java
index ef2b698..8595798 100644
--- a/src/main/org/apache/ant/compress/resources/CommonsCompressArchiveScanner.java
+++ b/src/main/org/apache/ant/compress/resources/CommonsCompressArchiveScanner.java
@@ -22,14 +22,13 @@
 import java.io.IOException;
 import java.util.Map;
 
+import org.apache.ant.compress.util.StreamHelper;
 import org.apache.ant.compress.util.ArchiveStreamFactory;
-import org.apache.ant.compress.util.FileAwareArchiveStreamFactory;
 import org.apache.ant.compress.util.Messages;
 import org.apache.tools.ant.BuildException;
 import org.apache.tools.ant.Project;
 import org.apache.tools.ant.types.ArchiveScanner;
 import org.apache.tools.ant.types.Resource;
-import org.apache.tools.ant.types.resources.FileProvider;
 import org.apache.tools.ant.util.FileUtils;
 import org.apache.commons.compress.archivers.ArchiveEntry;
 import org.apache.commons.compress.archivers.ArchiveInputStream;
@@ -93,14 +92,8 @@
 
         try {
             try {
-                if (factory instanceof FileAwareArchiveStreamFactory
-                    && src.as(FileProvider.class) != null) {
-                    FileProvider p =
-                        (FileProvider) src.as(FileProvider.class);
-                    FileAwareArchiveStreamFactory f =
-                        (FileAwareArchiveStreamFactory) factory;
-                    ai = f.getArchiveInputStream(p.getFile(), encoding);
-                } else {
+                ai = StreamHelper.getInputStream(factory, src, encoding);
+                if (ai == null) {
                     ai =
                         factory.getArchiveStream(new BufferedInputStream(src
                                                                          .getInputStream()),
diff --git a/src/main/org/apache/ant/compress/taskdefs/ArchiveBase.java b/src/main/org/apache/ant/compress/taskdefs/ArchiveBase.java
index b9c4594..ac821ab 100644
--- a/src/main/org/apache/ant/compress/taskdefs/ArchiveBase.java
+++ b/src/main/org/apache/ant/compress/taskdefs/ArchiveBase.java
@@ -42,7 +42,7 @@
 import org.apache.ant.compress.resources.ZipResource;
 import org.apache.ant.compress.util.ArchiveStreamFactory;
 import org.apache.ant.compress.util.EntryHelper;
-import org.apache.ant.compress.util.FileAwareArchiveStreamFactory;
+import org.apache.ant.compress.util.StreamHelper;
 
 import org.apache.commons.compress.archivers.ArchiveEntry;
 import org.apache.commons.compress.archivers.ArchiveOutputStream;
@@ -60,7 +60,6 @@
 import org.apache.tools.ant.types.Resource;
 import org.apache.tools.ant.types.ResourceCollection;
 import org.apache.tools.ant.types.resources.ArchiveResource;
-import org.apache.tools.ant.types.resources.FileProvider;
 import org.apache.tools.ant.types.resources.FileResource;
 import org.apache.tools.ant.types.resources.MappedResource;
 import org.apache.tools.ant.types.resources.Resources;
@@ -517,14 +516,8 @@
         try {
             String enc = Expand.NATIVE_ENCODING.equals(getEncoding())
                 ? null : getEncoding();
-            if (factory instanceof FileAwareArchiveStreamFactory
-                && getDest().as(FileProvider.class) != null) {
-                FileProvider p =
-                    (FileProvider) getDest().as(FileProvider.class);
-                FileAwareArchiveStreamFactory f =
-                    (FileAwareArchiveStreamFactory) factory;
-                out = f.getArchiveOutputStream(p.getFile(), enc);
-            } else {
+            out = StreamHelper.getOutputStream(factory, getDest(), enc);
+            if (out == null) {
                 out =
                     factory.getArchiveStream(new BufferedOutputStream(getDest()
                                                                       .getOutputStream()),
diff --git a/src/main/org/apache/ant/compress/taskdefs/ExpandBase.java b/src/main/org/apache/ant/compress/taskdefs/ExpandBase.java
index d154315..4de2b11 100644
--- a/src/main/org/apache/ant/compress/taskdefs/ExpandBase.java
+++ b/src/main/org/apache/ant/compress/taskdefs/ExpandBase.java
@@ -27,13 +27,14 @@
 
 import org.apache.ant.compress.util.ArchiveStreamFactory;
 import org.apache.ant.compress.util.EntryHelper;
-import org.apache.ant.compress.util.FileAwareArchiveStreamFactory;
 import org.apache.ant.compress.util.Messages;
+import org.apache.ant.compress.util.StreamHelper;
 
 import org.apache.tools.ant.BuildException;
 import org.apache.tools.ant.Project;
 import org.apache.tools.ant.taskdefs.Expand;
 import org.apache.tools.ant.types.Resource;
+import org.apache.tools.ant.types.resources.FileResource;
 import org.apache.tools.ant.util.FileNameMapper;
 import org.apache.tools.ant.util.FileUtils;
 
@@ -105,10 +106,9 @@
         }
         InputStream is = null;
         try {
-            if (factory instanceof FileAwareArchiveStreamFactory) {
-                FileAwareArchiveStreamFactory f =
-                    (FileAwareArchiveStreamFactory) factory;
-                is =  f.getArchiveInputStream(srcF, getEncoding());
+            is = StreamHelper.getInputStream(factory, new FileResource(srcF),
+                                             getEncoding());
+            if (is != null) {
                 expandArchiveStream(srcF.getPath(), (ArchiveInputStream) is,
                                     dir);
             } else {
diff --git a/src/main/org/apache/ant/compress/taskdefs/PackBase.java b/src/main/org/apache/ant/compress/taskdefs/PackBase.java
index 805b149..d290bf1 100644
--- a/src/main/org/apache/ant/compress/taskdefs/PackBase.java
+++ b/src/main/org/apache/ant/compress/taskdefs/PackBase.java
@@ -27,7 +27,7 @@
 
 import org.apache.ant.compress.resources.CommonsCompressCompressorResource;
 import org.apache.ant.compress.util.CompressorStreamFactory;
-import org.apache.ant.compress.util.FileAwareCompressorStreamFactory;
+import org.apache.ant.compress.util.StreamHelper;
 import org.apache.commons.compress.utils.IOUtils;
 import org.apache.tools.ant.BuildException;
 import org.apache.tools.ant.Task;
@@ -187,13 +187,8 @@
         OutputStream out = null;
         try {
             in = src.getInputStream();
-            if (factory instanceof FileAwareCompressorStreamFactory
-                && dest.as(FileProvider.class) != null) {
-                FileProvider p = (FileProvider) dest.as(FileProvider.class);
-                FileAwareCompressorStreamFactory f =
-                    (FileAwareCompressorStreamFactory) factory;
-                out =  f.getCompressorOutputStream(p.getFile());
-            } else {
+            out = StreamHelper.getOutputStream(factory, dest);
+            if (out == null) {
                 out =
                     factory.getCompressorStream(new BufferedOutputStream(dest.getOutputStream()));
             }
diff --git a/src/main/org/apache/ant/compress/taskdefs/UnpackBase.java b/src/main/org/apache/ant/compress/taskdefs/UnpackBase.java
index 7b65ee5..93941f7 100644
--- a/src/main/org/apache/ant/compress/taskdefs/UnpackBase.java
+++ b/src/main/org/apache/ant/compress/taskdefs/UnpackBase.java
@@ -24,10 +24,9 @@
 import java.io.InputStream;
 
 import org.apache.ant.compress.util.CompressorStreamFactory;
-import org.apache.ant.compress.util.FileAwareCompressorStreamFactory;
+import org.apache.ant.compress.util.StreamHelper;
 import org.apache.commons.compress.compressors.CompressorInputStream;
 import org.apache.tools.ant.BuildException;
-import org.apache.tools.ant.types.resources.FileProvider;
 import org.apache.tools.ant.util.FileUtils;
 import org.apache.tools.ant.taskdefs.Unpack;
 
@@ -67,14 +66,8 @@
             InputStream fis = null;
             try {
                 out = new FileOutputStream(dest);
-                if (factory instanceof FileAwareCompressorStreamFactory
-                    && srcResource.as(FileProvider.class) != null) {
-                    FileProvider p =
-                        (FileProvider) srcResource.as(FileProvider.class);
-                    FileAwareCompressorStreamFactory f =
-                        (FileAwareCompressorStreamFactory) factory;
-                    zIn =  f.getCompressorInputStream(p.getFile());
-                } else {
+                zIn = StreamHelper.getInputStream(factory, srcResource);
+                if (zIn == null) {
                     fis = srcResource.getInputStream();
                     zIn = factory.getCompressorStream(new BufferedInputStream(fis));
                 }
diff --git a/src/main/org/apache/ant/compress/util/StreamHelper.java b/src/main/org/apache/ant/compress/util/StreamHelper.java
new file mode 100644
index 0000000..4b207a0
--- /dev/null
+++ b/src/main/org/apache/ant/compress/util/StreamHelper.java
@@ -0,0 +1,127 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+
+package org.apache.ant.compress.util;
+
+import java.io.IOException;
+
+import org.apache.commons.compress.archivers.ArchiveInputStream;
+import org.apache.commons.compress.archivers.ArchiveOutputStream;
+import org.apache.commons.compress.compressors.CompressorInputStream;
+import org.apache.commons.compress.compressors.CompressorOutputStream;
+import org.apache.tools.ant.types.Resource;
+import org.apache.tools.ant.types.resources.FileProvider;
+
+/**
+ * Helper methods that deal with FileAware*Factory and Resources that
+ * may or may not provide files.
+ */
+public class StreamHelper {
+    private StreamHelper() { }
+
+    private static boolean isFileCombination(ArchiveStreamFactory factory,
+                                             Resource r) {
+        return factory instanceof FileAwareArchiveStreamFactory
+            && r.as(FileProvider.class) != null;
+    }
+
+    private static boolean isFileCombination(CompressorStreamFactory factory,
+                                             Resource r) {
+        return factory instanceof FileAwareCompressorStreamFactory
+            && r.as(FileProvider.class) != null;
+    }
+
+    /**
+     * If the factory knows about files and the resource can provide
+     * one, returns an ArchiveInputStream for it, otherwise returns
+     * null.
+     * @param r the resource to read from
+     * @param encoding the encoding of the entry names
+     */
+    public static
+        ArchiveInputStream getInputStream(ArchiveStreamFactory factory,
+                                          Resource r, String encoding)
+        throws IOException {
+        if (isFileCombination(factory, r)) {
+            FileProvider p = (FileProvider) r.as(FileProvider.class);
+            FileAwareArchiveStreamFactory f =
+                (FileAwareArchiveStreamFactory) factory;
+            return f.getArchiveInputStream(p.getFile(), encoding);
+        }
+        return null;
+    }
+
+    /**
+     * If the factory knows about files and the resource can provide
+     * one, returns an ArchiveOutputStream for it, otherwise returns
+     * null.
+     * @param r the resource to write to
+     * @param encoding the encoding of the entry names
+     */
+    public static
+        ArchiveOutputStream getOutputStream(ArchiveStreamFactory factory,
+                                            Resource r, String encoding)
+        throws IOException {
+        if (isFileCombination(factory, r)) {
+            FileProvider p = (FileProvider) r.as(FileProvider.class);
+            FileAwareArchiveStreamFactory f =
+                (FileAwareArchiveStreamFactory) factory;
+            return f.getArchiveOutputStream(p.getFile(), encoding);
+        }
+        return null;
+    }
+
+    /**
+     * If the factory knows about files and the resource can provide
+     * one, returns an CompressorInputStream for it, otherwise returns
+     * null.
+     * @param r the resource to read from
+     * @param encoding the encoding of the entry names
+     */
+    public static
+        CompressorInputStream getInputStream(CompressorStreamFactory factory,
+                                             Resource r)
+        throws IOException {
+        if (isFileCombination(factory, r)) {
+            FileProvider p = (FileProvider) r.as(FileProvider.class);
+            FileAwareCompressorStreamFactory f =
+                (FileAwareCompressorStreamFactory) factory;
+            return f.getCompressorInputStream(p.getFile());
+        }
+        return null;
+    }
+
+    /**
+     * If the factory knows about files and the resource can provide
+     * one, returns an CompressorOutputStream for it, otherwise returns
+     * null.
+     * @param r the resource to write to
+     */
+    public static
+        CompressorOutputStream getOutputStream(CompressorStreamFactory factory,
+                                               Resource r)
+        throws IOException {
+        if (isFileCombination(factory, r)) {
+            FileProvider p = (FileProvider) r.as(FileProvider.class);
+            FileAwareCompressorStreamFactory f =
+                (FileAwareCompressorStreamFactory) factory;
+            return f.getCompressorOutputStream(p.getFile());
+        }
+        return null;
+    }
+}