diff --git a/src/java.base/share/classes/java/nio/file/CopyMoveHelper.java b/src/java.base/share/classes/java/nio/file/CopyMoveHelper.java index 357e200e930..4e5bd7ebba1 100644 --- a/src/java.base/share/classes/java/nio/file/CopyMoveHelper.java +++ b/src/java.base/share/classes/java/nio/file/CopyMoveHelper.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2011, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -109,21 +109,21 @@ class CopyMoveHelper { LinkOption[] linkOptions = (opts.followLinks) ? new LinkOption[0] : new LinkOption[] { LinkOption.NOFOLLOW_LINKS }; - // retrieve source posix view, null if unsupported - final PosixFileAttributeView sourcePosixView = - Files.getFileAttributeView(source, PosixFileAttributeView.class); + // determine whether the source supports posix attributes + boolean sourceSupportsPosixAttributes = Files.getFileAttributeView + (source, PosixFileAttributeView.class) != null; // attributes of source file BasicFileAttributes sourceAttrs = null; - if (sourcePosixView != null) { + if (sourceSupportsPosixAttributes) sourceAttrs = Files.readAttributes(source, PosixFileAttributes.class, linkOptions); - } - if (sourceAttrs == null) + else sourceAttrs = Files.readAttributes(source, BasicFileAttributes.class, linkOptions); + assert sourceAttrs != null; if (sourceAttrs.isSymbolicLink()) @@ -151,7 +151,7 @@ class CopyMoveHelper { // copy basic and, if supported, POSIX attributes to target if (opts.copyAttributes) { BasicFileAttributeView targetView = null; - if (sourcePosixView != null) { + if (sourceSupportsPosixAttributes) { targetView = Files.getFileAttributeView(target, PosixFileAttributeView.class); }