|
27 | 27 | import java.nio.file.Files; |
28 | 28 | import java.nio.file.Path; |
29 | 29 | import java.nio.file.Paths; |
30 | | -import java.security.AccessControlException; |
31 | 30 | import java.security.MessageDigest; |
32 | 31 | import java.security.NoSuchAlgorithmException; |
33 | 32 | import java.util.Arrays; |
@@ -279,47 +278,6 @@ public static boolean isAbsolute (String path) |
279 | 278 | || path.charAt(2) == '\\')); |
280 | 279 | } |
281 | 280 |
|
282 | | - /** |
283 | | - * Copies a file |
284 | | - * |
285 | | - * @return false if failed to copy. |
286 | | - */ |
287 | | - public static boolean copy (File from, File to) |
288 | | - { |
289 | | - boolean targetFileExisted = to.exists(); |
290 | | - try { |
291 | | - copyFile(from, to, false); |
292 | | - return true; |
293 | | - } |
294 | | - catch (IOException e) { |
295 | | - // If the target did not exist before, make sure |
296 | | - // we delete it if there was any error |
297 | | - if (!targetFileExisted) |
298 | | - to.delete(); |
299 | | - |
300 | | - logger.error("Cannot copy " + from + " to " + to, e); |
301 | | - return false; |
302 | | - } |
303 | | - } |
304 | | - |
305 | | - /** |
306 | | - * Append all of <code>sourceFile</code> to the end of <code>targetFile</code> - like |
307 | | - * <code>copy</code>, but just adds to the end of the file. |
308 | | - * |
309 | | - * @return <code>true</code> iff the append succeeded |
310 | | - */ |
311 | | - public static boolean append (File sourceFile, File targetFile) |
312 | | - { |
313 | | - try { |
314 | | - copyFile(sourceFile, targetFile, true); |
315 | | - return true; |
316 | | - } |
317 | | - catch (IOException e) { |
318 | | - logger.error("Cannot append contents of " + sourceFile + " to " + targetFile, e); |
319 | | - return false; |
320 | | - } |
321 | | - } |
322 | | - |
323 | 281 | /** |
324 | 282 | * Write the contents of the given string to the file, creating it if it does not exist and overwriting it if it does. |
325 | 283 | * |
@@ -443,82 +401,6 @@ public static void writeProperties (Properties props, File f) |
443 | 401 | } |
444 | 402 | } |
445 | 403 |
|
446 | | - /** |
447 | | - * Copies a folder recursively, by overwriting files if necessary. Copies all folders but filters |
448 | | - * files. |
449 | | - * <p> |
450 | | - * <b>IMPORTANT:</b>The contents of <code>from</code> are copied to <code>to</code>, but a |
451 | | - * subdirectory is not created for them. This behaves like <code>rsync -a from/ to</code>. |
452 | | - * <p> |
453 | | - * If {@code from} is a file rather than a directory, it is copied (assuming the filter matches |
454 | | - * it) to the file named by {@code to} -- this must not already exist as a directory. |
455 | | - * |
456 | | - * @deprecated Use <code>FileUtil8.recursiveCopy</code> instead. |
457 | | - * @param from Directory whose contents should be copied, or a file. |
458 | | - * @param to Directory to which files and subdirectories of <code>from</code> should be copied, or |
459 | | - * a file path (if {@code from} is a file). |
460 | | - * @param filter the filter to use for selecting which files to copy, or <code>null</code> |
461 | | - * @return false if failed to copy. |
462 | | - */ |
463 | | - @Deprecated |
464 | | - public static boolean recursiveCopy (File from, File to, final FileFilter filter) |
465 | | - { |
466 | | - // Make sure we include all subfolders |
467 | | - FileFilter realFilter = new FileFilter() { |
468 | | - @Override |
469 | | - public boolean accept (File pathname) |
470 | | - { |
471 | | - if (filter == null || pathname.isDirectory()) |
472 | | - return true; |
473 | | - else |
474 | | - return filter == null || filter.accept(pathname); |
475 | | - } |
476 | | - }; |
477 | | - return strictRecursiveCopy(from, to, realFilter); |
478 | | - } |
479 | | - |
480 | | - /** |
481 | | - * Copies a folder recursively, by overwriting files if necessary. Unlike |
482 | | - * {@link #recursiveCopy(File, File, FileFilter)}, this version applies the filter to directories |
483 | | - * as well, and only recurses into directories that are accepted by the filter. |
484 | | - * <p> |
485 | | - * <b>IMPORTANT:</b>The contents of <code>from</code> are copied to <code>to</code>, but a |
486 | | - * subdirectory is not created for them. This behaves like <code>rsync -a from/ to</code>. |
487 | | - * <p> |
488 | | - * If {@code from} is a file rather than a directory, it is copied (assuming the filter matches |
489 | | - * it) to the file named by {@code to} -- this must not already exist as a directory. |
490 | | - * |
491 | | - * @deprecated Use <code>FileUtil8.recursiveCopy</code> instead. |
492 | | - * @param from Directory whose contents should be copied, or a file. |
493 | | - * @param to Directory to which files and subdirectories of <code>from</code> should be copied, or |
494 | | - * a file path (if {@code from} is a file). |
495 | | - * @param filter the filter to use for selecting which files to copy, or <code>null</code> |
496 | | - * @return false if failed to copy. |
497 | | - */ |
498 | | - @Deprecated |
499 | | - public static boolean strictRecursiveCopy (File from, File to, FileFilter filter) |
500 | | - { |
501 | | - if (!from.exists()) { |
502 | | - return false; |
503 | | - } |
504 | | - if (from.isFile()) { |
505 | | - return copy(from, to); |
506 | | - } |
507 | | - else { |
508 | | - if (!to.exists()) { |
509 | | - if (!to.mkdir()) { |
510 | | - return false; |
511 | | - } |
512 | | - } |
513 | | - boolean success = true; |
514 | | - for (File childFrom : list(from, filter)) { |
515 | | - File childTo = new File(to, childFrom.getName()); |
516 | | - success &= strictRecursiveCopy(childFrom, childTo, filter); |
517 | | - } |
518 | | - return success; |
519 | | - } |
520 | | - } |
521 | | - |
522 | 404 | /** |
523 | 405 | * Writes the entire contents of a stream to a file. Closes input stream when writing is done |
524 | 406 | * |
@@ -589,62 +471,6 @@ public static InputStream getFileInputStream (File file) |
589 | 471 | } |
590 | 472 | } |
591 | 473 |
|
592 | | - private static void copyFile (File from, File to, boolean append) throws IOException |
593 | | - { |
594 | | - // Try to exclude the case where the files are the same. If that happens, |
595 | | - // succeed except in 'append' mode since that is probably not the intention |
596 | | - // The logic below works if from and to both exist - and if one of them |
597 | | - // doesn't they can't be the same file! |
598 | | - from = tryMakeCanonical(from); |
599 | | - to = tryMakeCanonical(to); |
600 | | - if (from.equals(to)) { |
601 | | - if (append) |
602 | | - throw new IOException("Trying to append the contents of file " + from + " onto itself"); |
603 | | - else |
604 | | - return; // Nothing to do. |
605 | | - } |
606 | | - |
607 | | - try (FileInputStream fis = new FileInputStream(from); |
608 | | - InputStream in = new BufferedInputStream(fis)) |
609 | | - { |
610 | | - if (!to.exists()) |
611 | | - to.createNewFile(); |
612 | | - try (FileOutputStream fos = new FileOutputStream(to, append); |
613 | | - OutputStream out = new BufferedOutputStream(fos)) |
614 | | - { |
615 | | - byte[] buf = new byte[16 * 1024]; |
616 | | - int count; |
617 | | - while ((count = in.read(buf)) > 0) |
618 | | - out.write(buf, 0, count); |
619 | | - } |
620 | | - } |
621 | | - to.setExecutable(canExecute(from)); |
622 | | - } |
623 | | - |
624 | | - /** |
625 | | - * In the current Java security model, calling {@link File#canExecute()} requires the same |
626 | | - * permission as calling {@link Runtime#exec(String)}. This makes it impossible to run under a |
627 | | - * restrictive security manager if we blindly check for a file's execute bit. |
628 | | - * <p> |
629 | | - * To work around, if an {@link AccessControlException} arises, and it seems to refer to a lack of |
630 | | - * {@link SecurityConstants#FILE_EXECUTE_ACTION} permission, we suppress the exception and return |
631 | | - * <code>false</code>. Other {@link AccessControlException}s are re-thrown, and otherwise the |
632 | | - * return value coincides with {@link File#canExecute()} on the argument. |
633 | | - */ |
634 | | - private static boolean canExecute (File file) |
635 | | - { |
636 | | - try { |
637 | | - return file.canExecute(); |
638 | | - } |
639 | | - catch (AccessControlException e) { |
640 | | - if (e.getPermission() instanceof FilePermission |
641 | | - && ((FilePermission) e.getPermission()).getActions().contains("execute")) |
642 | | - return false; // deliberately ignoring security failure |
643 | | - throw e; |
644 | | - } |
645 | | - } |
646 | | - |
647 | | - |
648 | 474 | private static final BitSet allowedCharacters = new BitSet(256); |
649 | 475 | static { |
650 | 476 | for (int i = 'a'; i <= 'z'; i++) |
@@ -1633,22 +1459,6 @@ public static String sha1 (File file) |
1633 | 1459 | } |
1634 | 1460 | } |
1635 | 1461 |
|
1636 | | - /** |
1637 | | - * Copy the source properties file to the target, appending the given variable definitions as |
1638 | | - * properties (with proper escaping). An optional marker string is printed as a comment before the |
1639 | | - * extra variables, if any. |
1640 | | - */ |
1641 | | - public static void copyPropertiesFile ( |
1642 | | - File source, |
1643 | | - File target, |
1644 | | - Set<Pair<String, String>> extraVariables, |
1645 | | - String extraComment) |
1646 | | - { |
1647 | | - if (source.isFile()) |
1648 | | - copy(source, target); |
1649 | | - appendProperties(target, extraVariables, extraComment); |
1650 | | - } |
1651 | | - |
1652 | 1462 | /** |
1653 | 1463 | * Append the given set of properties to a specified file, taking care of proper escaping of |
1654 | 1464 | * special characters. |
@@ -1904,31 +1714,6 @@ public static void forceRename (File src, File dest) |
1904 | 1714 | + "'."); |
1905 | 1715 | } |
1906 | 1716 |
|
1907 | | - /** |
1908 | | - * Copy a file, creating any directories as needed. If the destination file (or directory) |
1909 | | - * exists, it is overwritten. |
1910 | | - * |
1911 | | - * @param src The file to be renamed. Must be non-null and must exist. |
1912 | | - * @param dest The path to copy the file to. Must be non-null. Will be overwritten if it already exists. |
1913 | | - */ |
1914 | | - public static void forceCopy(File src, File dest) |
1915 | | - { |
1916 | | - final String errorPrefix = "FileUtil.forceCopy: "; |
1917 | | - if (src == null) |
1918 | | - throw new CatastrophicError(errorPrefix + "source File is null."); |
1919 | | - if (dest == null) |
1920 | | - throw new CatastrophicError(errorPrefix + "destination File is null."); |
1921 | | - if (!src.exists()) |
1922 | | - throw new ResourceError(errorPrefix + "source File '" + src.toString() + "' does not exist.", new FileNotFoundException(src.toString())); |
1923 | | - |
1924 | | - mkdirs(dest.getAbsoluteFile().getParentFile()); |
1925 | | - if (dest.exists() && !recursiveDelete(dest)) |
1926 | | - throw new ResourceError(errorPrefix + "Couldn't overwrite destination file '" + dest.toString() + "'."); |
1927 | | - if (!copy(src, dest)) |
1928 | | - throw new ResourceError(errorPrefix + "Couldn't copy file '" + src.toString() + "' to '" + dest.toString() |
1929 | | - + "'."); |
1930 | | - } |
1931 | | - |
1932 | 1717 | /** |
1933 | 1718 | * Query whether a {@link File} is non-null, and represents an existing <b>file</b> that can be |
1934 | 1719 | * read. |
|
0 commit comments