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