[Title] Make copyRecursively which can filter files in FileUtil
authorho.namkoong <ho.namkoong@samsung.com>
Tue, 30 Oct 2012 11:58:41 +0000 (20:58 +0900)
committerho.namkoong <ho.namkoong@samsung.com>
Thu, 1 Nov 2012 02:48:46 +0000 (11:48 +0900)
[Type]
[Module]
[Priority]
[Jira#]
[Redmine#]
[Problem]
[Cause]
[Solution]
[TestCase]

Change-Id: I53053e93ef35573b6d34ea65d1b8111d5f997c2e

org.tizen.common/src/org/tizen/common/util/FileUtil.java

index af16211..08871bd 100755 (executable)
@@ -39,6 +39,7 @@ import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
+import java.util.List;
 import java.util.Stack;
 
 import org.tizen.common.util.log.Logger;
@@ -384,13 +385,27 @@ public class FileUtil {
     /**
      * Copy all the files in the source directory to the destination directory.
      * If destination directory does not exist, it is created.
-     * If destination file already exists , it determines overwrite it or not by overwrite parameter.
+     * If destination file already exists , it determines overwriting by overwrite parameter.
      * @param from source directory path
      * @param to destination directory path
      * @param overwrite if true, it overwrites. Else, skip copying.
      * @throws IOException
      */
     public static void copyRecursively(String from, String to, boolean overwrite) throws IOException {
+        copyRecursively(from, to, overwrite, null);
+    }
+    
+    /**
+     * Copy all the files in the source directory to the destination directory except files in filters parameter.
+     * If destination directory does not exist, it is created.
+     * If destination file already exists , it determines overwriting by overwrite parameter.
+     * @param from
+     * @param to
+     * @param overwrite
+     * @param filter
+     * @throws IOException
+     */
+    public static void copyRecursively(String from, String to, boolean overwrite, File... filters) throws IOException {
         File fromDir = new File(from);
         File toDir = new File(to);
         if (fromDir.exists() == false) {
@@ -398,7 +413,7 @@ public class FileUtil {
             return;
         }
         checkDirectory(fromDir, toDir);
-        copyRecursivelyWithoutDirChecking(fromDir, toDir, overwrite);
+        copyRecursivelyWithoutDirChecking(fromDir, toDir, overwrite, filters);
     }
     
     private static void checkDirectory(File fromDir, File toDir) throws IOException {
@@ -417,7 +432,7 @@ public class FileUtil {
         }
     }
 
-    private static void copyRecursivelyWithoutDirChecking(File fromDir, File toDir, boolean overwrite) throws IOException {
+    private static void copyRecursivelyWithoutDirChecking(File fromDir, File toDir, boolean overwrite, File... filters) throws IOException {
         Stack<File> fromFileStack = new Stack<File>();
         Stack<File> toFileStack = new Stack<File>();
         
@@ -439,7 +454,21 @@ public class FileUtil {
             }
             else if(fromFile.isFile()) {
                 if(!toFile.exists() || !toFile.isFile() || overwrite) {
-                    copyTo(fromFile, toFile, false);
+                    if(filters != null) {
+                        boolean found = false;
+                        for(File filter: filters) {
+                            if(filter.getCanonicalPath().equals(fromFile.getCanonicalPath())) {
+                                found = true;
+                                break;
+                            }
+                        }
+                        if(!found) {
+                            copyTo(fromFile, toFile, false);
+                        }
+                    }
+                    else {
+                        copyTo(fromFile, toFile, false);
+                    }
                 }
             }
         }