package  org.ts.ddcs.util;

import java.io.*;
import java.util.*;

public class FileUtil {

	private FileUtil() {

	}

	public static Map loadProperties(String fileName, String encoding) {

		Map result = null;
		FileInputStream inStream = null;
		try {
			result = new HashMap(1000);
			inStream = new FileInputStream(fileName);
			Properties prop = new Properties();
			prop.load(inStream);
			inStream.close();

			/*
			 * If the method had specialed encoding, so should convert encoding
			 * here.
			 */
			if (encoding != null) {
				Collection col = prop.keySet();
				Iterator iter = col.iterator();
				while (iter.hasNext()) {
					Object strKey = iter.next();
					String tmp = (String) prop.get(strKey);
					tmp = StringUtil.convertStringEnc(tmp, encoding);
					result.put(strKey, tmp);
					// System.out.println(strKey.toString()
					// + " = " + tmp);
				}
			} else {
				result = prop;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (inStream != null) {
					inStream.close();
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
		return result;
	}

	public static void createFilePath(String filePath) throws IOException {

		if (filePath == null || filePath.trim().equals(""))
			throw new IOException("File path name is null.");

		File f = new File(filePath);

		if (!f.exists()) {
			f.mkdirs();
		}
	}

	public static String readFile(String fileName, String encoding)
			throws IOException {

		InputStream is = new FileInputStream(fileName);
		int length = is.available();
		byte buffer[] = new byte[length];
		is.read(buffer, 0, length);
		return new String(buffer, encoding);
	}

	public static void writeFile(String fileName, String data, String encoding)
			throws IOException {

		OutputStream os = new FileOutputStream(fileName);
		byte buffer[] = data.getBytes(encoding);
		os.write(buffer, 0, buffer.length);
	}

	public static void writeFile(String fileName, String data)
			throws IOException {

		OutputStream os = new FileOutputStream(fileName);
		byte buffer[] = data.getBytes();
		os.write(buffer, 0, buffer.length);
	}

	public static void deleteOverdueFiles(String path, int intervalDay)
			throws IOException {

		long currenTime = System.currentTimeMillis();
		long interval = (long) 24 * 60 * 60 * 1000 * intervalDay;
		File reportFiles = new File(path);
		String[] tmpFiles = reportFiles.list();
		if (tmpFiles == null)
			return;
		int size = tmpFiles.length;
		for (int i = 0; i < size; i++) {
			File tmp = new File(path + "/" + tmpFiles[i]);
			long lastTime = tmp.lastModified();
			if ((currenTime - lastTime) > interval)
				tmp.delete();
		}
	}

	public static void deleteOverdueFiles2(String path, int intervalDay)
			throws IOException {

		long currenTime = System.currentTimeMillis();
		long interval = (long) 24 * 60 * 60 * 1000 * intervalDay;
		File reportFiles = new File(path);
		String[] tmpFiles = reportFiles.list();

		if (tmpFiles == null)
			return;
		int size = tmpFiles.length;
		for (int i = 0; i < size; i++) {
			File tmp = new File(path + "/" + tmpFiles[i]);
			if (tmp.isDirectory())
				continue;
			long lastTime = tmp.lastModified();
			if ((currenTime - lastTime) > interval)
				tmp.delete();
		}
	}

	public static void copyFile(File src, File dest) throws Exception {

		FileInputStream in = new FileInputStream(src);
		FileOutputStream out = new FileOutputStream(dest);

		byte[] buffer = new byte[1024];
		int n = 0;
		while (-1 != (n = in.read(buffer))) {
			out.write(buffer, 0, n);
		}

		in.close();
		out.close();
	}

	public static void copyFile(InputStream in, File dest) throws Exception {

		FileOutputStream out = new FileOutputStream(dest);

		byte[] buffer = new byte[1024];
		int n = 0;
		while (-1 != (n = in.read(buffer))) {
			out.write(buffer, 0, n);
		}

		in.close();
		out.close();
	}

	public static void writeStreamToFile(InputStream is, String filePath)
			throws IOException {

		File flowFile = new File(filePath);
		File flowDir = new File(flowFile.getParent());
		if (!flowDir.exists()) {
			flowDir.mkdirs();
		}

		FileOutputStream os = new FileOutputStream(flowFile);

		byte[] buffer = new byte[1024];
		while (is.read(buffer) > 0) {
			os.write(buffer);
		}

		// is.close();
		os.close();
	}

	public static void writeStreamToFile(String is, String filePath)
			throws IOException {

		File flowFile = new File(filePath);
		File flowDir = new File(flowFile.getParent());
		if (!flowDir.exists()) {
			flowDir.mkdirs();
		}

		FileOutputStream os = new FileOutputStream(flowFile);

		os.write(is.getBytes("UTF-8"));
		os.close();
	}

	public static boolean deleteAll(File path) {

		if (path.isFile()) {
			return path.delete();
		}

		if (!path.exists()) {
			return true;
		}

		File[] files = path.listFiles();

		if (files == null)
			return true;

		for (int i = files.length - 1; i >= 0; i--) {
			if (files[i].isDirectory()) {
				deleteAll(files[i]);
			}
			files[i].delete();
		}

		files = path.listFiles();
		return files.length == 0 ? true : false;
	}

	public static boolean deleteAllAndShelf(File path) {

		if (!path.exists())
			return true;

		deleteAll(path);
		return path.delete();
	}

	public static boolean deleteAllAndShelfAndParent(File path) {

		if (!path.exists())
			return true;

		deleteAll(path);
		boolean flag = path.delete();

		if (flag) {
			String file = path.getParent();
			File parentFile = new File(file);
			if (parentFile.isDirectory()) {
				if (parentFile.list().length == 0) {

					deleteAllAndShelf(parentFile);
				}
			}
		}
		return flag;
	}

	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();
		}
	}

	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;
	}

}
