package jp.co.y_net.amm.tools;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

public class ResourceCopy {
    public static void main(String[] args) {
        
    }
    
    /*
     * 〓〓 以下、共通部品 〓〓
     */
    /**
     * チャネルを利用したファイルのコピー処理
     * 
     * @param formFile コピー元
     * @param toFileName コピー先
     */
    public static void copy(File formFile, String toFileName) {
        FileChannel ifc = null;
        FileChannel ofc = null;
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            /* 入力元ファイルストリームからチャネル取得 */
            fis = new FileInputStream(formFile);
            ifc = fis.getChannel();

            /* 出力先ファイルチャネル取得 */
            File outFile = new File(toFileName);
            fos = new FileOutputStream(outFile);
            ofc = fos.getChannel();

            /* バイト転送 */
            ifc.transferTo(0, ifc.size(), ofc);

        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (ifc != null)
                    ifc.close();
                if (fis != null)
                    fis.close();
                if (ofc != null)
                    ofc.close();
                if (fos != null)
                    fos.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
    
    /**
     * テキストファイルを読み込む
     * 
     * @param filepath
     * @param charsetName 例:"UTF-8"
     * @return List<String>
     */
    private static List<String> readText(String filepath, String charsetName) {
        List<String> result = new ArrayList<String>();
        FileInputStream is = null;
        InputStreamReader r = null;
        BufferedReader br = null;
        try {
            is = new FileInputStream(filepath);
            r = new InputStreamReader(is, Charset.forName(charsetName));
            br = new BufferedReader(r);
            // int ch;
            // while ((ch = in.read()) != -1) {
            String line;
            while ((line = br.readLine()) != null) {
                result.add(line);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (br != null)
                    br.close();
                if (r != null)
                    r.close();
                if (is != null)
                    is.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return result;
    }
    public static List<String> readText(String filepath) {
        return readText(filepath, "UTF-8");
    }
    /**
     * テキストファイルを書き出す
     * 
     * @param filepath
     * @param charsetName 例:"UTF-8"
     * @param text
     */
    private static void writeText(String filepath, String charsetName, List<String> text) {
        File f = new File(filepath);
        OutputStream os = null;
        Writer w = null;
        BufferedWriter bw = null;
        try {
            os = new FileOutputStream(f);
            w = new OutputStreamWriter(os, charsetName);
            bw = new BufferedWriter(w);
            for (String line : text) {
                bw.write(line);
                bw.newLine();
                // bw.flush();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (bw != null)
                    bw.close();
                if (w != null)
                    w.close();
                if (os != null)
                    os.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
    public static void writeText(String filepath, List<String> string) {
        writeText(filepath, "UTF-8", string);
    }
}
