package backend;
import java.io.*;
import java.nio.channels.*;

/**
 * Copy of FileUtils class in http://www.rgagnon.com/javadetails/java-0064.html
 * @author Réal Gagnon, modified by Pyry Kröger
 *
 */
public class FileUtils{
    public static void copyFile(File in, File out)
    {
    	
    	if (!out.getParentFile().isDirectory()) {
    		out.getParentFile().mkdirs();
    	}
    	FileChannel inChannel = null;
        FileChannel outChannel = null;
        try {
        inChannel = new
            FileInputStream(in).getChannel();
         outChannel = new
            FileOutputStream(out).getChannel();
         inChannel.transferTo(0, inChannel.size(),
                 outChannel);
        } catch (FileNotFoundException e) {
        	System.err.println(e.getLocalizedMessage());
        } catch (IOException e) {
        	
        }
        finally {
        	try {
            if (inChannel != null) inChannel.close();
            if (outChannel != null) outChannel.close();
        	}
        	catch (IOException e) {
        		
        	}
        }
    }
    
    /**
     * @author Pyry Kröger
     */
    public static void copyFile(String inFile, String outFile) throws IOException {
    	FileUtils.copyFile(new File(inFile), new File(outFile));
    }
    
    /**
     * @author Pyry Kröger
     */
    public static void delFile(String filename) {
    	File f = new File(filename);
    	Debug.println("Deleting " + f);
    	f.delete();
    }
    
    /**
     * @author Pyry Kröger
     * @param s 
     */
    public static String getValidFilename(String s) {
		s = s.replaceAll("/", "_");
		s = s.replaceAll("\\\\", "_");
		s = s.replaceAll(":", "_");
		return s;
	}
}
