1 18 package org.apache.geronimo.interop.util; 19 20 import java.io.BufferedInputStream ; 21 import java.io.BufferedOutputStream ; 22 import java.io.BufferedReader ; 23 import java.io.ByteArrayOutputStream ; 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.FileOutputStream ; 27 import java.io.FileReader ; 28 import java.io.FilenameFilter ; 29 import java.io.IOException ; 30 import java.io.InputStream ; 31 import java.io.OutputStream ; 32 import java.util.Iterator ; 33 import java.util.LinkedList ; 34 import java.util.List ; 35 36 import org.apache.geronimo.interop.SystemException; 37 import org.apache.geronimo.interop.properties.SystemProperties; 38 39 40 public abstract class FileUtil { 41 43 private static int _tempIndex; 44 45 private static Object _tempIndexLock = new Object (); 46 47 49 public static int compareLines(String file1, String file2) { 50 return compareLines(file1, file2, false); 51 } 52 53 public static int compareLines(String file1, String file2, boolean removeTopLevelComments) { 54 String lines1 = readLines(file1, removeTopLevelComments); 55 String lines2 = readLines(file2, removeTopLevelComments); 56 return lines1.compareTo(lines2); 57 } 58 59 public static void copyDir(String fromDir, String toDir) { 60 copyDir(fromDir, toDir, true); 61 } 62 63 public static void copyDir(String fromDir, String toDir, boolean rec) { 64 File dirFile = new File (fromDir); 65 if (!dirFile.exists()) { 66 return; 67 } 68 69 File toDirFile = new File (toDir); 70 if (!toDirFile.exists()) { 71 toDirFile.mkdir(); 72 } 73 String [] fileList = dirFile.list(); 74 if (fileList != null) { 75 for (int i = 0; i < fileList.length; i++) { 76 String name = fileList[i]; 77 String from = fromDir + File.separator + name; 78 String to = toDir + File.separatorChar + name; 79 File file = new File (from); 80 if (file.isDirectory()) { 81 if (rec) { 82 copyDir(from, to); 83 } 84 } else { 85 copyFile(from, to); 86 } 87 } 88 } 89 } 90 91 public static void copyFile(String from, String to) { 92 mkdirs(to); 93 try { 94 InputStream input = new BufferedInputStream (new FileInputStream (from)); 95 OutputStream output = new BufferedOutputStream (new FileOutputStream (to)); 96 int c; 97 while ((c = input.read()) != -1) { 98 output.write(c); 99 } 100 input.close(); 101 output.close(); 102 } catch (IOException ex) { 103 throw new SystemException(ex); 104 } 105 } 106 107 public static void copyFiles(String fromDir, String toDir, List files) { 108 for (Iterator i = files.iterator(); i.hasNext();) { 109 String file = (String ) i.next(); 110 copyFile(fromDir + "/" + file, toDir + "/" + file); 111 } 112 } 113 114 public static void deleteDir(String dir) { 115 File dirFile = new File (dir); 116 if (dirFile.exists()) { 117 deleteFilesInDir(dir); 118 dirFile.delete(); 119 } 120 } 121 122 public static void deleteFile(String file) { 123 new File (file).delete(); 124 } 125 126 public static void deleteFiles(List files) { 127 for (Iterator i = files.iterator(); i.hasNext();) { 128 String fileName = (String ) i.next(); 129 File file = new File (fileName); 130 file.delete(); 131 } 132 } 133 134 public static void deleteFilesInDir(String dir) { 135 File dirFile = new File (dir); 136 String [] fileList = dirFile.list(); 137 if (fileList != null) { 138 for (int i = 0; i < fileList.length; i++) { 139 String path = dir + File.separator + fileList[i]; 140 File file = new File (path); 141 if (file.isDirectory()) { 142 deleteDir(path); 143 } 144 file.delete(); 145 } 146 } 147 } 148 149 public static String expandHomeRelativePath(String path) { 150 if (path.startsWith("~")) { 151 path = SystemProperties.getHome() + path.substring(1); 152 } 153 return path; 154 } 155 156 public static List findFiles(String baseDir) { 157 return findFiles(baseDir, "", true, true, ""); 158 } 159 160 public static List findFiles(String baseDir, String pattern) { 161 return findFiles(baseDir, pattern, true, true, ""); 162 } 163 164 public static List findFiles(String baseDir, String pattern, boolean fullPath, boolean recursive) { 165 return findFiles(baseDir, pattern, fullPath, recursive, ""); 166 } 167 168 private static List findFiles(String baseDir, String pattern, boolean fullPath, boolean recursive, String relativeBase) { 169 if (pattern.equals("**")) { 170 pattern = ""; recursive = true; 172 } 173 final String prefix = StringUtil.beforeFirst("*", pattern); 174 final String suffix = StringUtil.afterFirst("*", pattern); 175 final boolean finalRecursive = recursive; 176 FilenameFilter filter = new FilenameFilter () { 177 public boolean accept(File file, String name) { 178 if (finalRecursive && new File (file.getPath() + File.separator + name).isDirectory()) { 179 return true; 180 } 181 return name.startsWith(prefix) && name.endsWith(suffix); 182 } 183 } 184 ; 185 List list = new LinkedList (); 186 File dirFile = new File (baseDir); 187 String [] files = dirFile.list(filter); 188 if (files != null) { 189 int n = files.length; 190 for (int i = 0; i < n; i++) { 191 String fileName = files[i]; 192 String fullName = baseDir.length() == 0 ? fileName 193 : (baseDir + (fullPath ? File.separatorChar : '/') + fileName); 194 File file = new File (fullName); 195 if (file.isDirectory()) { 196 if (recursive) { 197 String relativeName = relativeBase.length() == 0 ? fileName 198 : (relativeBase + '/' + fileName); 199 list.addAll(findFiles(fullName, pattern, fullPath, 200 recursive, relativeName)); 201 } 202 } else if (fullPath) { 203 list.add(fullName); 204 } else { 205 String relativeName = relativeBase.length() == 0 ? fileName 206 : (relativeBase + '/' + fileName); 207 list.add(relativeName); 208 } 209 } 210 } 211 return list; 212 } 213 214 public static void mkdir(String dir) { 215 try { 216 new File (dir).mkdirs(); 217 } catch (Exception ex) { 218 throw new SystemException(ex); 219 } 220 } 221 222 public static void mkdirs(String file) { 223 try { 224 file = file.replace('/', File.separatorChar); 225 int pos = file.lastIndexOf(File.separatorChar); 226 if (pos != -1) { 227 String dir = file.substring(0, pos); 228 mkdir(dir); 229 } 230 } catch (Exception ex) { 231 throw new SystemException(ex); 232 } 233 } 234 235 public static String newTempDir() { 236 String tempDir = SystemProperties.getTempDir(); 237 synchronized (_tempIndexLock) { 238 tempDir += "/" + (++_tempIndex); 239 } 240 tempDir = pretty(tempDir); 241 deleteFilesInDir(tempDir); 242 mkdirs(tempDir + "/x.x"); 243 return tempDir; 244 } 245 246 public static String pretty(String file) { 247 try { 248 return new File (file).getCanonicalPath(); 249 } catch (Exception ignore) { 250 return file.replace('/', File.separatorChar); 251 } 252 } 253 254 257 public static byte[] readBytes(String fileName) { 258 try { 259 ByteArrayOutputStream bytes = new ByteArrayOutputStream (); 260 InputStream input = new BufferedInputStream (new FileInputStream (fileName)); 261 int c; 262 while ((c = input.read()) != -1) { 263 bytes.write((byte) c); 264 } 265 input.close(); 266 return bytes.toByteArray(); 267 } catch (IOException ex) { 268 throw new SystemException(ex); 269 } 270 } 271 272 public static String readLines(String fileName) { 273 return readLines(fileName, false); 274 } 275 276 279 public static String readLines(String fileName, boolean removeTopLevelComments) { 280 try { 281 StringBuffer code = new StringBuffer (); 282 BufferedReader input = new BufferedReader (new FileReader (fileName)); 283 String line; 284 while ((line = input.readLine()) != null) { 285 if (removeTopLevelComments && line.length() >= 3) { 286 char c1 = line.charAt(1); 287 char c2 = line.charAt(2); 288 if (c1 == '*' && c2 == '*') { 289 continue; 290 } 291 } 292 code.append(line); 293 code.append('\n'); 294 } 295 input.close(); 296 return code.toString(); 297 } catch (IOException ex) { 298 throw new SystemException(ex); 299 } 300 } 301 } 302 | Popular Tags |