/*
- * Copyright (C) 2001, 2002 The Mir-coders group
+ * Copyright (C) 2005 The Mir-coders group
*
* This file is part of Mir.
*
*/
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);
- }
-}
+/**
+ * @deprecated Use mir.util.FileRoutines instead
+ */
+public class FileFunctions extends FileRoutines {
+}
\ No newline at end of file