/*
 * Copyright
 *   2007 axYus - www.axyus.com
 *   2005 C.Marchand - christophe.marchand@axyus.com
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * XEMELIOS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package fr.gouv.finances.dgfip.xemelios.utils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author chm
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class FileUtils {
    /**
     * Reads the file and returns the content as a String
     * @param f the file to read
     * @return The content read
     * @throws IOException
     */
    public static String readTextFile(File f, String fileEncoding) throws IOException {
//        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
        StringBuffer sb = new StringBuffer();
        InputStreamReader isr = new InputStreamReader(new FileInputStream(f),fileEncoding);
        char[] buf = new char[255];
        int read = isr.read(buf);
        while(read>0) {
        	sb.append(buf,0,read);
        	read = isr.read(buf);
        }
        isr.close();
//        int count=bis.available();
//        while(count>0) {
//            byte[] b = new byte[count];
//            bis.read(b);
//            sb.append(new String(b));
//            count=bis.available();
//        }
        return sb.toString();
    }
    public static String getFileEncoding(File f) throws IOException {
        String ret = null;
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
        byte[] bb = new byte[255];
        bis.read(bb);
        String buff = new String(bb);
        buff = buff.substring(buff.indexOf("<?"));
        buff = buff.substring(0,buff.indexOf("?>")+2);
        String header = buff;
        int encodingPos = header.indexOf("encoding=");
        if(encodingPos>=0) {
            String s = header.substring(encodingPos+9);
            if(s.startsWith("\"")) {
                ret=s.substring(1,s.indexOf('"',1));
            } else {
                s = s.substring(0,s.length()-2).trim();
                if(s.indexOf(' ')>0) ret = s.substring(0,s.indexOf(' '));
                else ret = s;
            }
        } else {
            InputStreamReader isr = new InputStreamReader(new FileInputStream(f));
            ret = isr.getEncoding();
            isr.close();
        }
        bis.close();
        return ret;
    }

    public static void dropRecursiveDirectory(File dir) throws Exception {
        if(!dir.exists())
            throw new Exception(dir.getAbsolutePath()+" does not exist");
        if(!dir.isDirectory())
            throw new Exception(dir.getAbsolutePath()+" is not a directory");
        File[] files = dir.listFiles();
        for(File f: files) {
            if(f.isDirectory()) dropRecursiveDirectory(f);
            else f.delete();
        }
    }
}
