rebuilding head
[mir.git] / source / mir / util / FileFunctions.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 java.io.*;
33 import java.util.Arrays;
34 import java.util.Collections;
35 import java.util.List;
36
37 import gnu.regexp.RE;
38
39 public class FileFunctions {
40   protected static final int FILE_COPY_BUFFER_SIZE = 65536;
41
42   private FileFunctions() {
43   }
44
45   public static void copyFile(File aSourceFile, File aDestinationFile) throws IOException {
46     BufferedInputStream inputStream;
47     BufferedOutputStream outputStream;
48     int nrBytesRead;
49     byte[] buffer = new byte[FILE_COPY_BUFFER_SIZE];
50
51     inputStream = new BufferedInputStream(
52       new FileInputStream(aSourceFile));
53     try {
54       File directory = new File(aDestinationFile.getParent());
55         if (directory!=null && !directory.exists()){
56           directory.mkdirs();
57       }
58       outputStream = new BufferedOutputStream(
59         new FileOutputStream(aDestinationFile),8192);
60       try {
61         do {
62           nrBytesRead = inputStream.read(buffer);
63           if (nrBytesRead>0)
64             outputStream.write(buffer, 0, nrBytesRead);
65         }
66         while (nrBytesRead>=0);
67       }
68       finally {
69         outputStream.close();
70       }
71     }
72     finally {
73       inputStream.close();
74     }
75   }
76
77   public static void copyDirectory(File aSourceDirectory, File aDestinationDirectory) throws IOException {
78     int i;
79     File sourceFile;
80     File destinationFile;
81     File[] files = aSourceDirectory.listFiles();
82
83     if (!aDestinationDirectory.exists())
84       aDestinationDirectory.mkdirs();
85
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);
93       }
94       else {
95         copyFile(sourceFile, destinationFile);
96       }
97     }
98   }
99
100   public static void copy(File aSource, File aDestination) throws IOException {
101     if (aSource.isDirectory()) {
102       copyDirectory(aSource, aDestination);
103     }
104     else if (aDestination.isDirectory()) {
105       copyFile(aSource, new File(aDestination, aSource.getName()));
106     }
107     else {
108       copyFile(aSource, aDestination);
109     }
110   }
111
112   /**
113    * Copy the contents of an {@link InputStream} to a {@link File}
114    */
115   public static void copy(InputStream aSource, File aDestination) throws IOException {
116     BufferedOutputStream outputStream =
117         new BufferedOutputStream(new FileOutputStream(aDestination), 8192);
118
119     int read;
120     byte[] buf = new byte[8 * 1024];
121
122     while ((read = aSource.read(buf)) != -1) {
123       outputStream.write(buf, 0, read);
124     }
125
126     aSource.close();
127     outputStream.close();
128   }
129
130   /**
131    * Moves a {@link File} to a new location
132    */
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);
139       try {
140         while (inputStream.read(buffer)>0) {
141           outputStream.write(buffer);
142         }
143       }
144       finally {
145         outputStream.close();
146         inputStream.close();
147       }
148       aSource.delete();
149     };
150   }
151
152   public static class RegExpFileFilter implements FilenameFilter {
153     private RE expression;
154
155     public RegExpFileFilter(String anExpression) {
156       try {
157         expression = new RE(anExpression);
158       }
159       catch (Throwable t) {
160         throw new RuntimeException(t.getMessage());
161       }
162     }
163
164     public boolean accept(File aDir, String aName) {
165       return expression.isMatch(aName) && !new File(aDir, aName).isDirectory();
166     }
167   }
168
169   public static class DirectoryFilter implements FilenameFilter {
170     public DirectoryFilter() {
171     }
172
173     public boolean accept(File aDir, String aName) {
174       return new File(aDir, aName).isDirectory();
175     }
176   }
177
178   public static List getDirectoryContentsAsList(File aDirectory, FilenameFilter aFilter) {
179     Object[] contents = aDirectory.list(aFilter);
180     if (contents==null)
181       return Collections.EMPTY_LIST;
182     else
183       return Arrays.asList(contents);
184   }
185
186   public static String getExtension(String aPath) {
187     int position = aPath.lastIndexOf('.');
188     if (position>=0) {
189       return aPath.substring(position+1);
190     }
191     else {
192       return "";
193     }
194   }
195
196   public static boolean isAbsolutePath(String aPath) {
197     return new File(aPath).isAbsolute();
198   }
199
200   public static File getAbsoluteOrRelativeFile(File aParentIfRelative, String aPath) {
201     if (isAbsolutePath(aPath)) {
202       return new File(aPath);
203     }
204     else {
205       return new File(aParentIfRelative, aPath);
206     }
207   }
208 }