package com.mcbans.firestar.mcbans.config;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.nio.channels.FileChannel;

import com.mcbans.firestar.mcbans.MCBans;

public class FileStructure {
    /**
     * Create directory
     * @param dir
     */
    public static void createDir(final File dir){
        // 既に存在すれば作らない
        if (dir.isDirectory()){
            return;
        }
        if (!dir.mkdir()){
            System.out.print("[MCBans] Can't create directory: " + dir.getName());
        }
    }

    /**
     * Copy file from [srcPath] to [destPath]
     * Use FileChannel#transferTo
     * @param srcPath From Path
     * @param destPath  To Path
     * @throws IOException IOException
     */
    public static void copyTransfer(String srcPath, String destPath) throws IOException {
        FileChannel srcChannel = null, destChannel = null;
        try {
            srcChannel = new FileInputStream(srcPath).getChannel();
            destChannel = new FileOutputStream(destPath).getChannel();

            srcChannel.transferTo(0, srcChannel.size(), destChannel);
        } finally {
            srcChannel.close();
            destChannel.close();
        }
    }

    /**
     * Extract resource file
     * @param from From path
     * @param to To path
     * @param force if true, override exist file
     * @param lang if true, extract with another method
     */
    static void extractResource(String from, File to, boolean force, boolean lang){
        File of = to;

        // if to path is directory, cast to File. return if not file or directory
        if (to.isDirectory()){
            String filename = new File(from).getName();
            of = new File(to, filename);
        }else if(!of.isFile()){
            System.out.print("[MCBans] not a file:" + of);
            return;
        }

        // if file exist, check force flag
        if (of.exists() && !force){
            return;
        }

        OutputStream out = null;
        InputStream in = null;
        InputStreamReader reader = null;
        OutputStreamWriter writer =null;
        DataInputStream dis = null;
        try{
            // get inside jar resource uri
            URL res = MCBans.class.getResource(from);
            if (res == null){
                System.out.print("[MCBans] Can't find "+ from +" in plugin Jar file");
                return;
            }
            URLConnection resConn = res.openConnection();
            resConn.setUseCaches(false);
            in = resConn.getInputStream();

            if (in == null){
                System.out.print("[MCBans] Can't get input stream from " + res);
            }else{
                // write file
                if (lang){
                    reader = new InputStreamReader(in, "UTF-8");
                    writer = new OutputStreamWriter(new FileOutputStream(of)); // not specify output encode

                    int text;
                    while ((text = reader.read()) != -1){
                        writer.write(text);
                    }
                }else{
                    out = new FileOutputStream(of);
                    byte[] buf = new byte[1024]; // Buffer size
                    int len = 0;
                    while((len = in.read(buf)) >= 0){
                        out.write(buf, 0, len);
                    }
                }
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }finally{
            // close stream
            try{
                if (out != null)
                    out.close();
                if (in != null)
                    in.close();
                if (reader != null)
                    reader.close();
                if (writer != null)
                    writer.close();
            }catch (Exception ex){}
        }
    }
}

@@ -618,19 +618,37 @@
  // 
-%E6%97%A2%E3%81%AB%E5%AD%98%E5%9C%A8%E3%81%99%E3%82%8C%E3%81%B0%E4%BD%9C%E3%82%89%E3%81%AA%E3%81%84
+if already exists, do nothing
 %0D%0A  

Fix japanese comment