new admin templates! with many thanks to init...
[mir.git] / source / mir / util / FileFunctions.java
1 package mir.util;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.FilenameFilter;
8
9 import gnu.regexp.RE;
10
11 public class FileFunctions {
12   protected static final int FILE_COPY_BUFFER_SIZE = 65536;
13
14   private FileFunctions() {
15   }
16
17   public static void copyFile(File aSourceFile, File aDestinationFile) throws IOException {
18     FileInputStream inputStream;
19     FileOutputStream outputStream;
20     int nrBytesRead;
21     byte[] buffer = new byte[FILE_COPY_BUFFER_SIZE];
22
23     inputStream = new FileInputStream(aSourceFile);
24     try {
25       File directory = new File(aDestinationFile.getParent());
26         if (directory!=null && !directory.exists()){
27           directory.mkdirs();
28       }
29       outputStream = new FileOutputStream(aDestinationFile);
30       try {
31         do {
32           nrBytesRead = inputStream.read(buffer);
33           if (nrBytesRead>0)
34             outputStream.write(buffer, 0, nrBytesRead);
35         }
36         while (nrBytesRead>=0);
37       }
38       finally {
39         outputStream.close();
40       }
41     }
42     finally {
43       inputStream.close();
44     }
45   }
46
47   public static void copyDirectory(File aSourceDirectory, File aDestinationDirectory) throws IOException {
48     int i;
49     File sourceFile;
50     File destinationFile;
51     File[] files = aSourceDirectory.listFiles();
52
53     if (!aDestinationDirectory.exists())
54       aDestinationDirectory.mkdirs();
55
56     for (i=0; i<files.length; i++) {
57       sourceFile = files[i];
58       destinationFile=new File(aDestinationDirectory, sourceFile.getName());
59       if (sourceFile.isDirectory()) {
60         if (!destinationFile.exists())
61           destinationFile.mkdir();
62         copyDirectory(sourceFile, destinationFile);
63       }
64       else {
65         copyFile(sourceFile, destinationFile);
66       }
67     }
68   }
69
70   public static void copy(File aSource, File aDestination) throws IOException {
71     if (aSource.isDirectory()) {
72       copyDirectory(aSource, aDestination);
73     }
74     else if (aDestination.isDirectory()) {
75       copyFile(aSource, new File(aDestination, aSource.getName()));
76     }
77     else {
78       copyFile(aSource, aDestination);
79     }
80   }
81
82   public static class RegExpFileFilter implements FilenameFilter {
83     private RE expression;
84
85     public RegExpFileFilter(String anExpression) {
86       try {
87         expression = new RE(anExpression);
88       }
89       catch (Throwable t) {
90         throw new RuntimeException(t.getMessage());
91       }
92     }
93
94     public boolean accept(File aDir, String aName) {
95       return expression.isMatch(aName) && !new File(aDir, aName).isDirectory();
96     }
97   }
98
99   public static class DirectoryFilter implements FilenameFilter {
100     public DirectoryFilter() {
101     }
102
103     public boolean accept(File aDir, String aName) {
104       return new File(aDir, aName).isDirectory();
105     }
106
107   }
108
109 }