package com.colorful.keyboard.theme.myutil;

import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

public final class ConfigFileUtils {

    private ConfigFileUtils() {
        throw new UnsupportedOperationException("error");
    }

    public static boolean configCopy(String source, String target, boolean isFolder) {
        if (isFolder) {
            new File(target).mkdirs();
            File a = new File(source);
            String[] file = a.list();
            File temp = null;
            for (int i = 0; i < file.length; i++) {
                if (source.endsWith(File.separator)) {
                    temp = new File(source + file[i]);
                } else {
                    temp = new File(source + File.separator + file[i]);
                }
                if (temp.isFile()) {
                    try {
                        FileInputStream input = new FileInputStream(temp);
                        FileOutputStream output = new FileOutputStream(target + File.separator + temp.getName().toString());
                        byte[] b = new byte[1024];
                        int len;
                        while ((len = input.read(b)) != -1) {
                            output.write(b, 0, len);
                        }
                        output.flush();
                        output.close();
                        input.close();
                        return true;
                    }catch (Exception e){
                        Log.e("666","异常了");
                        return false;
                    }

                } if (temp.isDirectory()) {
                    configCopy(source + File.separator + file[i], target + File.separator + file[i], true);
                }
            }
        } else {
            int byteread = 0;
            File oldfile = new File(source);
            if (oldfile.exists()) {
                try {
                    InputStream inputStream = new FileInputStream(source);
                    File file = new File(target);
                    file.getParentFile().mkdirs();
                    file.createNewFile();
                    FileOutputStream outputStream = new FileOutputStream(file);
                    byte[] buffer = new byte[1024];
                    while ((byteread = inputStream.read(buffer)) != -1){
                        outputStream.write(buffer, 0, byteread);
                    }
                    inputStream.close();
                    outputStream.close();
                    return true;
                }catch (Exception e){
                    return false;

                }

            }
        }
        return false;
    }

}
