-package mir.util;\r
-\r
-import java.io.File;\r
-import java.io.FileInputStream;\r
-import java.io.FileOutputStream;\r
-import java.io.IOException;\r
-import java.io.FilenameFilter;\r
-\r
-import gnu.regexp.RE;\r
-\r
-public class FileFunctions {\r
- protected static final int FILE_COPY_BUFFER_SIZE = 65536;\r
-\r
- private FileFunctions() {\r
- }\r
-\r
- public static void copyFile(File aSourceFile, File aDestinationFile) throws IOException {\r
- FileInputStream inputStream;\r
- FileOutputStream outputStream;\r
- int nrBytesRead;\r
- byte[] buffer = new byte[FILE_COPY_BUFFER_SIZE];\r
-\r
- inputStream = new FileInputStream(aSourceFile);\r
- try {\r
- File directory = new File(aDestinationFile.getParent());\r
- if (directory!=null && !directory.exists()){\r
- directory.mkdirs();\r
- }\r
- outputStream = new FileOutputStream(aDestinationFile);\r
- try {\r
- do {\r
- nrBytesRead = inputStream.read(buffer);\r
- if (nrBytesRead>0)\r
- outputStream.write(buffer, 0, nrBytesRead);\r
- }\r
- while (nrBytesRead>=0);\r
- }\r
- finally {\r
- outputStream.close();\r
- }\r
- }\r
- finally {\r
- inputStream.close();\r
- }\r
- }\r
-\r
- public static void copyDirectory(File aSourceDirectory, File aDestinationDirectory) throws IOException {\r
- int i;\r
- File sourceFile;\r
- File destinationFile;\r
- File[] files = aSourceDirectory.listFiles();\r
-\r
- if (!aDestinationDirectory.exists())\r
- aDestinationDirectory.mkdirs();\r
-\r
- for (i=0; i<files.length; i++) {\r
- sourceFile = files[i];\r
- destinationFile=new File(aDestinationDirectory, sourceFile.getName());\r
- if (sourceFile.isDirectory()) {\r
- if (!destinationFile.exists())\r
- destinationFile.mkdir();\r
- copyDirectory(sourceFile, destinationFile);\r
- }\r
- else {\r
- copyFile(sourceFile, destinationFile);\r
- }\r
- }\r
- }\r
-\r
- public static void copy(File aSource, File aDestination) throws IOException {\r
- if (aSource.isDirectory()) {\r
- copyDirectory(aSource, aDestination);\r
- }\r
- else if (aDestination.isDirectory()) {\r
- copyFile(aSource, new File(aDestination, aSource.getName()));\r
- }\r
- else {\r
- copyFile(aSource, aDestination);\r
- }\r
- }\r
-\r
- public static class RegExpFileFilter implements FilenameFilter {\r
- private RE expression;\r
-\r
- public RegExpFileFilter(String anExpression) {\r
- try {\r
- expression = new RE(anExpression);\r
- }\r
- catch (Throwable t) {\r
- throw new RuntimeException(t.getMessage());\r
- }\r
- }\r
-\r
- public boolean accept(File aDir, String aName) {\r
- return expression.isMatch(aName) && !new File(aDir, aName).isDirectory();\r
- }\r
- }\r
-\r
- public static class DirectoryFilter implements FilenameFilter {\r
- public DirectoryFilter() {\r
- }\r
-\r
- public boolean accept(File aDir, String aName) {\r
- return new File(aDir, aName).isDirectory();\r
- }\r
-\r
- }\r
-\r
-}
\ No newline at end of file
+/*
+ * Copyright (C) 2001, 2002 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,
+ * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
+ * (or with modified versions of the above that use the same license as the above),
+ * 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.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.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+public class FileFunctions {
+ protected static final int FILE_COPY_BUFFER_SIZE = 65536;
+
+ private FileFunctions() {
+ }
+
+ 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();
+ }
+ }
+
+ 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);
+ }
+ }
+ }
+
+ 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();
+ }
+ }
+
+ /**
+ * Reads the content of a file into a string
+ */
+ 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
+ {
+ FileInputStream input = new FileInputStream(fileName);
+ int size= input.available();
+ byte result[]= new byte[size];
+ input.read(result);
+ return result;
+ }
+
+ 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();
+ }
+ }
+
+ public static List getDirectoryContentsAsList(File aDirectory, FilenameFilter aFilter) {
+ Object[] contents = aDirectory.list(aFilter);
+ if (contents==null)
+ return Collections.EMPTY_LIST;
+ return Arrays.asList(contents);
+ }
+
+ 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();
+ }
+
+ public static File getAbsoluteOrRelativeFile(File aParentIfRelative, String aPath) {
+ if (isAbsolutePath(aPath)) {
+ return new File(aPath);
+ }
+ return new File(aParentIfRelative, aPath);
+ }
+}