package cologne.eck.tools;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import cologne.eck.all_peas.data.PeaProperties;
public class FileTools {
/**
* Get all files of a directory including sub-directories
* as an unsorted list.
*
* @param directory the directory
* @param filesOnly true if only file should be included
* false if files and directories should be included
*
* @return an unsorted list of all included files
* or null if the directory is null
* or is not to a directory
*/
public static ArrayList <File> listAllFilesOfFolder(File directory, boolean filesOnly) {
if (directory == null) {
System.err.println("FileTools: directory is null");
return null;
}
if (! directory.isDirectory()) {
System.err.println("FileTools: argument is not directory");
return null;
}
ArrayList<File> resultList = new ArrayList<File>();
File[] fileList = directory.listFiles();
if (fileList == null) {
System.err.println("FileTools: No files in directory");
return null;
}
int len = fileList.length;
for (int i = 0; i < len; i++) {
File file = fileList[i];
//resultList.add(file);
if (file.isFile()) {
resultList.add(file);
//System.out.println(file.getAbsolutePath());
} else if (file.isDirectory()) {
if (filesOnly == false){
resultList.add(file);
}
if (listAllFilesOfFolder(file, filesOnly) != null){
resultList.addAll(listAllFilesOfFolder(file, filesOnly));
}
}
}
return resultList;
}
/**
* Get all names of files of a directory including sub-directories
* as an unsorted list.
*
* @param directoryName the name of the directory
* @param filesOnly true if only file should be included
* false if files and directories should be included
*
* @return an unsorted list of all included file names
* or null if the directory name is null
* or refers not to a directory
*/
public static ArrayList <String> listAllFileNamesOfFolder(String directoryName, boolean filesOnly) {
if (directoryName == null) {
System.err.println("FileTools: directory nam is null");
return null;
}
File directory = new File(directoryName);
if (! directory.isDirectory()) {
System.err.println("FileTools: argument is not directory");
return null;
}
ArrayList<String> resultList = new ArrayList<String>();
File[] fileList = directory.listFiles();
int len = fileList.length;
for (int i = 0; i < len; i++) {
File file = fileList[i];
resultList.add(file.getAbsolutePath());
if (file.isFile()) {
resultList.add(file.getAbsolutePath());
//System.out.println(file.getAbsolutePath());
} else if (file.isDirectory()) {
if (filesOnly == false){
resultList.add(file.getAbsolutePath());
}
resultList.addAll(listAllFileNamesOfFolder(file.getAbsolutePath(), filesOnly));
}
}
return resultList;
}
/**
* Get all names of files of a directory including sub-directories
* as an array of Strings.
*
* @param directoryName the name of the directory
* @param filesOnly true if only file should be included
* false if files and directories should be included
*
* @return an array of all included file names
* or null if the directory name is null
* or refers not to a directory
*/
public static String[] getAllFileNamesOfFolder(String directoryName, boolean filesOnly) {
ArrayList <String> list = listAllFileNamesOfFolder(directoryName, filesOnly);
if (list == null) {
return null;
} else {
String[] array = new String[list.size()];
return list.toArray(array);
}
}
/**
* Get all files of a directory including sub-directories
* as an array of Strings.
*
* @param directoryName the directory
* @param filesOnly true if only file should be included
* false if files and directories should be included
*
* @return an array of all included files
* or null if the directory name is null
* or refers not to a directory
*/
public static File[] getAllFilesOfFolder(File directory, boolean filesOnly) {
ArrayList <File> list = listAllFilesOfFolder(directory, filesOnly);
if (list == null) {
return null;
} else {
File[] array = new File[list.size()];
//System.out.println("list size: " + array.length);
return list.toArray(array);
}
}
/**
* Get the file size in bytes
*
* @param fileName the name of the file
* @return the number of bytes (long value)
*/
public final static long getFileSize(File file) {
RandomAccessFile raf = null;
if (file.isDirectory() ) {
return 0;
}
long result = 0;
try {
raf = new RandomAccessFile(file, "r");
result = raf.length();
raf.close();
} catch (FileNotFoundException e) {
// this will be thrown if file is not file and not directory
if ( PeaProperties.getWorkingMode().equals("-t")) {
System.err.println(e.toString() + ", " + e.getMessage() + ": " + file.getAbsolutePath());
}
} catch (IOException e) {
if ( PeaProperties.getWorkingMode().equals("-t")) {
System.err.println(e.toString() + ", " + e.getMessage() + ": " + file.getAbsolutePath());
}
} catch (Exception e) {
if ( PeaProperties.getWorkingMode().equals("-t")) {
System.err.println(e.toString() + ", " + e.getMessage() + ": " + file.getAbsolutePath());
}
}
return result;
}
/**
* Get the file size in bytes
*
* @param fileName the name of the file
* @return the number of bytes (long value)
*/
public final static long getFileSize(String fileName) {
RandomAccessFile raf = null;
File file = new File(fileName);
if (file.isDirectory() ) {
return 0;
}
long result = 0;
try {
raf = new RandomAccessFile(file, "r");
result = raf.length();
raf.close();
} catch (FileNotFoundException e) {
// this will be thrown if file is not file and not directory
if ( PeaProperties.getWorkingMode().equals("-t")) {
System.err.println(e.toString() + ", " + e.getMessage() + ": " + fileName);
}
} catch (IOException e) {
if ( PeaProperties.getWorkingMode().equals("-t")) {
System.err.println(e.toString() + ", " + e.getMessage() + ": " + fileName);
}
} catch (Exception e) {
if ( PeaProperties.getWorkingMode().equals("-t")) {
System.err.println(e.toString() + ", " + e.getMessage() + ": " + fileName);
}
}
return result;
}
/**
* Get the number and overall size of files.
* There is no warning for long execution time!
*
* @param fileNames an array of file names
* @return an array of long values: the first value [0] is the
* number of the containing files (no directories),
* the second [1] is the size of all included files.
* There is no warning for long execution time!
*/
public static final long[] getNumberAndSize(String[] fileNames){
long[] result = new long[2];
if (fileNames == null){
System.err.println("FileTools getNumberAndSize[]: fileNames null");
return null;
}
int len = fileNames.length;
if (len == 0) {
result[0] = 0;
result[1] = 0;
return result;
}
RandomAccessFile raf = null;
for (int i = 0; i < len; i++) {
if (! new File(fileNames[i]).isFile()){
// do not count directories and other
continue;
}
try {
raf = new RandomAccessFile(fileNames[i], "r");
result[1] += raf.length();
raf.close();
} catch (FileNotFoundException e) {
System.err.println(e.toString() +" FileTools getNumberAndSize[]: " + e.getMessage() + ", " + fileNames[i]);
} catch (IOException e) {
//read_write_failed = true;
System.err.println(e.toString() + " FileTools getNumberAndSize[]: " + e.getMessage() + ", " + fileNames[i]);
//e.printStackTrace();
} catch (Exception e) {
System.err.println(e.toString() + " - " + e.getMessage() + ", " + fileNames[i]);
//e.printStackTrace();
}
// only count files, not folders:
result[0] ++;
}
return result;
}
/**
* Get the number of files of a directory - inclusive
* all files of sub-directories, but no directory itself.
* There is no warning for long execution time!
*
* @param directoryName the name of the directory to check
* @return an array of long values: the first value [0] is the
* number of the containing files (no directories),
* the second [1] is the size of all included files.
* There is no warning for long execution time!
*/
public static final long[] getNumberAndSize(String directoryName) {
File directory = new File(directoryName);
RandomAccessFile raf = null;
long[] result = new long[2];
File[] children = directory.listFiles();
if (children != null) {
for (int i = 0; i < children.length; i++) {
if (children[i].isDirectory() ) {
long[] dirResult = getNumberAndSize(children[i].getAbsolutePath());
// function was broken because of extremeSizeWarning
// this avoids NullPointerException
if (dirResult == null) {
return null;
}
result[0] += dirResult[0];
result[1] += dirResult[1];
} else if (children[i].isFile() ) {
try {
raf = new RandomAccessFile(children[i], "r");
result[1] += raf.length();
raf.close();
} catch (FileNotFoundException e) {
//file_not_found = true;
System.err.println("FileTools getNumberAndSize: " + e.getMessage() + ", " + children[i]);
//e.printStackTrace();
} catch (IOException e) {
//read_write_failed = true;
System.err.println("FileTools getNumberAndSize: " + e.getMessage() + ", " + children[i]);
//e.printStackTrace();
} catch (Exception e) {
System.err.println(e.toString() + " - " + e.getMessage() + ", " + children[i]);
//e.printStackTrace();
}
// only count files, not folders:
result[0] ++;
}
}
}
return result;
}
/**
* Checks a file if it is a valid to encrypt/decrypt or not.
* Checks existence, read and write access, if empty or too large.
* If the file is invalid, the function return one of these five
* error string:
* can_not_find, can_not_read, can_not_write, empty_file, file_too_large.
* If the file is valid, the functions returns null.
*
* @param file the file to check
* @param printErrors print error messages to stderr (true) or not (false)
*
* @return an error string if the file is invalid
* or null if the file is valid
*/
public final static String checkAccess(File file, boolean printErrors) {
if ( ! file.exists() ) {
if (printErrors == true) {
System.err.println("File does not exist: " + file.getAbsolutePath());
}
return "can_not_find";
}
if ( ! file.canRead() ) {
if (printErrors == true) {
System.err.println("No read access to file: " + file.getAbsolutePath());
}
return "can_not_read";
}
if ( ! file.canWrite() ) {
if (printErrors == true) {
System.err.println("No write access to file: " + file.getAbsolutePath());
}
return "can_not_write";
}
if ( file.length() == 0 && ! file.isDirectory() ) {
if (printErrors == true) {
System.err.println("Empty file: " + file.getAbsolutePath());
}
return "empty_file";
}
if ( file.length() > Long.MAX_VALUE || file.length() < 0) {
if (printErrors == true) {
System.err.println("Invalid file size: " + file.getAbsolutePath());
}
return "file_too_large";
}
return null;
}
/**
* Get all parent directory names of a file as list.
* The direct directory is the first element of the
* list, the root directory the last element.
*
* @param file the file
*
* @return the list of all parent directory names, where
* the direct parent directory is the first element
*/
/* public static ArrayList<String> listParentDirectoryNames(File file) {
!!! untested !!!
ArrayList<String> list = new ArrayList<String>();
String parentName = file.getParent();
if (parentName == null) {
return null;
}
File parent = new File(parentName);
while (parent.isDirectory()) {
list.add(parentName);
parentName = parent.getParent();
if (parentName == null ){
break;
}
parent = new File(parentName);
if (parent.exists() == false) {
break;
}
}
if (list.isEmpty()){
return null;
} else {
return list;
}
} */
}