import gnu.regexp.RE;
-import java.io.BufferedInputStream;
-import java.io.BufferedOutputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.FilenameFilter;
-import java.io.IOException;
-import java.io.InputStream;
+import java.io.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
protected FileRoutines() {
}
+ /**
+ * Copy a file
+ */
public static void copyFile(File aSourceFile, File aDestinationFile) throws IOException {
BufferedInputStream inputStream;
BufferedOutputStream outputStream;
int nrBytesRead;
byte[] buffer = new byte[FILE_COPY_BUFFER_SIZE];
- inputStream = new BufferedInputStream(
- new FileInputStream(aSourceFile));
+ inputStream = new BufferedInputStream(new FileInputStream(aSourceFile));
try {
File directory = new File(aDestinationFile.getParent());
- if (directory!=null && !directory.exists()){
- directory.mkdirs();
+ if (directory != null && !directory.exists()) {
+ directory.mkdirs();
}
- outputStream = new BufferedOutputStream(
- new FileOutputStream(aDestinationFile),8192);
+ outputStream = new BufferedOutputStream(new FileOutputStream(aDestinationFile), 8192);
try {
do {
nrBytesRead = inputStream.read(buffer);
- if (nrBytesRead>0)
+ if (nrBytesRead > 0)
outputStream.write(buffer, 0, nrBytesRead);
}
- while (nrBytesRead>=0);
+ while (nrBytesRead >= 0);
}
finally {
outputStream.close();
}
}
+ /**
+ * Copy a directory recursively
+ */
public static void copyDirectory(File aSourceDirectory, File aDestinationDirectory) throws IOException {
int i;
File sourceFile;
if (!aDestinationDirectory.exists())
aDestinationDirectory.mkdirs();
- for (i=0; i<files.length; i++) {
+ for (i = 0; i < files.length; i++) {
sourceFile = files[i];
- destinationFile=new File(aDestinationDirectory, sourceFile.getName());
+ destinationFile = new File(aDestinationDirectory, sourceFile.getName());
if (sourceFile.isDirectory()) {
if (!destinationFile.exists())
destinationFile.mkdir();
}
}
+ /**
+ * Copy a file or directory. If the source is a file and the destination
+ * a directory, the file is copied using the same file name into the\
+ * directory.
+ *
+ * @param aSource the source file
+ * @param aDestination the destination file
+ */
public static void copy(File aSource, File aDestination) throws IOException {
if (aSource.isDirectory()) {
copyDirectory(aSource, aDestination);
FileInputStream inputStream = new FileInputStream(aSource);
FileOutputStream outputStream = new FileOutputStream(aDestination);
try {
- int count=inputStream.read(buffer);
- while (count>0) {
+ int count = inputStream.read(buffer);
+ while (count > 0) {
outputStream.write(buffer, 0, count);
- count=inputStream.read(buffer);
+ count = inputStream.read(buffer);
}
}
finally {
* Return all files in a directory
*
* @param aDirectory The directory to list
- * @param aFilter the filter to apply to files
- * @return a <code>List</code> of filenames of type <code>String</code>
+ * @param aFilter the filter to apply to files
+ * @return a <code>List</code> of filenames of type <code>String</code>
*/
public static List getDirectoryContentsAsList(File aDirectory, FilenameFilter aFilter) {
Object[] contents = aDirectory.list(aFilter);
- if (contents==null) {
+ if (contents == null) {
return Collections.EMPTY_LIST;
}
- return Arrays.asList(contents);
+ return Arrays.asList(contents);
}
/**
*/
public static String getExtension(String aPath) {
int position = aPath.lastIndexOf('.');
- if (position>=0) {
- return aPath.substring(position+1);
+ if (position >= 0) {
+ return aPath.substring(position + 1);
}
- return "";
+ return "";
}
public static boolean isAbsolutePath(String aPath) {
* {@link File}.
*
* @param aBasePath The base path to use for relative paths
- * @param aPath The path to transform
+ * @param aPath The path to transform
* @return An absolute representation of the supplied path
*/
public static File getAbsoluteOrRelativeFile(File aBasePath, String aPath) {
return new File(aPath);
}
- return new File(aBasePath, aPath);
+ return new File(aBasePath, aPath);
}
/**
- * Reads the content of a file into a string
- *
- * TODO: The encoding to be used has not been taken into account: the routine now
- * assumes the file is encoded according to the JVM/platform default
- */
- public static String readFileIntoString(String fileName) throws IOException {
- return new String(readFileIntoByteArray(fileName));
- }
+ * Reads the content of a file into an array of bytes
+ */
+ public static byte[] readFileIntoByteArray(String fileName) throws IOException {
+ InputStream input = new FileInputStream(fileName);
+ ByteArrayOutputStream result = new ByteArrayOutputStream();
- /**
- * Reads the content of a file into an array of bytes
- *
- * TODO: add a loop: {@link java.io.InputStream#available()} is not
- * the total number of bytes left in the stream, but depends on buffering
- * etc.
- */
- public static byte[] readFileIntoByteArray(String fileName) throws IOException {
- InputStream input = new FileInputStream(fileName);
- int size= input.available();
- byte result[] = new byte[size];
- input.read(result);
+ IORoutines.copyStream(input, result);
- return result;
- }
+ return result.toByteArray();
+ }
/**
* Creates all parent directories of a file if they do not exist
*/
- public static void createParentDirectories(File aFile) {
- if (aFile.getParentFile()!=null && !aFile.getParentFile().exists()) {
- aFile.getParentFile().mkdirs();
- }
- }
- }
\ No newline at end of file
+ public static void createParentDirectories(File aFile) {
+ if (aFile.getParentFile() != null && !aFile.getParentFile().exists()) {
+ aFile.getParentFile().mkdirs();
+ }
+ }
+}
\ No newline at end of file