use of java.nio.file.StandardCopyOption in project graal by oracle.
the class IOHelper method copy.
static void copy(final Path source, final Path target, final FileSystem sourceFileSystem, final FileSystem targetFileSystem, CopyOption... options) throws IOException {
if (source.equals(target)) {
return;
}
final Path sourceReal = sourceFileSystem.toRealPath(source, LinkOption.NOFOLLOW_LINKS);
final Path targetReal = targetFileSystem.toRealPath(target, LinkOption.NOFOLLOW_LINKS);
if (sourceReal.equals(targetReal)) {
return;
}
final Set<LinkOption> linkOptions = new HashSet<>();
final Set<StandardCopyOption> copyOptions = EnumSet.noneOf(StandardCopyOption.class);
for (CopyOption option : options) {
if (option instanceof StandardCopyOption) {
copyOptions.add((StandardCopyOption) option);
} else if (option instanceof LinkOption) {
linkOptions.add((LinkOption) option);
}
}
if (copyOptions.contains(StandardCopyOption.ATOMIC_MOVE)) {
throw new AtomicMoveNotSupportedException(source.getFileName().toString(), target.getFileName().toString(), "Atomic move not supported");
}
final Map<String, Object> sourceAttributes = sourceFileSystem.readAttributes(sourceReal, "basic:isSymbolicLink,isDirectory,lastModifiedTime,lastAccessTime,creationTime", linkOptions.toArray(new LinkOption[linkOptions.size()]));
if ((Boolean) sourceAttributes.getOrDefault("isSymbolicLink", false)) {
throw new IOException("Copying of symbolic links is not supported.");
}
if (copyOptions.contains(StandardCopyOption.REPLACE_EXISTING)) {
try {
targetFileSystem.delete(targetReal);
} catch (NoSuchFileException notFound) {
// Does not exist - nothing to delete
}
} else {
boolean exists;
try {
targetFileSystem.checkAccess(targetReal, EnumSet.noneOf(AccessMode.class));
exists = true;
} catch (IOException ioe) {
exists = false;
}
if (exists) {
throw new FileAlreadyExistsException(target.toString());
}
}
if ((Boolean) sourceAttributes.getOrDefault("isDirectory", false)) {
targetFileSystem.createDirectory(targetReal);
} else {
final Set<StandardOpenOption> readOptions = EnumSet.of(StandardOpenOption.READ);
final Set<StandardOpenOption> writeOptions = EnumSet.of(StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW);
try (final SeekableByteChannel sourceChannel = sourceFileSystem.newByteChannel(sourceReal, readOptions);
final SeekableByteChannel targetChannel = targetFileSystem.newByteChannel(targetReal, writeOptions)) {
final ByteBuffer buffer = ByteBuffer.allocateDirect(1 << 16);
while (sourceChannel.read(buffer) != -1) {
buffer.flip();
while (buffer.hasRemaining()) {
targetChannel.write(buffer);
}
buffer.clear();
}
}
}
if (copyOptions.contains(StandardCopyOption.COPY_ATTRIBUTES)) {
String[] basicMutableAttributes = { "lastModifiedTime", "lastAccessTime", "creationTime" };
try {
for (String key : basicMutableAttributes) {
final Object value = sourceAttributes.get(key);
if (value != null) {
targetFileSystem.setAttribute(targetReal, key, value);
}
}
} catch (Throwable rootCause) {
try {
targetFileSystem.delete(targetReal);
} catch (Throwable suppressed) {
rootCause.addSuppressed(suppressed);
}
throw rootCause;
}
}
}
use of java.nio.file.StandardCopyOption in project google-cloud-java by GoogleCloudPlatform.
the class CloudStorageFileSystemProvider method copy.
@Override
public void copy(Path source, Path target, CopyOption... options) throws IOException {
initStorage();
boolean wantCopyAttributes = false;
boolean wantReplaceExisting = false;
boolean setContentType = false;
boolean setCacheControl = false;
boolean setContentEncoding = false;
boolean setContentDisposition = false;
CloudStoragePath toPath = CloudStorageUtil.checkPath(target);
BlobInfo.Builder tgtInfoBuilder = BlobInfo.newBuilder(toPath.getBlobId()).setContentType("");
int blockSize = -1;
for (CopyOption option : options) {
if (option instanceof StandardCopyOption) {
switch((StandardCopyOption) option) {
case COPY_ATTRIBUTES:
wantCopyAttributes = true;
break;
case REPLACE_EXISTING:
wantReplaceExisting = true;
break;
case ATOMIC_MOVE:
default:
throw new UnsupportedOperationException(option.toString());
}
} else if (option instanceof CloudStorageOption) {
if (option instanceof OptionBlockSize) {
blockSize = ((OptionBlockSize) option).size();
} else if (option instanceof OptionMimeType) {
tgtInfoBuilder.setContentType(((OptionMimeType) option).mimeType());
setContentType = true;
} else if (option instanceof OptionCacheControl) {
tgtInfoBuilder.setCacheControl(((OptionCacheControl) option).cacheControl());
setCacheControl = true;
} else if (option instanceof OptionContentEncoding) {
tgtInfoBuilder.setContentEncoding(((OptionContentEncoding) option).contentEncoding());
setContentEncoding = true;
} else if (option instanceof OptionContentDisposition) {
tgtInfoBuilder.setContentDisposition(((OptionContentDisposition) option).contentDisposition());
setContentDisposition = true;
} else {
throw new UnsupportedOperationException(option.toString());
}
} else {
throw new UnsupportedOperationException(option.toString());
}
}
CloudStoragePath fromPath = CloudStorageUtil.checkPath(source);
blockSize = blockSize != -1 ? blockSize : Ints.max(fromPath.getFileSystem().config().blockSize(), toPath.getFileSystem().config().blockSize());
if (fromPath.seemsLikeADirectory() && toPath.seemsLikeADirectory()) {
if (fromPath.getFileSystem().config().usePseudoDirectories() && toPath.getFileSystem().config().usePseudoDirectories()) {
// NOOP: This would normally create an empty directory.
return;
} else {
checkArgument(!fromPath.getFileSystem().config().usePseudoDirectories() && !toPath.getFileSystem().config().usePseudoDirectories(), "File systems associated with paths don't agree on pseudo-directories.");
}
}
if (fromPath.seemsLikeADirectoryAndUsePseudoDirectories()) {
throw new CloudStoragePseudoDirectoryException(fromPath);
}
if (toPath.seemsLikeADirectoryAndUsePseudoDirectories()) {
throw new CloudStoragePseudoDirectoryException(toPath);
}
try {
if (wantCopyAttributes) {
BlobInfo blobInfo = storage.get(fromPath.getBlobId());
if (null == blobInfo) {
throw new NoSuchFileException(fromPath.toString());
}
if (!setCacheControl) {
tgtInfoBuilder.setCacheControl(blobInfo.getCacheControl());
}
if (!setContentType) {
tgtInfoBuilder.setContentType(blobInfo.getContentType());
}
if (!setContentEncoding) {
tgtInfoBuilder.setContentEncoding(blobInfo.getContentEncoding());
}
if (!setContentDisposition) {
tgtInfoBuilder.setContentDisposition(blobInfo.getContentDisposition());
}
tgtInfoBuilder.setAcl(blobInfo.getAcl());
tgtInfoBuilder.setMetadata(blobInfo.getMetadata());
}
BlobInfo tgtInfo = tgtInfoBuilder.build();
Storage.CopyRequest.Builder copyReqBuilder = Storage.CopyRequest.newBuilder().setSource(fromPath.getBlobId());
if (wantReplaceExisting) {
copyReqBuilder = copyReqBuilder.setTarget(tgtInfo);
} else {
copyReqBuilder = copyReqBuilder.setTarget(tgtInfo, Storage.BlobTargetOption.doesNotExist());
}
CopyWriter copyWriter = storage.copy(copyReqBuilder.build());
copyWriter.getResult();
} catch (StorageException oops) {
throw asIoException(oops);
}
}
Aggregations