1 25 package org.archive.util; 26 27 import java.io.BufferedReader ; 28 import java.io.File ; 29 import java.io.FileFilter ; 30 import java.io.FileInputStream ; 31 import java.io.FileNotFoundException ; 32 import java.io.FileOutputStream ; 33 import java.io.FilenameFilter ; 34 import java.io.IOException ; 35 import java.io.InputStreamReader ; 36 import java.nio.channels.FileChannel ; 37 import java.util.Arrays ; 38 import java.util.HashSet ; 39 import java.util.Iterator ; 40 import java.util.List ; 41 import java.util.Set ; 42 import java.util.logging.Level ; 43 import java.util.logging.Logger ; 44 import java.util.regex.Pattern ; 45 46 47 51 public class FileUtils { 52 private static final Logger LOGGER = 53 Logger.getLogger(FileUtils.class.getName()); 54 55 public static final File TMPDIR = 56 new File (System.getProperty("java.io.tmpdir", "/tmp")); 57 58 private static final boolean DEFAULT_OVERWRITE = true; 59 60 63 private FileUtils() { 64 super(); 65 } 66 67 public static int copyFiles(final File srcDir, Set srcFile, 68 final File dest) 69 throws IOException { 70 int count = 0; 71 for (Iterator i = srcFile.iterator(); i.hasNext();) { 72 String name = (String )i.next(); 73 File src = new File (srcDir, name); 74 File tgt = new File (dest, name); 75 if (LOGGER.isLoggable(Level.FINE)) { 76 LOGGER.fine("Before " + src.getAbsolutePath() + " " + 77 src.exists() + ", " + tgt.getAbsolutePath() + " " + 78 tgt.exists()); 79 } 80 copyFiles(src, tgt); 81 if (LOGGER.isLoggable(Level.FINE)) { 82 LOGGER.fine("After " + src.getAbsolutePath() + " " + 83 src.exists() + ", " + tgt.getAbsolutePath() + " " + 84 tgt.exists()); 85 } 86 count++; 87 } 88 return count; 89 } 90 91 97 public static void copyFiles(File src, File dest) 98 throws IOException { 99 copyFiles(src, null, dest, false, true); 100 } 101 102 107 public static String [] getSortedDirContent(final File src, 108 final FilenameFilter filter) { 109 if (!src.exists()) { 110 if (LOGGER.isLoggable(Level.FINE)) { 111 LOGGER.fine(src.getAbsolutePath() + " does not exist"); 112 } 113 return null; 114 } 115 116 if (!src.isDirectory()) { 117 if (LOGGER.isLoggable(Level.FINE)) { 118 LOGGER.fine(src.getAbsolutePath() + " is not directory."); 119 } 120 return null; 121 } 122 String [] list = (filter == null)? src.list(): src.list(filter); 124 if (list != null) { 125 Arrays.sort(list); 126 } 127 return list; 128 } 129 130 143 public static void copyFiles(final File src, final FilenameFilter filter, 144 final File dest, final boolean inSortedOrder, final boolean overwrite) 145 throws IOException { 146 if (!src.exists()) { 148 if (LOGGER.isLoggable(Level.FINE)) { 149 LOGGER.fine(src.getAbsolutePath() + " does not exist"); 150 } 151 return; 152 } 153 154 if (src.isDirectory()) { 155 if (LOGGER.isLoggable(Level.FINE)) { 156 LOGGER.fine(src.getAbsolutePath() + " is a directory."); 157 } 158 if (!dest.exists()) { 160 dest.mkdirs(); 161 } 162 String list[] = (filter == null)? src.list(): src.list(filter); 164 if (inSortedOrder) { 165 Arrays.sort(list); 166 } 167 for (int i = 0; i < list.length; i++) { 168 copyFiles(new File (src, list[i]), filter, 169 new File (dest, list[i]), inSortedOrder, overwrite); 170 } 171 } else { 172 copyFile(src, dest, overwrite); 173 } 174 } 175 176 185 public static boolean copyFile(final File src, final File dest) 186 throws FileNotFoundException , IOException { 187 return copyFile(src, dest, -1, DEFAULT_OVERWRITE); 188 } 189 190 202 public static boolean copyFile(final File src, final File dest, 203 final boolean overwrite) 204 throws FileNotFoundException , IOException { 205 return copyFile(src, dest, -1, overwrite); 206 } 207 208 218 public static boolean copyFile(final File src, final File dest, 219 long extent) 220 throws FileNotFoundException , IOException { 221 return copyFile(src, dest, extent, DEFAULT_OVERWRITE); 222 } 223 224 237 public static boolean copyFile(final File src, final File dest, 238 long extent, final boolean overwrite) 239 throws FileNotFoundException , IOException { 240 boolean result = false; 241 if (LOGGER.isLoggable(Level.FINE)) { 242 LOGGER.fine("Copying file " + src + " to " + dest + " extent " + 243 extent + " exists " + dest.exists()); 244 } 245 if (dest.exists()) { 246 if (overwrite) { 247 dest.delete(); 248 LOGGER.finer(dest.getAbsolutePath() + " removed before copy."); 249 } else { 250 return result; 252 } 253 } 254 FileInputStream fis = null; 255 FileOutputStream fos = null; 256 FileChannel fcin = null; 257 FileChannel fcout = null; 258 try { 259 fis = new FileInputStream (src); 261 fos = new FileOutputStream (dest); 262 fcin = fis.getChannel(); 263 fcout = fos.getChannel(); 264 if (extent < 0) { 265 extent = fcin.size(); 266 } 267 268 long trans = fcin.transferTo(0, extent, fcout); 270 if (trans < extent) { 271 result = false; 272 } 273 result = true; 274 } catch (IOException e) { 275 String message = "Copying " + src.getAbsolutePath() + " to " + 280 dest.getAbsolutePath() + " with extent " + extent + 281 " got IOE: " + e.getMessage(); 282 if (e.getMessage().equals("Invalid argument")) { 283 LOGGER.severe("Failed copy, trying workaround: " + message); 284 workaroundCopyFile(src, dest); 285 } else { 286 IOException newE = new IOException (message); 287 newE.setStackTrace(e.getStackTrace()); 288 throw newE; 289 } 290 } finally { 291 if (fcin != null) { 293 fcin.close(); 294 } 295 if (fcout != null) { 296 fcout.close(); 297 } 298 if (fis != null) { 299 fis.close(); 300 } 301 if (fos != null) { 302 fos.close(); 303 } 304 } 305 return result; 306 } 307 308 protected static void workaroundCopyFile(final File src, 309 final File dest) 310 throws IOException { 311 FileInputStream from = null; 312 FileOutputStream to = null; 313 try { 314 from = new FileInputStream (src); 315 to = new FileOutputStream (dest); 316 byte[] buffer = new byte[4096]; 317 int bytesRead; 318 while ((bytesRead = from.read(buffer)) != -1) { 319 to.write(buffer, 0, bytesRead); 320 } 321 } finally { 322 if (from != null) { 323 try { 324 from.close(); 325 } catch (IOException e) { 326 e.printStackTrace(); 327 } 328 } 329 if (to != null) { 330 try { 331 to.close(); 332 } catch (IOException e) { 333 e.printStackTrace(); 334 } 335 } 336 } 337 } 338 339 344 public static boolean deleteDir(File dir) { 345 if (dir.isDirectory()) { 346 String [] children = dir.list(); 347 for (int i=0; i<children.length; i++) { 348 boolean success = deleteDir(new File (dir, children[i])); 349 if (!success) { 350 return false; 351 } 352 } 353 } 354 return dir.delete(); 356 } 357 358 359 360 367 public static String readFileAsString(File file) throws IOException { 368 StringBuffer sb = new StringBuffer ((int) file.length()); 369 String line; 370 BufferedReader br = new BufferedReader (new InputStreamReader ( 371 new FileInputStream (file))); 372 try { 373 line = br.readLine(); 374 while (line != null) { 375 sb.append(line); 376 sb.append("\n"); 377 line = br.readLine(); 378 } 379 } finally { 380 br.close(); 381 } 382 return sb.toString(); 383 } 384 385 393 public static File [] getFilesWithPrefix(File dir, final String prefix) { 394 FileFilter prefixFilter = new FileFilter () { 395 public boolean accept(File pathname) 396 { 397 return pathname.getName().toLowerCase(). 398 startsWith(prefix.toLowerCase()); 399 } 400 }; 401 return dir.listFiles(prefixFilter); 402 } 403 404 410 public static FileFilter getRegexpFileFilter(String regexp) { 411 class RegexpFileFilter implements FileFilter { 413 Pattern pattern; 414 415 protected RegexpFileFilter(String re) { 416 pattern = Pattern.compile(re); 417 } 418 419 public boolean accept(File pathname) { 420 return pattern.matcher(pathname.getName()).matches(); 421 } 422 } 423 424 return new RegexpFileFilter(regexp); 425 } 426 427 435 public static void syncDirectories(final File src, 436 final FilenameFilter filter, final File tgt) 437 throws IOException { 438 Set <String > srcFilenames = null; 439 do { 440 srcFilenames = new HashSet <String >(Arrays.asList(src.list(filter))); 441 List <String > tgtFilenames = Arrays.asList(tgt.list(filter)); 442 srcFilenames.removeAll(tgtFilenames); 443 if (srcFilenames.size() > 0) { 444 int count = FileUtils.copyFiles(src, srcFilenames, tgt); 445 if (LOGGER.isLoggable(Level.FINE)) { 446 LOGGER.fine("Copied " + count); 447 } 448 } 449 } while (srcFilenames != null && srcFilenames.size() > 0); 450 } 451 452 457 public static File isReadable(final File f) throws IOException { 458 if (!f.exists()) { 459 throw new FileNotFoundException (f.getAbsolutePath() + 460 " does not exist."); 461 } 462 463 if (!f.canRead()) { 464 throw new FileNotFoundException (f.getAbsolutePath() + 465 " is not readable."); 466 } 467 468 return f; 469 } 470 471 477 public static boolean isReadableWithExtensionAndMagic(final File f, 478 final String uncompressedExtension, final String magic) 479 throws IOException { 480 boolean result = false; 481 FileUtils.isReadable(f); 482 if(f.getName().toLowerCase().endsWith(uncompressedExtension)) { 483 FileInputStream fis = new FileInputStream (f); 484 try { 485 byte [] b = new byte[magic.length()]; 486 int read = fis.read(b, 0, magic.length()); 487 fis.close(); 488 if (read == magic.length()) { 489 StringBuffer beginStr 490 = new StringBuffer (magic.length()); 491 for (int i = 0; i < magic.length(); i++) { 492 beginStr.append((char)b[i]); 493 } 494 495 if (beginStr.toString(). 496 equalsIgnoreCase(magic)) { 497 result = true; 498 } 499 } 500 } finally { 501 fis.close(); 502 } 503 } 504 505 return result; 506 } 507 } | Popular Tags |