package mykeynote.misc;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import mykeynote.exceptions.ExceptionString;
import mykeynote.exceptions.FileCreateException;
import mykeynote.exceptions.FileDeleteException;
import mykeynote.exceptions.FileNotWritableException;
import mykeynote.exceptions.FileWriteException;
public class CommonFunctions {
/**
* This is a helper function, which checks if a file exists, is readable,
* writable and executable.
* @param file The file, which has to be checked
* @return This method returns an integer, which corresponds to the {@link <a href="http://en.wikipedia.org/wiki/File_system_permissions#Octal_notation">octal notation</a>}.<br>
* If the file is <b>readable</b>, then the answer is increased by 4, if
* <b>writable</b> by 2, if <b>executable</b> by 1.
* @throws FileNotFoundException This exception is thrown if the specific file does not exist.
*/
public int checkFile(File file) throws FileNotFoundException{
int answer = 0;
if(!file.exists()){
String error = String.format(ExceptionString.FILENOTFOUND.getExceptionString(), file.getAbsoluteFile());
throw new FileNotFoundException(error);
}
if(!file.canRead())
answer +=4;
if(!file.canWrite())
answer +=2;
if(!file.canExecute())
answer +=1;
return answer;
}
/**
* Takes a file, reads it and returns a List of Type String, where each
* element is a new line
*
* @param file
* The file to be read
* @return ArrayList of String, where each element is a line of the file
* @throws FileNotFoundException
* Reports if the file was not found
* @throws IOException
*/
public ArrayList<String> readFile(File file) throws FileNotFoundException, IOException {
ArrayList<String> list = new ArrayList<String>();
String line = "";
BufferedReader read;
read = new BufferedReader(new FileReader(file));
line = read.readLine();
list.add(line);
while ((line = read.readLine()) != null) {
list.add(line);
}
return list;
}
public void writeFile(File file, ArrayList<String> answer, boolean replace) throws FileDeleteException,
FileCreateException, FileNotWritableException, FileWriteException{
String data = answer.get(0);
int i = 1;
while(i < answer.size()){
data += "\n" + answer.get(i++);
}
writeFile(file, data, replace);
}
/**
* This method saves a String to a file.
* @param file The file, where the String should be saved to.
* @param data The data, that should be saved.
* @param replace This variable makes only a difference if the file exists prior
* to the writing. If this variable is set to <b>true</b>, then the file will
* be purged and then created, else, if it is <b>false</b>, it will append
* the new data to the old file.
* @throws FileDeleteException This exception is thrown if the file could not be deleted.
* @throws FileCreateException This exception is thrown if the file could not be created.
* @throws FileNotWritableException This exception is not writable due to write permeations.
* @throws FileWriteException This exception indicates, that during the writing to a file
* an error has occurred.
*/
public void writeFile(File file, String data, boolean replace) throws FileDeleteException,
FileCreateException, FileNotWritableException, FileWriteException{
if(file.exists()){
if(replace){
replaceFile(file);
}
} else {
createFile(file);
}
if(!file.canWrite()){
throw new FileNotWritableException(String.format(ExceptionString.FILENOTWRITABLEEXCEPTION.getExceptionString(),file.getAbsolutePath()));
}
writeFileHelper(file, data);
}
private void writeFileHelper(File file, String data) throws FileWriteException{
BufferedWriter fileWriter = null;
try{
fileWriter = new BufferedWriter(new FileWriter(file, true));
fileWriter.write(data);
fileWriter.flush();
} catch (IOException e){
throw new FileWriteException(String.format(ExceptionString.FILEWRITEEXCEPTION.getExceptionString(), file.getAbsolutePath()));
}
try{
fileWriter.close();
} catch(IOException e) {
//nothing to do...
}
}
private void replaceFile(File file) throws FileDeleteException, FileCreateException{
if(!file.delete()){
throw new FileDeleteException(String.format(ExceptionString.FILEDELETEEXCEPTIOIN.getExceptionString(), file.getAbsolutePath()));
}
createFile(file);
}
private void createFile(File file) throws FileCreateException{
try{
if(!file.createNewFile()){
throw new FileCreateException(String.format(ExceptionString.FILECREATEEXCEPTION.getExceptionString(), file.getAbsolutePath()));
}
} catch (IOException e){
throw new FileCreateException(String.format(ExceptionString.FILECREATEEXCEPTION.getExceptionString(), file.getAbsolutePath()));
}
}
}