--- /dev/null
+/*
+ * Copyright (C) 2001-2006 The Mir-coders group
+ *
+ * This file is part of Mir.
+ *
+ * Mir is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Mir is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Mir; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * In addition, as a special exception, The Mir-coders gives permission to link
+ * the code of this program with any library licensed under the Apache Software License,
+ * and distribute linked combinations including the two. You must obey the
+ * GNU General Public License in all respects for all of the code used other than
+ * the above mentioned libraries. If you modify this file, you may extend this
+ * exception to your version of the file, but you are not obligated to do so.
+ * If you do not wish to do so, delete this exception statement from your version.
+ */
+package mir.util;
+
+import gnu.regexp.RE;
+
+import java.io.*;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+public class FileRoutines {
+ protected static final int FILE_COPY_BUFFER_SIZE = 65536;
+
+ 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));
+ try {
+ File directory = new File(aDestinationFile.getParent());
+ if (directory != null && !directory.exists()) {
+ directory.mkdirs();
+ }
+ outputStream = new BufferedOutputStream(new FileOutputStream(aDestinationFile), 8192);
+ try {
+ do {
+ nrBytesRead = inputStream.read(buffer);
+ if (nrBytesRead > 0)
+ outputStream.write(buffer, 0, nrBytesRead);
+ }
+ while (nrBytesRead >= 0);
+ }
+ finally {
+ outputStream.close();
+ }
+ }
+ finally {
+ inputStream.close();
+ }
+ }
+
+ /**
+ * Copy a directory recursively
+ */
+ public static void copyDirectory(File aSourceDirectory, File aDestinationDirectory) throws IOException {
+ int i;
+ File sourceFile;
+ File destinationFile;
+ File[] files = aSourceDirectory.listFiles();
+
+ if (!aDestinationDirectory.exists())
+ aDestinationDirectory.mkdirs();
+
+ for (i = 0; i < files.length; i++) {
+ sourceFile = files[i];
+ destinationFile = new File(aDestinationDirectory, sourceFile.getName());
+ if (sourceFile.isDirectory()) {
+ if (!destinationFile.exists())
+ destinationFile.mkdir();
+ copyDirectory(sourceFile, destinationFile);
+ }
+ else {
+ copyFile(sourceFile, destinationFile);
+ }
+ }
+ }
+
+ /**
+ * 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);
+ }
+ else if (aDestination.isDirectory()) {
+ copyFile(aSource, new File(aDestination, aSource.getName()));
+ }
+ else {
+ copyFile(aSource, aDestination);
+ }
+ }
+
+ /**
+ * Copy the contents of an {@link InputStream} to a {@link File}
+ */
+ public static void copy(InputStream aSource, File aDestination) throws IOException {
+ BufferedOutputStream outputStream =
+ new BufferedOutputStream(new FileOutputStream(aDestination), 8192);
+
+ int read;
+ byte[] buf = new byte[8 * 1024];
+
+ while ((read = aSource.read(buf)) != -1) {
+ outputStream.write(buf, 0, read);
+ }
+
+ aSource.close();
+ outputStream.close();
+ }
+
+ /**
+ * Moves a {@link File} to a new location
+ */
+ public static void move(File aSource, File aDestination) throws IOException {
+ aDestination.getParentFile().mkdirs();
+ if (!aSource.renameTo(aDestination)) {
+ byte[] buffer = new byte[16384];
+ FileInputStream inputStream = new FileInputStream(aSource);
+ FileOutputStream outputStream = new FileOutputStream(aDestination);
+ try {
+ int count = inputStream.read(buffer);
+ while (count > 0) {
+ outputStream.write(buffer, 0, count);
+ count = inputStream.read(buffer);
+ }
+ }
+ finally {
+ outputStream.close();
+ inputStream.close();
+ }
+ aSource.delete();
+ }
+ }
+
+ public static class RegExpFileFilter implements FilenameFilter {
+ private RE expression;
+
+ public RegExpFileFilter(String anExpression) {
+ try {
+ expression = new RE(anExpression);
+ }
+ catch (Throwable t) {
+ throw new RuntimeException(t.getMessage());
+ }
+ }
+
+ public boolean accept(File aDir, String aName) {
+ return expression.isMatch(aName) && !new File(aDir, aName).isDirectory();
+ }
+ }
+
+ public static class DirectoryFilter implements FilenameFilter {
+ public DirectoryFilter() {
+ }
+
+ public boolean accept(File aDir, String aName) {
+ return new File(aDir, aName).isDirectory();
+ }
+ }
+
+ /**
+ * 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>
+ */
+ public static List getDirectoryContentsAsList(File aDirectory, FilenameFilter aFilter) {
+ Object[] contents = aDirectory.list(aFilter);
+
+ if (contents == null) {
+ return Collections.EMPTY_LIST;
+ }
+
+ return Arrays.asList(contents);
+ }
+
+ /**
+ * Return the extension of a path. (e.g. <code>getExtension("example.txt")</code> will
+ * return <code>"txt"</code>
+ */
+ public static String getExtension(String aPath) {
+ int position = aPath.lastIndexOf('.');
+ if (position >= 0) {
+ return aPath.substring(position + 1);
+ }
+ return "";
+ }
+
+ public static boolean isAbsolutePath(String aPath) {
+ return new File(aPath).isAbsolute();
+ }
+
+ /**
+ * Transforms an absolute or relative path into an absolute
+ * {@link File}.
+ *
+ * @param aBasePath The base path to use for relative paths
+ * @param aPath The path to transform
+ * @return An absolute representation of the supplied path
+ */
+ public static File getAbsoluteOrRelativeFile(File aBasePath, String aPath) {
+ if (isAbsolutePath(aPath)) {
+ return new File(aPath);
+ }
+
+ return new File(aBasePath, aPath);
+ }
+
+ /**
+ * Reads the content of a file into an array of bytes
+ */
+ public static byte[] readFileIntoByteArray(File aFile) throws IOException {
+ InputStream input = new FileInputStream(aFile);
+ ByteArrayOutputStream result = new ByteArrayOutputStream();
+
+ IORoutines.copyStream(input, result);
+
+ return result.toByteArray();
+ }
+
+ /**
+ * Reads the content of a file into an array of bytes
+ */
+ public static void writeByteArrayIntoFile(byte[] anArray, File aFile) throws IOException {
+ OutputStream output = new FileOutputStream(aFile);
+
+ try {
+ ByteArrayInputStream input = new ByteArrayInputStream(anArray);
+ try {
+ IORoutines.copyStream(input, output);
+ }
+ finally {
+ input.close();
+ }
+ }
+ finally {
+ output.close();
+ }
+ }
+
+
+
+ /**
+ * 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