package com.otter.otterlibrary;

import android.content.Context;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * OFile is a toolkit of file operation.
 */
public class OFile {

    private static void copy(InputStream in, OutputStream out) {
        try {
            byte[] buffer = new byte[1024];
            int byteCount;

            while ((byteCount = in.read(buffer)) != -1) {
                out.write(buffer, 0, byteCount);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /** Copy the file. It will override current file. */
    public static boolean copyFile(File from, File to) {
        if (from == null || to == null) {
            return false;
        }
        if (!from.exists() || !from.isFile() || !from.canRead()) {
            return false;
        }

        try {
            FileInputStream in = new FileInputStream(from);
            FileOutputStream out = new FileOutputStream(to);
            copy(in, out);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        long fromLength = from.length();
        long toLength = to.length();
        return fromLength == toLength;
    }

    /**
     * Copy the directory recursively.
     * If the destination directory did exist, it will be deleted first.
     */
    public static boolean copyDir(File from, File to) {
        if (from == null || to == null) {
            return false;
        }
        if (!from.exists() || !from.canRead()) {
            return false;
        }

        if (from.isDirectory()) {
            if (to.exists()) {
                // Destination directory did exist, delete it.
                if (!deleteDir(to)) {
                    return false;
                }
            }
            if (!to.mkdirs()) {
                return false;
            }

            String[] children = from.list();
            for (String child : children) {
                if (!copyDir(new File(from, child), new File(to, child))) {
                    return false;
                }
            }
        } else {
            return copyFile(from, to);
        }

        // The directory copy is completely.
        return true;
    }

    /** Delete the folder recursively. */
    public static boolean deleteDir(File dir) {
        if (dir == null) {
            return false;
        }

        if (dir.isDirectory()) {
            File[] children = dir.listFiles();
            for (File child : children) {
                if (!deleteDir(child)) {
                    return false;
                }
            }
        }

        // The directory is now empty so delete it
        return dir.delete();
    }

    /** Copy the file from assets. It will override current file. */
    public static boolean copyFileFromAssets(Context ctx, String fileName, File to) {
        if (fileName == null || to == null) {
            return false;
        }
        try {
            InputStream in = ctx.getAssets().open(fileName);
            FileOutputStream out = new FileOutputStream(to);
            copy(in, out);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return to.exists();
    }
}

@@ -340,16 +340,143 @@
 File %7B%0A%0A
+    private static boolean isAvailableFile(File file) %7B%0A        return file != null && file.exists() && file.canRead();%0A    %7D%0A%0A
     priv
@@ -1373,99 +1373,29 @@
 if (
-from == null %7C%7C to == null) %7B%0A            return false;%0A        %7D%0A        if (!from.exists(
+!isAvailableFile(from
 ) %7C%7C
@@ -1409,39 +1409,34 @@
 isFile() %7C%7C 
-!from.canRead()
+to == null
 ) %7B%0A        
@@ -2038,119 +2038,44 @@
 if (
-from == null %7C%7C to == null) %7B%0A            return false;%0A        %7D%0A        if (!from.exists() %7C%7C !from.canRead()
+!isAvailableFile(from) %7C%7C to == null
 ) %7B%0A
@@ -2274,38 +2274,16 @@
 Dir(to))
- %7B%0A                   
  return 
@@ -2301,35 +2301,18 @@
         
-    %7D%0A            
 %7D
+%0A
 %0A       
@@ -2338,53 +2338,21 @@
 ()) 
-%7B%0A                return false;%0A            %7D
+return false;
 %0A%0A  
@@ -2826,34 +2826,30 @@
 if (
-dir == null) %7B%0A           
+!isAvailableFile(dir))
  ret
@@ -2854,34 +2854,24 @@
 eturn false;
-%0A        %7D
 %0A%0A        if
@@ -3416,32 +3416,33 @@
 alse;%0A        %7D%0A
+%0A
         try %7B%0A  

Add isAvailableFile(File) method to code refactor