package test.net.jawr.web;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Reader;
import java.io.StringWriter;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;

public class FileUtils {

	public static File createDir(String pathName) throws Exception {
		File dir = new File(getClasspathRootDir() + pathName);
		if (!dir.exists())
			dir.mkdir();
		return dir;
	}

	public static void clearDirectory(String path) {
		deleteDirectory(path);
		File dir = new File(path);
		if (!dir.exists())
			dir.mkdir();
	}
	
	public static boolean deleteDirectory(String path) {
		return deleteDirectory(new File(path));
	}
	
	public static boolean deleteDirectory(File path) {
		if (path.exists()) {
			File[] files = path.listFiles();
			for (int i = 0; i < files.length; i++) {
				if (files[i].isDirectory()) {
					deleteDirectory(files[i]);
				} else {
					files[i].delete();
				}
			}
		}
		return (path.delete());
	}

	/**
	 * Removes the carriage return from he string passed in parameter.
	 * @param str the string
	 * @return a string without carriage return.
	 */
	public static String removeCarriageReturn(String str){
		return str.replaceAll("\r\n", "\n");
	}
	
	public static String readClassPathFile(String path) throws Exception {
		
		return removeCarriageReturn(readFile(getClassPathFile(path)).toString());
	}

	public static String readFile(File toRead) throws Exception {
		StringWriter sw = new StringWriter();
		FileInputStream fis = new FileInputStream(toRead);
		FileChannel inchannel = fis.getChannel();
		Reader rd = Channels.newReader(inchannel, Charset.forName("UTF-8").newDecoder(), -1);
		int i;
		while ((i = rd.read()) != -1)
			sw.write(i);
		rd.close();
		sw.close();

		return removeCarriageReturn(sw.getBuffer().toString());
	}
	
	public static String getClasspathRootDir() throws Exception {
		File tmpRoot = getClassPathFile("test.properties").getParentFile();
		return tmpRoot.getCanonicalPath().replaceAll("%20", " ");
	}

	public static File getClassPathFile(String pathName) throws Exception {
		// In windows, pathnames with spaces are returned as %20
		if (pathName.indexOf("%20") != -1)
			pathName = pathName.replaceAll("%20", " ");
		File tmp = new File(getResourceURL(pathName).getFile());
		return tmp;
	}

	public static URL getResourceURL(String resource) throws IOException {
		URL url = null;
		ClassLoader loader = Thread.currentThread().getContextClassLoader();
		if (loader != null)
			url = loader.getResource(resource);
		if (url == null)
			url = ClassLoader.getSystemResource(resource);
		if (url == null)
			throw new IOException("Resource " + resource + " was not found");
		return url;
	}
}

@@ -261,42 +261,8 @@
 el;%0D
-%0Aimport java.nio.charset.Charset;%0D
 %0A%0D%0Ap
@@ -1502,39 +1502,290 @@
 ile(
-File toRead) throws Exception %7B
+String filePath) throws Exception %7B%0D%0A%09%09%0D%0A%09%09return readFile(new File(filePath));%0D%0A%09%7D%0D%0A%09%0D%0A%09public static String readFile(File toRead) throws Exception %7B%0D%0A%09%09%0D%0A%09%09return readFile(toRead, %22UTF-8%22);%0D%0A%09%7D%0D%0A%09%0D%0A%09public static String readFile(File toRead, String charset) throws Exception %7B%0D%0A%09%09
 %0D%0A%09%09
@@ -1970,49 +1970,15 @@
 el, 
-C
+c
 harset
-.forName(%22UTF-8%22).newDecoder(), -1
 );%0D%0A

Update test