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.
34 import java.io.BufferedInputStream;
35 import java.io.BufferedOutputStream;
37 import java.io.FileInputStream;
38 import java.io.FileOutputStream;
39 import java.io.FilenameFilter;
40 import java.io.IOException;
41 import java.io.InputStream;
42 import java.util.Arrays;
43 import java.util.Collections;
44 import java.util.List;
46 public class FileRoutines {
47 protected static final int FILE_COPY_BUFFER_SIZE = 65536;
49 protected FileRoutines() {
52 public static void copyFile(File aSourceFile, File aDestinationFile) throws IOException {
53 BufferedInputStream inputStream;
54 BufferedOutputStream outputStream;
56 byte[] buffer = new byte[FILE_COPY_BUFFER_SIZE];
58 inputStream = new BufferedInputStream(
59 new FileInputStream(aSourceFile));
61 File directory = new File(aDestinationFile.getParent());
62 if (directory!=null && !directory.exists()){
65 outputStream = new BufferedOutputStream(
66 new FileOutputStream(aDestinationFile),8192);
69 nrBytesRead = inputStream.read(buffer);
71 outputStream.write(buffer, 0, nrBytesRead);
73 while (nrBytesRead>=0);
84 public static void copyDirectory(File aSourceDirectory, File aDestinationDirectory) throws IOException {
88 File[] files = aSourceDirectory.listFiles();
90 if (!aDestinationDirectory.exists())
91 aDestinationDirectory.mkdirs();
93 for (i=0; i<files.length; i++) {
94 sourceFile = files[i];
95 destinationFile=new File(aDestinationDirectory, sourceFile.getName());
96 if (sourceFile.isDirectory()) {
97 if (!destinationFile.exists())
98 destinationFile.mkdir();
99 copyDirectory(sourceFile, destinationFile);
102 copyFile(sourceFile, destinationFile);
107 public static void copy(File aSource, File aDestination) throws IOException {
108 if (aSource.isDirectory()) {
109 copyDirectory(aSource, aDestination);
111 else if (aDestination.isDirectory()) {
112 copyFile(aSource, new File(aDestination, aSource.getName()));
115 copyFile(aSource, aDestination);
120 * Copy the contents of an {@link InputStream} to a {@link File}
122 public static void copy(InputStream aSource, File aDestination) throws IOException {
123 BufferedOutputStream outputStream =
124 new BufferedOutputStream(new FileOutputStream(aDestination), 8192);
127 byte[] buf = new byte[8 * 1024];
129 while ((read = aSource.read(buf)) != -1) {
130 outputStream.write(buf, 0, read);
134 outputStream.close();
138 * Moves a {@link File} to a new location
140 public static void move(File aSource, File aDestination) throws IOException {
141 aDestination.getParentFile().mkdirs();
142 if (!aSource.renameTo(aDestination)) {
143 byte[] buffer = new byte[16384];
144 FileInputStream inputStream = new FileInputStream(aSource);
145 FileOutputStream outputStream = new FileOutputStream(aDestination);
147 int count=inputStream.read(buffer);
149 outputStream.write(buffer, 0, count);
150 count=inputStream.read(buffer);
154 outputStream.close();
161 public static class RegExpFileFilter implements FilenameFilter {
162 private RE expression;
164 public RegExpFileFilter(String anExpression) {
166 expression = new RE(anExpression);
168 catch (Throwable t) {
169 throw new RuntimeException(t.getMessage());
173 public boolean accept(File aDir, String aName) {
174 return expression.isMatch(aName) && !new File(aDir, aName).isDirectory();
178 public static class DirectoryFilter implements FilenameFilter {
179 public DirectoryFilter() {
182 public boolean accept(File aDir, String aName) {
183 return new File(aDir, aName).isDirectory();
188 * Return all files in a directory
190 * @param aDirectory The directory to list
191 * @param aFilter the filter to apply to files
192 * @return a <code>List</code> of filenames of type <code>String</code>
194 public static List getDirectoryContentsAsList(File aDirectory, FilenameFilter aFilter) {
195 Object[] contents = aDirectory.list(aFilter);
197 if (contents==null) {
198 return Collections.EMPTY_LIST;
201 return Arrays.asList(contents);
205 * Return the extension of a path. (e.g. <code>getExtension("example.txt")</code> will
206 * return <code>"txt"</code>
208 public static String getExtension(String aPath) {
209 int position = aPath.lastIndexOf('.');
211 return aPath.substring(position+1);
216 public static boolean isAbsolutePath(String aPath) {
217 return new File(aPath).isAbsolute();
221 * Transforms an absolute or relative path into an absolute
224 * @param aBasePath The base path to use for relative paths
225 * @param aPath The path to transform
226 * @return An absolute representation of the supplied path
228 public static File getAbsoluteOrRelativeFile(File aBasePath, String aPath) {
229 if (isAbsolutePath(aPath)) {
230 return new File(aPath);
233 return new File(aBasePath, aPath);
237 * Reads the content of a file into a string
239 * TODO: The encoding to be used has not been taken into account: the routine now
240 * assumes the file is encoded according to the JVM/platform default
242 public static String readFileIntoString(String fileName) throws IOException {
243 return new String(readFileIntoByteArray(fileName));
247 * Reads the content of a file into an array of bytes
249 * TODO: add a loop: {@link java.io.InputStream#available()} is not
250 * the total number of bytes left in the stream, but depends on buffering
253 public static byte[] readFileIntoByteArray(String fileName) throws IOException {
254 InputStream input = new FileInputStream(fileName);
255 int size= input.available();
256 byte result[] = new byte[size];
263 * Creates all parent directories of a file if they do not exist
265 public static void createParentDirectories(File aFile) {
266 if (aFile.getParentFile()!=null && !aFile.getParentFile().exists()) {
267 aFile.getParentFile().mkdirs();