a49c1498b4e6f7c4f1cb7eabb4c36358dfc32b12
[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         int count=inputStream.read(buffer);
148         while (count>0) {
149           outputStream.write(buffer, 0, count);
150           count=inputStream.read(buffer);
151         }
152       }
153       finally {
154         outputStream.close();
155         inputStream.close();
156       }
157       aSource.delete();
158     }
159   }
160
161   /**
162    * Reads the content of a file into a string
163    */
164   public static String readFileIntoString(String fileName) 
165    throws IOException
166   {
167     return new String(readFileIntoByteArray(fileName));
168   }
169   /**
170    * Reads the content of a file into an array of bytes
171    */
172   public static byte[] readFileIntoByteArray(String fileName) 
173     throws IOException
174   {
175     FileInputStream input = new FileInputStream(fileName);
176     int size= input.available();
177     byte result[]= new byte[size];
178     input.read(result);
179     return result;
180   }
181
182   public static class RegExpFileFilter implements FilenameFilter {
183     private RE expression;
184
185     public RegExpFileFilter(String anExpression) {
186       try {
187         expression = new RE(anExpression);
188       }
189       catch (Throwable t) {
190         throw new RuntimeException(t.getMessage());
191       }
192     }
193
194     public boolean accept(File aDir, String aName) {
195       return expression.isMatch(aName) && !new File(aDir, aName).isDirectory();
196     }
197   }
198
199   public static class DirectoryFilter implements FilenameFilter {
200     public DirectoryFilter() {
201     }
202
203     public boolean accept(File aDir, String aName) {
204       return new File(aDir, aName).isDirectory();
205     }
206   }
207
208   public static List getDirectoryContentsAsList(File aDirectory, FilenameFilter aFilter) {
209     Object[] contents = aDirectory.list(aFilter);
210     if (contents==null)
211       return Collections.EMPTY_LIST;
212                 return Arrays.asList(contents);
213   }
214
215   public static String getExtension(String aPath) {
216     int position = aPath.lastIndexOf('.');
217     if (position>=0) {
218       return aPath.substring(position+1);
219     }
220                 return "";
221   }
222
223   public static boolean isAbsolutePath(String aPath) {
224     return new File(aPath).isAbsolute();
225   }
226
227   public static File getAbsoluteOrRelativeFile(File aParentIfRelative, String aPath) {
228     if (isAbsolutePath(aPath)) {
229       return new File(aPath);
230     }
231                 return new File(aParentIfRelative, aPath);
232   }
233 }