| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
package org.apache.commons.io.file; |
| 19 |
|
| 20 |
import java.io.IOException; |
| 21 |
import java.nio.file.CopyOption; |
| 22 |
import java.nio.file.FileVisitResult; |
| 23 |
import java.nio.file.Files; |
| 24 |
import java.nio.file.Path; |
| 25 |
import java.nio.file.ProviderMismatchException; |
| 26 |
import java.nio.file.attribute.BasicFileAttributes; |
| 27 |
import java.util.Arrays; |
| 28 |
import java.util.Objects; |
| 29 |
|
| 30 |
import org.apache.commons.io.file.Counters.PathCounters; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class CopyDirectoryVisitor extends CountingPathVisitor { |
| 38 |
|
| 39 |
private static CopyOption[] toCopyOption(final CopyOption... copyOptions) { |
| 40 |
return copyOptions == null ? PathUtils.EMPTY_COPY_OPTIONS : copyOptions.clone(); |
| 41 |
} |
| 42 |
|
| 43 |
private final CopyOption[] copyOptions; |
| 44 |
private final Path sourceDirectory; |
| 45 |
private final Path targetDirectory; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public CopyDirectoryVisitor(final PathCounters pathCounter, final Path sourceDirectory, final Path targetDirectory, final CopyOption... copyOptions) { |
| 56 |
super(pathCounter); |
| 57 |
this.sourceDirectory = sourceDirectory; |
| 58 |
this.targetDirectory = targetDirectory; |
| 59 |
this.copyOptions = toCopyOption(copyOptions); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public CopyDirectoryVisitor(final PathCounters pathCounter, final PathFilter fileFilter, final PathFilter dirFilter, final Path sourceDirectory, |
| 74 |
final Path targetDirectory, final CopyOption... copyOptions) { |
| 75 |
super(pathCounter, fileFilter, dirFilter); |
| 76 |
this.sourceDirectory = sourceDirectory; |
| 77 |
this.targetDirectory = targetDirectory; |
| 78 |
this.copyOptions = toCopyOption(copyOptions); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
protected void copy(final Path sourceFile, final Path targetFile) throws IOException { |
| 90 |
Files.copy(sourceFile, targetFile, copyOptions); |
| 91 |
} |
| 92 |
|
| 93 |
@Override |
| 94 |
public boolean equals(final Object obj) { |
| 95 |
if (this == obj) { |
| 96 |
return true; |
| 97 |
} |
| 98 |
if (!super.equals(obj)) { |
| 99 |
return false; |
| 100 |
} |
| 101 |
if (getClass() != obj.getClass()) { |
| 102 |
return false; |
| 103 |
} |
| 104 |
final CopyDirectoryVisitor other = (CopyDirectoryVisitor) obj; |
| 105 |
return Arrays.equals(copyOptions, other.copyOptions) && Objects.equals(sourceDirectory, other.sourceDirectory) |
| 106 |
&& Objects.equals(targetDirectory, other.targetDirectory); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public CopyOption[] getCopyOptions() { |
| 116 |
return copyOptions.clone(); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public Path getSourceDirectory() { |
| 126 |
return sourceDirectory; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public Path getTargetDirectory() { |
| 136 |
return targetDirectory; |
| 137 |
} |
| 138 |
|
| 139 |
@Override |
| 140 |
public int hashCode() { |
| 141 |
final int prime = 31; |
| 142 |
int result = super.hashCode(); |
| 143 |
result = prime * result + Arrays.hashCode(copyOptions); |
| 144 |
return prime * result + Objects.hash(sourceDirectory, targetDirectory); |
| 145 |
} |
| 146 |
|
| 147 |
@Override |
| 148 |
public FileVisitResult preVisitDirectory(final Path directory, final BasicFileAttributes attributes) |
| 149 |
throws IOException { |
| 150 |
final Path newTargetDir = resolveRelativeAsString(directory); |
| 151 |
if (Files.notExists(newTargetDir)) { |
| 152 |
Files.createDirectory(newTargetDir); |
| 153 |
} |
| 154 |
return super.preVisitDirectory(directory, attributes); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
private Path resolveRelativeAsString(final Path directory) { |
| 168 |
return PathUtils.resolve(targetDirectory, sourceDirectory.relativize(directory)); |
| 169 |
} |
| 170 |
|
| 171 |
@Override |
| 172 |
public FileVisitResult visitFile(final Path sourceFile, final BasicFileAttributes attributes) throws IOException { |
| 173 |
final Path targetFile = resolveRelativeAsString(sourceFile); |
| 174 |
if (accept(sourceFile, attributes)) { |
| 175 |
copy(sourceFile, targetFile); |
| 176 |
updateFileCounters(targetFile, attributes); |
| 177 |
} |
| 178 |
return FileVisitResult.CONTINUE; |
| 179 |
} |
| 180 |
|
| 181 |
} |