2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
6 * Mir is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Mir is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Mir; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * In addition, as a special exception, The Mir-coders gives permission to link
21 * the code of this program with any library licensed under the Apache Software License,
22 * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23 * (or with modified versions of the above that use the same license as the above),
24 * and distribute linked combinations including the two. You must obey the
25 * GNU General Public License in all respects for all of the code used other than
26 * the above mentioned libraries. If you modify this file, you may extend this
27 * exception to your version of the file, but you are not obligated to do so.
28 * If you do not wish to do so, delete this exception statement from your version.
33 import java.util.Arrays;
34 import java.util.Collections;
35 import java.util.List;
39 public class FileFunctions {
40 protected static final int FILE_COPY_BUFFER_SIZE = 65536;
42 private FileFunctions() {
45 public static void copyFile(File aSourceFile, File aDestinationFile) throws IOException {
46 BufferedInputStream inputStream;
47 BufferedOutputStream outputStream;
49 byte[] buffer = new byte[FILE_COPY_BUFFER_SIZE];
51 inputStream = new BufferedInputStream(
52 new FileInputStream(aSourceFile));
54 File directory = new File(aDestinationFile.getParent());
55 if (directory!=null && !directory.exists()){
58 outputStream = new BufferedOutputStream(
59 new FileOutputStream(aDestinationFile),8192);
62 nrBytesRead = inputStream.read(buffer);
64 outputStream.write(buffer, 0, nrBytesRead);
66 while (nrBytesRead>=0);
77 public static void copyDirectory(File aSourceDirectory, File aDestinationDirectory) throws IOException {
81 File[] files = aSourceDirectory.listFiles();
83 if (!aDestinationDirectory.exists())
84 aDestinationDirectory.mkdirs();
86 for (i=0; i<files.length; i++) {
87 sourceFile = files[i];
88 destinationFile=new File(aDestinationDirectory, sourceFile.getName());
89 if (sourceFile.isDirectory()) {
90 if (!destinationFile.exists())
91 destinationFile.mkdir();
92 copyDirectory(sourceFile, destinationFile);
95 copyFile(sourceFile, destinationFile);
100 public static void copy(File aSource, File aDestination) throws IOException {
101 if (aSource.isDirectory()) {
102 copyDirectory(aSource, aDestination);
104 else if (aDestination.isDirectory()) {
105 copyFile(aSource, new File(aDestination, aSource.getName()));
108 copyFile(aSource, aDestination);
113 * Copy the contents of an {@link InputStream} to a {@link File}
115 public static void copy(InputStream aSource, File aDestination) throws IOException {
116 BufferedOutputStream outputStream =
117 new BufferedOutputStream(new FileOutputStream(aDestination), 8192);
120 byte[] buf = new byte[8 * 1024];
122 while ((read = aSource.read(buf)) != -1) {
123 outputStream.write(buf, 0, read);
127 outputStream.close();
131 * Moves a {@link File} to a new location
133 public static void move(File aSource, File aDestination) throws IOException {
134 aDestination.getParentFile().mkdirs();
135 if (!aSource.renameTo(aDestination)) {
136 byte[] buffer = new byte[16384];
137 FileInputStream inputStream = new FileInputStream(aSource);
138 FileOutputStream outputStream = new FileOutputStream(aDestination);
140 while (inputStream.read(buffer)>0) {
141 outputStream.write(buffer);
145 outputStream.close();
152 public static class RegExpFileFilter implements FilenameFilter {
153 private RE expression;
155 public RegExpFileFilter(String anExpression) {
157 expression = new RE(anExpression);
159 catch (Throwable t) {
160 throw new RuntimeException(t.getMessage());
164 public boolean accept(File aDir, String aName) {
165 return expression.isMatch(aName) && !new File(aDir, aName).isDirectory();
169 public static class DirectoryFilter implements FilenameFilter {
170 public DirectoryFilter() {
173 public boolean accept(File aDir, String aName) {
174 return new File(aDir, aName).isDirectory();
178 public static List getDirectoryContentsAsList(File aDirectory, FilenameFilter aFilter) {
179 Object[] contents = aDirectory.list(aFilter);
181 return Collections.EMPTY_LIST;
183 return Arrays.asList(contents);
186 public static String getExtension(String aPath) {
187 int position = aPath.lastIndexOf('.');
189 return aPath.substring(position+1);
196 public static boolean isAbsolutePath(String aPath) {
197 return new File(aPath).isAbsolute();
200 public static File getAbsoluteOrRelativeFile(File aParentIfRelative, String aPath) {
201 if (isAbsolutePath(aPath)) {
202 return new File(aPath);
205 return new File(aParentIfRelative, aPath);