Path: blob/master/src/java.base/share/classes/java/nio/file/CopyMoveHelper.java
41159 views
/*1* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.nio.file;2627import java.nio.file.attribute.*;28import java.io.InputStream;29import java.io.IOException;3031/**32* Helper class to support copying or moving files when the source and target33* are associated with different providers.34*/3536class CopyMoveHelper {37private CopyMoveHelper() { }3839/**40* Parses the arguments for a file copy operation.41*/42private static class CopyOptions {43boolean replaceExisting = false;44boolean copyAttributes = false;45boolean followLinks = true;4647private CopyOptions() { }4849static CopyOptions parse(CopyOption... options) {50CopyOptions result = new CopyOptions();51for (CopyOption option: options) {52if (option == StandardCopyOption.REPLACE_EXISTING) {53result.replaceExisting = true;54continue;55}56if (option == LinkOption.NOFOLLOW_LINKS) {57result.followLinks = false;58continue;59}60if (option == StandardCopyOption.COPY_ATTRIBUTES) {61result.copyAttributes = true;62continue;63}64if (option == null)65throw new NullPointerException();66throw new UnsupportedOperationException("'" + option +67"' is not a recognized copy option");68}69return result;70}71}7273/**74* Converts the given array of options for moving a file to options suitable75* for copying the file when a move is implemented as copy + delete.76*/77private static CopyOption[] convertMoveToCopyOptions(CopyOption... options)78throws AtomicMoveNotSupportedException79{80int len = options.length;81CopyOption[] newOptions = new CopyOption[len+2];82for (int i=0; i<len; i++) {83CopyOption option = options[i];84if (option == StandardCopyOption.ATOMIC_MOVE) {85throw new AtomicMoveNotSupportedException(null, null,86"Atomic move between providers is not supported");87}88newOptions[i] = option;89}90newOptions[len] = LinkOption.NOFOLLOW_LINKS;91newOptions[len+1] = StandardCopyOption.COPY_ATTRIBUTES;92return newOptions;93}9495/**96* Simple copy for use when source and target are associated with different97* providers98*/99static void copyToForeignTarget(Path source, Path target,100CopyOption... options)101throws IOException102{103CopyOptions opts = CopyOptions.parse(options);104LinkOption[] linkOptions = (opts.followLinks) ? new LinkOption[0] :105new LinkOption[] { LinkOption.NOFOLLOW_LINKS };106107// attributes of source file108BasicFileAttributes attrs = Files.readAttributes(source,109BasicFileAttributes.class,110linkOptions);111if (attrs.isSymbolicLink())112throw new IOException("Copying of symbolic links not supported");113114// delete target if it exists and REPLACE_EXISTING is specified115if (opts.replaceExisting) {116Files.deleteIfExists(target);117} else if (Files.exists(target))118throw new FileAlreadyExistsException(target.toString());119120// create directory or copy file121if (attrs.isDirectory()) {122Files.createDirectory(target);123} else {124try (InputStream in = Files.newInputStream(source)) {125Files.copy(in, target);126}127}128129// copy basic attributes to target130if (opts.copyAttributes) {131BasicFileAttributeView view =132Files.getFileAttributeView(target, BasicFileAttributeView.class);133try {134view.setTimes(attrs.lastModifiedTime(),135attrs.lastAccessTime(),136attrs.creationTime());137} catch (Throwable x) {138// rollback139try {140Files.delete(target);141} catch (Throwable suppressed) {142x.addSuppressed(suppressed);143}144throw x;145}146}147}148149/**150* Simple move implements as copy+delete for use when source and target are151* associated with different providers152*/153static void moveToForeignTarget(Path source, Path target,154CopyOption... options) throws IOException155{156copyToForeignTarget(source, target, convertMoveToCopyOptions(options));157Files.delete(source);158}159}160161162