Skip to content

Commit

Permalink
In progress
Browse files Browse the repository at this point in the history
  • Loading branch information
ymenager committed Jan 22, 2018
1 parent 50b7feb commit ade1257
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 11 deletions.
63 changes: 60 additions & 3 deletions core/src/main/java/com/kloudtek/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,47 @@
import java.util.Set;

public class FileUtils {
public static void mkdir(File file) throws IOException {
if( ! file.mkdir() ) {
throw new IOException("Unable to create directory: "+file.getPath());
}
}

public static void mkdirs(File file) throws IOException {
if( ! file.mkdirs() ) {
throw new IOException("Unable to create directory: "+file.getPath());
}
}

public static void copy( File src, File dst ) throws IOException {
FileInputStream is = null;
FileOutputStream os = null;
try {
is = new FileInputStream(src);
os = new FileOutputStream(dst);
IOUtils.copy(is,os);
} finally {
IOUtils.close(is,os);
}
}

/**
* Copy the files in the specified directory to another directory
* @param dir Source directory
* @param destination Destination Directory
*/
public static void copyFilesInDir(File dir, File destination) throws IOException {
for (File file : listFileInDir(dir)) {
File destFile = new File(destination, file.getName());
if( file.isDirectory() ) {
mkdir(destFile);
copyFilesInDir(file,destFile);
} else {
copy(file,destFile);
}
}
}

public static byte[] toByteArray(File file) throws IOException {
FileInputStream is = new FileInputStream(file);
try {
Expand All @@ -16,6 +57,25 @@ public static byte[] toByteArray(File file) throws IOException {
}
}

public static void checkFileIsDirectory(File... files) throws IOException {
for (File file : files) {
if( ! file.exists() ) {
throw new IOException("File "+file.getPath()+" doesn't exist");
}
if( ! file.isDirectory() ) {
throw new IOException("File "+file.getPath()+" isn't a directory");
}
}
}

public static File[] listFileInDir(File directory) throws IOException {
File[] files = directory.listFiles();
if( files == null ) {
throw new IOException("Unable to list files in directory: "+directory.getPath());
}
return files;
}

/**
* List all files/directories in a directory
*
Expand All @@ -27,9 +87,6 @@ public static byte[] toByteArray(File file) throws IOException {
* @throws IOException If an error listing the file occurs
*/
public static Set<String> listAllFilesNames(File directory, boolean recursive, boolean includeFiles, boolean includeDirs) throws IOException {
if (!directory.isDirectory()) {
throw new IOException("File is not a directory: " + directory.getPath());
}
HashSet<String> filepaths = new HashSet<String>();
recursiveFileNameList(filepaths, directory, null, recursive, includeFiles, includeDirs);
return filepaths;
Expand Down
16 changes: 9 additions & 7 deletions core/src/main/java/com/kloudtek/util/io/IOUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,16 @@ public static String toString(Reader reader) throws IOException {
/**
* Close a closeable object, suppressing any resulting exception
*
* @param closeable Closeable object, can be null (in which case nothing will be done)
* @param closeables Closeable objects which can be null (in which case nothing will be done)
*/
public static void close(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (Throwable e) {
// needs to be throwable because in some cases closing a classpath resource jar throws ExceptionInInitializerError ?!?!?!?!?!?
public static void close(Closeable... closeables) {
for (Closeable closeable : closeables) {
if (closeable != null) {
try {
closeable.close();
} catch (Throwable e) {
// needs to be throwable because in some cases closing a classpath resource jar throws ExceptionInInitializerError ?!?!?!?!?!?
}
}
}
}
Expand Down
1 change: 0 additions & 1 deletion core/src/test/java/com/kloudtek/util/FileUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ private Set<String> createTmpDirAndListFilenames(boolean recursive, boolean incl
}
}


@NotNull
private static HashSet<String> asSet(String... filenames) {
return new HashSet<String>(Arrays.asList(filenames));
Expand Down

0 comments on commit ade1257

Please sign in to comment.