32a964a4fad75765891c08185e254909e3d08627
[mir.git] / source / mir / util / FileRoutines.java
1 /*
2  * Copyright (C) 2001, 2002 The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
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.
10  *
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.
15  *
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
19  *
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.
29  */
30 package mir.util;
31
32 import gnu.regexp.RE;
33
34 import java.io.*;
35 import java.util.Arrays;
36 import java.util.Collections;
37 import java.util.List;
38
39 public class FileRoutines {
40   protected static final int FILE_COPY_BUFFER_SIZE = 65536;
41
42   protected FileRoutines() {
43   }
44
45   /**
46    * Copy a file
47    */
48   public static void copyFile(File aSourceFile, File aDestinationFile) throws IOException {
49     BufferedInputStream inputStream;
50     BufferedOutputStream outputStream;
51     int nrBytesRead;
52     byte[] buffer = new byte[FILE_COPY_BUFFER_SIZE];
53
54     inputStream = new BufferedInputStream(new FileInputStream(aSourceFile));
55     try {
56       File directory = new File(aDestinationFile.getParent());
57       if (directory != null && !directory.exists()) {
58         directory.mkdirs();
59       }
60       outputStream = new BufferedOutputStream(new FileOutputStream(aDestinationFile), 8192);
61       try {
62         do {
63           nrBytesRead = inputStream.read(buffer);
64           if (nrBytesRead > 0)
65             outputStream.write(buffer, 0, nrBytesRead);
66         }
67         while (nrBytesRead >= 0);
68       }
69       finally {
70         outputStream.close();
71       }
72     }
73     finally {
74       inputStream.close();
75     }
76   }
77
78   /**
79    * Copy a directory recursively
80    */
81   public static void copyDirectory(File aSourceDirectory, File aDestinationDirectory) throws IOException {
82     int i;
83     File sourceFile;
84     File destinationFile;
85     File[] files = aSourceDirectory.listFiles();
86
87     if (!aDestinationDirectory.exists())
88       aDestinationDirectory.mkdirs();
89
90     for (i = 0; i < files.length; i++) {
91       sourceFile = files[i];
92       destinationFile = new File(aDestinationDirectory, sourceFile.getName());
93       if (sourceFile.isDirectory()) {
94         if (!destinationFile.exists())
95           destinationFile.mkdir();
96         copyDirectory(sourceFile, destinationFile);
97       }
98       else {
99         copyFile(sourceFile, destinationFile);
100       }
101     }
102   }
103
104   /**
105    * Copy a file or directory. If the source is a file and the destination
106    * a directory, the file is copied using the same file name into the\
107    * directory.
108    *
109    * @param aSource the source file
110    * @param aDestination the destination file
111    */
112   public static void copy(File aSource, File aDestination) throws IOException {
113     if (aSource.isDirectory()) {
114       copyDirectory(aSource, aDestination);
115     }
116     else if (aDestination.isDirectory()) {
117       copyFile(aSource, new File(aDestination, aSource.getName()));
118     }
119     else {
120       copyFile(aSource, aDestination);
121     }
122   }
123
124   /**
125    * Copy the contents of an {@link InputStream} to a {@link File}
126    */
127   public static void copy(InputStream aSource, File aDestination) throws IOException {
128     BufferedOutputStream outputStream =
129         new BufferedOutputStream(new FileOutputStream(aDestination), 8192);
130
131     int read;
132     byte[] buf = new byte[8 * 1024];
133
134     while ((read = aSource.read(buf)) != -1) {
135       outputStream.write(buf, 0, read);
136     }
137
138     aSource.close();
139     outputStream.close();
140   }
141
142   /**
143    * Moves a {@link File} to a new location
144    */
145   public static void move(File aSource, File aDestination) throws IOException {
146     aDestination.getParentFile().mkdirs();
147     if (!aSource.renameTo(aDestination)) {
148       byte[] buffer = new byte[16384];
149       FileInputStream inputStream = new FileInputStream(aSource);
150       FileOutputStream outputStream = new FileOutputStream(aDestination);
151       try {
152         int count = inputStream.read(buffer);
153         while (count > 0) {
154           outputStream.write(buffer, 0, count);
155           count = inputStream.read(buffer);
156         }
157       }
158       finally {
159         outputStream.close();
160         inputStream.close();
161       }
162       aSource.delete();
163     }
164   }
165
166   public static class RegExpFileFilter implements FilenameFilter {
167     private RE expression;
168
169     public RegExpFileFilter(String anExpression) {
170       try {
171         expression = new RE(anExpression);
172       }
173       catch (Throwable t) {
174         throw new RuntimeException(t.getMessage());
175       }
176     }
177
178     public boolean accept(File aDir, String aName) {
179       return expression.isMatch(aName) && !new File(aDir, aName).isDirectory();
180     }
181   }
182
183   public static class DirectoryFilter implements FilenameFilter {
184     public DirectoryFilter() {
185     }
186
187     public boolean accept(File aDir, String aName) {
188       return new File(aDir, aName).isDirectory();
189     }
190   }
191
192   /**
193    * Return all files in a directory
194    *
195    * @param aDirectory The directory to list
196    * @param aFilter    the filter to apply to files
197    * @return a <code>List</code> of filenames of type <code>String</code>
198    */
199   public static List getDirectoryContentsAsList(File aDirectory, FilenameFilter aFilter) {
200     Object[] contents = aDirectory.list(aFilter);
201
202     if (contents == null) {
203       return Collections.EMPTY_LIST;
204     }
205
206     return Arrays.asList(contents);
207   }
208
209   /**
210    * Return the extension of a path. (e.g. <code>getExtension("example.txt")</code> will
211    * return <code>"txt"</code>
212    */
213   public static String getExtension(String aPath) {
214     int position = aPath.lastIndexOf('.');
215     if (position >= 0) {
216       return aPath.substring(position + 1);
217     }
218     return "";
219   }
220
221   public static boolean isAbsolutePath(String aPath) {
222     return new File(aPath).isAbsolute();
223   }
224
225   /**
226    * Transforms an absolute or relative path into an absolute
227    * {@link File}.
228    *
229    * @param aBasePath The base path to use for relative paths
230    * @param aPath     The path to transform
231    * @return An absolute representation of the supplied path
232    */
233   public static File getAbsoluteOrRelativeFile(File aBasePath, String aPath) {
234     if (isAbsolutePath(aPath)) {
235       return new File(aPath);
236     }
237
238     return new File(aBasePath, aPath);
239   }
240
241   /**
242    * Reads the content of a file into an array of bytes
243    */
244   public static byte[] readFileIntoByteArray(String fileName) throws IOException {
245     InputStream input = new FileInputStream(fileName);
246     ByteArrayOutputStream result = new ByteArrayOutputStream();
247
248     IORoutines.copyStream(input, result);
249
250     return result.toByteArray();
251   }
252
253   /**
254    * Creates all parent directories of a file if they do not exist
255    */
256   public static void createParentDirectories(File aFile) {
257     if (aFile.getParentFile() != null && !aFile.getParentFile().exists()) {
258       aFile.getParentFile().mkdirs();
259     }
260   }
261 }