package eu.stratuslab.storage.disk.utils;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Writer;

import org.restlet.data.Status;
import org.restlet.resource.ResourceException;

public class FileUtils {

	public static void appendToFile(File file, String contents) {
		Writer writer = null;

		try {
			writer = new FileWriter(file, true);
			writer.append(contents);
		} catch (IOException e) {
			throw new ResourceException(Status.SERVER_ERROR_INTERNAL,
					"An error occured while writting " + file.getName());
		} finally {
			if (writer != null) {
				try {
					writer.close();
				} catch (IOException consumed) {
					throw new ResourceException(Status.SERVER_ERROR_INTERNAL,
							"An error occured while closing " + file.getName());
				}
			}
		}
	}

	public static void writeToFile(File file, String contents) {
		Writer writer = null;

		try {
			writer = new FileWriter(file);
			writer.write(contents);
		} catch (IOException e) {
			throw new ResourceException(Status.SERVER_ERROR_INTERNAL,
					"An error occured while writting " + file.getName());
		} finally {
			if (writer != null) {
				try {
					writer.close();
				} catch (IOException consumed) {
					throw new ResourceException(Status.SERVER_ERROR_INTERNAL,
							"An error occured while closing " + file.getName());
				}
			}
		}
	}

	public static void createZeroFile(File file, int sizeInGB) {
		OutputStream ostream = null;

		try {
			ostream = new FileOutputStream(file);

			// Create 1 MB buffer of zeros.
			byte[] buffer = new byte[1024000];

			for (int i = 0; i < 1000 * sizeInGB; i++) {
				ostream.write(buffer);
			}
		} catch (IOException e) {
			throw new ResourceException(Status.SERVER_ERROR_INTERNAL,
					"An error occured while creating a block file "
							+ file.getName());
		} finally {
			if (ostream != null) {
				try {
					ostream.close();
				} catch (IOException consumed) {
					throw new ResourceException(Status.SERVER_ERROR_INTERNAL,
							"An error occured while closing block file "
									+ file.getName());
				}
			}
		}

	}

	public static Boolean fileHasLine(File file, String line) {
		Boolean isPresent = false;
		DataInputStream in = null;
		FileInputStream is = null;
		BufferedReader br = null; 
		
		try {
			is = new FileInputStream(file);
			in = new DataInputStream(is);
			br = new BufferedReader(new InputStreamReader(in));
			
			String currentLine;

			while ((currentLine = br.readLine()) != null) {
				if (currentLine.equals(line)) {
					isPresent = true;
					break;
				}
			}
		} catch (FileNotFoundException e) {
			throw new ResourceException(Status.SERVER_ERROR_INTERNAL,
					"An error occured will reading file " + file.getName());
		} catch (IOException e) {
			throw new ResourceException(Status.SERVER_ERROR_INTERNAL,
					"An error occured will reading file " + file.getName());
		} finally {
			try {
				if (in != null) {
					in.close();
				}
				if (is != null) {
					is.close();
				}
				if (br != null) {
					br.close();
				}
			} catch (IOException e) {
				throw new ResourceException(Status.SERVER_ERROR_INTERNAL,
						"An error occured while closing file " + file.getName());
			}
		}

		return isPresent;
	}
}

@@ -1316,33 +1316,181 @@
 itting %22
- + file.getName()
+%0A%09%09%09%09%09%09%09+ file.getAbsolutePath()%0A%09%09%09%09%09%09%09+ %22.%5Cn Free space on device: %22%0A%09%09%09%09%09%09%09+ String.valueOf(file.getParentFile()%0A%09%09%09%09%09%09%09%09%09.getFreeSpace() / (1024 * 1024)) + %22MB.%22
 );%0A%09%09%7D f
@@ -1690,39 +1690,56 @@
 le closing %22
- + file.getName
+%0A%09%09%09%09%09%09%09%09%09+ file.getAbsolutePath
 ());%0A%09%09%09%09%7D%0A%09
@@ -2666,12 +2666,9 @@
 ull;
- %0A%09%09
+%0A
 %0A%09%09t
@@ -2795,19 +2795,16 @@
 r(in));%0A
-%09%09%09
 %0A%09%09%09Stri

Improve error reporting in file creation