1 2 24 package org.enhydra.tool.common; 25 26 import java.io.BufferedInputStream ; 28 import java.io.BufferedOutputStream ; 29 import java.io.IOException ; 30 import java.io.File ; 31 import java.io.FileFilter ; 32 import java.io.FileInputStream ; 33 import java.io.FileNotFoundException ; 34 import java.io.FileOutputStream ; 35 import java.io.InputStream ; 36 import java.io.OutputStream ; 37 import java.net.URL ; 38 import java.net.URLClassLoader ; 39 import java.util.ArrayList ; 40 import java.util.ResourceBundle ; 41 42 public class FileUtil { 44 static ResourceBundle res = 45 ResourceBundle.getBundle("org.enhydra.tool.common.Res"); 47 public static void copy(InputStream source, 48 OutputStream dest) throws IOException { 49 BufferedInputStream in = null; 50 BufferedOutputStream out = null; 51 int read = 1; 52 int readSum = 0; 53 int available = 1; 54 int sleepCount = 0; 55 56 if (source instanceof BufferedInputStream ) { 57 in = (BufferedInputStream ) source; 58 } else { 59 in = new BufferedInputStream (source); 60 } 61 if (dest instanceof BufferedOutputStream ) { 62 out = (BufferedOutputStream ) dest; 63 } else { 64 out = new BufferedOutputStream (dest); 65 } 66 while (true) { 67 byte[] bytes = new byte[0]; 68 69 available = in.available(); 70 if (available < 1) { 71 out.flush(); 72 if ((read < bytes.length) && (readSum > 0)) { 73 break; 74 } else if (sleepCount > 10) { 75 break; 76 } else { 77 FileUtil.sleep(100); 78 sleepCount++; 79 available = in.available(); 80 } 81 } 82 if (available > 2048) { 83 bytes = new byte[2048]; 84 } else if (available > 1024) { 85 bytes = new byte[1024]; 86 } else if (available > 256) { 87 bytes = new byte[256]; 88 } else if (available > 16) { 89 bytes = new byte[16]; 90 } else { 91 bytes = new byte[1]; 92 } 93 read = in.read(bytes, 0, bytes.length); 94 if (read > 0) { 95 readSum += read; 96 out.write(bytes, 0, read); 97 } 98 } 99 out.flush(); 100 } 101 102 public static File copy(InputStream source, 104 File dest) throws ToolException { 105 FileOutputStream out = null; 106 107 try { 108 out = new FileOutputStream (dest); 109 FileUtil.copy(source, out); 110 source.close(); 111 out.flush(); 112 out.close(); 113 } catch (FileNotFoundException e) { 114 String mess = new String (); 115 116 mess = ResUtil.format(res.getString("Unable_to_create_0_"), dest); 117 throw new ToolException(e, mess); 118 } catch (IOException e) { 119 String mess = new String (); 120 121 mess = ResUtil.format(res.getString("Unable_to_create_0_"), dest); 122 throw new ToolException(e, mess); 123 } 124 return dest; 125 } 126 127 public static File copy(Template source, File dest) throws ToolException { 128 File parentFile = null; 129 InputStream in = null; 130 131 if (source.isDirectory()) { 132 if (dest.isDirectory()) { 133 return dest; 134 } else if (dest.exists()) { 135 String mess = new String (); 136 137 mess = ResUtil.format(res.getString("Unable_to_copy"), 138 source.toString(), dest); 139 throw new ToolException(mess); 140 } else { 141 dest = source.getOutput(); 142 dest.mkdirs(); 143 return dest; 144 } 145 } 146 if (source.isFile() && dest.isDirectory()) { 147 parentFile = new File (dest.getAbsolutePath()); 148 dest = source.getOutput(); 149 } else { 150 parentFile = new File (dest.getParent()); 151 } 152 parentFile.mkdirs(); 153 try { 154 in = source.getInputStream(); 155 dest = FileUtil.copy(in, dest); 156 } catch (IOException e) { 157 e.printStackTrace(System.err); 158 String mess = new String (); 159 160 mess = ResUtil.format(res.getString("Unable_to_copy_file"), 161 source.toString(), dest); 162 throw new ToolException(e, mess); 163 } catch (ToolException e) { 164 e.printStackTrace(System.err); 165 String mess = new String (); 166 167 mess = ResUtil.format(res.getString("Unable_to_copy_file"), 168 source.toString(), dest); 169 throw new ToolException(e, mess); 170 } 171 return dest; 172 } 173 174 public static File copy(File source, File dest) throws ToolException { 175 File parentFile = null; 176 FileInputStream in = null; 177 178 if (source.isDirectory()) { 179 if (dest.isDirectory()) { 180 return dest; 181 } else if (dest.exists()) { 182 String mess = new String (); 183 184 mess = ResUtil.format(res.getString("Unable_to_copy"), 185 source, dest); 186 throw new ToolException(mess); 187 } else { 188 dest = new File (dest, source.getName()); 189 dest.mkdirs(); 190 return dest; 191 } 192 } 193 if (source.isFile() && dest.isDirectory()) { 194 parentFile = new File (dest.getAbsolutePath()); 195 dest = new File (dest, source.getName()); 196 } else { 197 parentFile = new File (dest.getParent()); 198 } 199 parentFile.mkdirs(); 200 try { 201 in = new FileInputStream (source); 202 dest = FileUtil.copy(in, dest); 203 } catch (FileNotFoundException e) { 204 String mess = new String (); 205 206 mess = ResUtil.format(res.getString("Unable_to_copy_file"), 207 source, dest); 208 throw new ToolException(e, mess); 209 } catch (ToolException e) { 210 String mess = new String (); 211 212 mess = ResUtil.format(res.getString("Unable_to_copy_file"), 213 source, dest); 214 throw new ToolException(e, mess); 215 } 216 return dest; 217 } 218 219 public static boolean isDirectory(String path) { 220 boolean dir = false; 221 222 if (path != null) { 223 File file = new File (path); 224 225 dir = file.isDirectory(); 226 } 227 return dir; 228 } 229 230 public static boolean isFile(String path) { 231 boolean file = false; 232 233 if (path != null) { 234 File f = new File (path); 235 236 file = f.isFile(); 237 } 238 return file; 239 } 240 241 public static String toCanonicalPath(final String in) { 242 final String DOT = new String () + '.'; 243 String current = FileUtil.toCurrentPath(in); 244 String out = new String (current); 245 int index = -1; 246 247 index = in.indexOf(DOT + DOT); 248 if (index < 0) { 249 index = current.indexOf(File.separator + '.'); 250 } 251 if (index < 0) { 252 index = current.indexOf('.' + File.separator); 253 } 254 if ((index > -1) || in.startsWith(DOT) || in.endsWith(DOT)) { 255 File file = new File (current); 256 257 try { 258 out = file.getCanonicalPath(); 259 } catch (Exception e) { 260 out = current; 261 e.printStackTrace(); 262 } 263 } 264 return out; 265 } 266 267 public static String toJavaPath(final String in) { 268 String path = new String (in); 269 270 path = FileUtil.toCurrentPath(path); 271 return path.replace('\\', '/'); 272 } 273 274 public static String toCurrentPath(String path) { 275 String cPath = path; 276 File file; 277 278 if (File.separatorChar == '/') { 279 cPath = FileUtil.toShellPath(cPath); 280 } else { 281 cPath = FileUtil.toWindowsPath(cPath); 282 } 283 file = new File (cPath); 284 285 file = new File (file.getAbsolutePath()); 287 if (file.exists()) { 288 cPath = file.getAbsolutePath(); 289 } 290 return cPath.trim(); 291 } 292 293 306 public static String toShellPath(String inPath) { 307 StringBuffer path = new StringBuffer (); 308 int index = -1; 309 310 inPath = inPath.trim(); 311 index = inPath.indexOf(":\\"); inPath = inPath.replace('\\', '/'); 313 if (index > -1) { 314 path.append("//"); path.append(inPath.substring(0, index)); 316 path.append('/'); 317 path.append(inPath.substring(index + 2)); 318 } else { 319 path.append(inPath); 320 } 321 return path.toString(); 322 } 323 324 336 public static String toWindowsPath(String path) { 337 String winPath = path; 338 int index = winPath.indexOf("//"); 340 if (index > -1) { 341 winPath = winPath.substring(0, index) + ":\\" + winPath.substring(index + 2); 343 } 344 index = winPath.indexOf(':'); 345 if (index == 1) { 346 winPath = winPath.substring(0, 1).toUpperCase() 347 + winPath.substring(1); 348 } 349 winPath = winPath.replace('/', '\\'); 350 return winPath; 351 } 352 353 public static File findFirst(FileFilter filter, String path) { 354 return FileUtil.findFirst(filter, new File (path), 0); 355 } 356 357 public static File findFirst(FileFilter filter, File dir, int count) { 358 final String DIR_CLASSES = "classes"; final String DIR_OUTPUT = "output"; File [] files = dir.listFiles(filter); 361 File found = null; 362 File candidate = null; 363 364 if (count > 500) { 365 366 } else { 368 count++; 369 if (files != null && files.length >= 1) { 370 found = files[0]; 371 } 372 if (found == null) { 373 files = dir.listFiles(); 374 if (files != null) { 375 for (int i = 0; i < files.length; i++) { 376 if (files[i].isDirectory()) { 377 if (files[i].getName().equalsIgnoreCase(DIR_CLASSES) 378 || files[i].getName().equalsIgnoreCase(DIR_OUTPUT)) { 379 380 } else { 382 candidate = FileUtil.findFirst(filter, 383 files[i], 384 count); 385 if (found == null) { 386 found = candidate; 387 } else if (candidate != null) { 388 if (FileUtil.charCount(found.getAbsolutePath(), File.separatorChar) 389 > FileUtil.charCount(candidate.getAbsolutePath(), 390 File.separatorChar)) { 391 found = candidate; 392 } 393 } 394 } 395 } 396 } 397 } 398 } 399 } 400 return found; 401 } 402 403 public static String [] findJarPaths(String jar, ClassLoader loader) { 404 405 ArrayList list = new ArrayList (); 407 String [] paths = new String [0]; 408 URLClassLoader urlLoader = null; 409 410 if (loader == null || (!(loader instanceof URLClassLoader ))) { 411 loader = ClassLoader.getSystemClassLoader(); 412 } 413 if (loader instanceof URLClassLoader ) { 414 urlLoader = (URLClassLoader ) loader; 415 for (int i = 0; i < urlLoader.getURLs().length; i++) { 416 String path = null; 417 PathHandle ph = null; 418 419 path = urlLoader.getURLs()[i].getFile().toLowerCase(); 420 if (path.endsWith(jar.toLowerCase())) { 421 if (path.charAt(2) == ':') { 422 path = path.substring(1); 423 } 424 ph = PathHandle.createPathHandle(path); 425 if (ph.isFile()) { 426 list.add(ph.getPath()); 427 } 428 } 429 } 430 list.trimToSize(); 431 paths = new String [list.size()]; 432 paths = (String []) list.toArray(paths); 433 list.clear(); 434 } 435 return paths; 436 } 437 438 private static int charCount(String in, char lookFor) { 439 String search = new String (in); 440 int index = search.indexOf(lookFor); 441 int count = 0; 442 443 while (index > 0) { 444 count++; 445 search = search.substring(index + 1); 446 index = search.indexOf(lookFor); 447 } 448 return count; 449 } 450 451 private static void sleep(int m) { 452 try { 453 Thread.sleep(m); 454 } catch (InterruptedException e) { 455 e.printStackTrace(System.err); 456 } 457 } 458 459 } 460 | Popular Tags |