package org.ta.pddserver.PDFTemplate;

import java.io.*;

/**
 * 文件操作代码
 *
 * @author cn.outofmemory
 * @date 2013-1-7
 */
public class FileUtils {
    /**
     * 将文本文件中的内容读入到buffer中
     * @param buffer buffer
     * @param filePath 文件路径
     * @throws IOException 异常
     * @author cn.outofmemory
     * @date 2013-1-7
     */
    public static void readToBuffer(StringBuffer buffer, String filePath) throws IOException {
        InputStream is = new FileInputStream(filePath);
        String line; // 用来保存每行读取的内容
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        line = reader.readLine(); // 读取第一行
        while (line != null) { // 如果 line 为空说明读完了
            buffer.append(line); // 将读到的内容添加到 buffer 中
            buffer.append("\n"); // 添加换行符
            line = reader.readLine(); // 读取下一行
        }
        reader.close();
        is.close();
    }

    /**
     * 读取文本文件内容
     * @param filePath 文件所在路径
     * @return 文本内容
     * @throws IOException 异常
     * @author cn.outofmemory
     * @date 2013-1-7
     */
    public static String readFile(String filePath){
        try {
            StringBuffer sb = new StringBuffer();
            FileUtils.readToBuffer(sb, filePath);
            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";

    }

    public static long getFileSize(String filename) {
        File file = new File(filename);
        if (!file.exists() || !file.isFile()) {
            System.out.println("文件不存在");
            return -1;
        }
        return file.length();
    }

    public static long getFileSize(File file) {
        if (!file.exists() || !file.isFile()) {
            System.out.println("文件不存在");
            return -1;
        }
        return file.length();
    }

/*
    public static String genChecksum(String file, CheckSumAlgoType checkSumAlgoType) {
        */
/**
         * 使用org.apache.commons.codec.digest.DigestUtils
         *//*

        String checksum = null;
        try {
            switch (checkSumAlgoType) {
                case MD5:
                    checksum = DigestUtils.md5Hex(new FileInputStream(file));
                    break;

                case SHA_1:
                    checksum = DigestUtils.sha1Hex(new FileInputStream(file));
                    break;

                case SHA_256:
                    checksum = DigestUtils.sha256Hex(new FileInputStream(file));
                    break;
                case SHA_512:
                    checksum = DigestUtils.sha512Hex(new FileInputStream(file));
                    break;
                default:
                    checksum = DigestUtils.md5Hex(new FileInputStream(file));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        return checksum;
    }

    public static String genChecksum(File file, CheckSumAlgoType checkSumAlgoType) {
        */
/**
         * 使用org.apache.commons.codec.digest.DigestUtils
         *//*

        String checksum = null;
        try {
            switch (checkSumAlgoType) {
                case MD5:
                    checksum = DigestUtils.md5Hex(new FileInputStream(file));
                    break;

                case SHA_1:
                    checksum = DigestUtils.sha1Hex(new FileInputStream(file));
                    break;

                case SHA_256:
                    checksum = DigestUtils.sha256Hex(new FileInputStream(file));
                    break;
                case SHA_512:
                    checksum = DigestUtils.sha512Hex(new FileInputStream(file));
                    break;
                default:
                    checksum = DigestUtils.md5Hex(new FileInputStream(file));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        return checksum;
    }
*/

    public static void copy(String source, String dest, int bufferSize) {
        InputStream in = null;
        OutputStream out = null;
        try {
            in = new FileInputStream(source);
            out = new FileOutputStream(dest);

            byte[] buffer = new byte[bufferSize];
            int len;

            while ((len = in.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }
            in.close();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static boolean checkDir(String dirName) {
        try {
            File dir = new File(dirName);
            if (dir.exists()) {
                System.out.println("创建目录" + dirName + "失败，目标目录已经存在");
            } else {
                if (!dirName.endsWith(File.separator)) {
                    dirName = dirName + File.separator;
                }
                if (dir.mkdirs()) {
                    System.out.println("创建目录" + dirName + "成功！");
                } else {
                    System.out.println("创建目录" + dirName + "失败！");
                    return false;
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    //删除文件夹
    //param folderPath 文件夹完整绝对路径
    public static void delFolder(String folderPath) {
        try {
            delAllFile(folderPath); //删除完里面所有内容
            String filePath = folderPath;
            filePath = filePath.toString();
            File myFilePath = new File(filePath);
            myFilePath.delete(); //删除空文件夹
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //删除指定文件夹下所有文件
    //param path 文件夹完整绝对路径
    public static boolean delAllFile(String path) {
        boolean flag = false;
        File file = new File(path);
        if (!file.exists()) {
            return flag;
        }
        if (!file.isDirectory()) {
            return flag;
        }
        String[] tempList = file.list();
        File temp = null;
        for (int i = 0; i < tempList.length; i++) {
            if (path.endsWith(File.separator)) {
                temp = new File(path + tempList[i]);
            } else {
                temp = new File(path + File.separator + tempList[i]);
            }
            if (temp.isFile()) {
                temp.delete();
            }
            if (temp.isDirectory()) {
                delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
                delFolder(path + "/" + tempList[i]);//再删除空文件夹
                flag = true;
            }
        }
        return flag;
    }

}