Path: blob/master/src/java.base/share/classes/sun/nio/fs/AbstractFileSystemProvider.java
41159 views
/*1* Copyright (c) 2011, 2020, 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 sun.nio.fs;2627import java.nio.file.Path;28import java.nio.file.LinkOption;29import java.nio.file.attribute.BasicFileAttributes;30import java.nio.file.spi.FileSystemProvider;31import java.io.IOException;32import java.util.Map;3334/**35* Base implementation class of FileSystemProvider36*/3738public abstract class AbstractFileSystemProvider extends FileSystemProvider {39protected AbstractFileSystemProvider() { }4041/**42* Splits the given attribute name into the name of an attribute view and43* the attribute. If the attribute view is not identified then it assumed44* to be "basic".45*/46private static String[] split(String attribute) {47String[] s = new String[2];48int pos = attribute.indexOf(':');49if (pos == -1) {50s[0] = "basic";51s[1] = attribute;52} else {53s[0] = attribute.substring(0, pos++);54s[1] = (pos == attribute.length()) ? "" : attribute.substring(pos);55}56return s;57}5859/**60* Gets a DynamicFileAttributeView by name. Returns {@code null} if the61* view is not available.62*/63abstract DynamicFileAttributeView getFileAttributeView(Path file,64String name,65LinkOption... options);6667@Override68public final void setAttribute(Path file,69String attribute,70Object value,71LinkOption... options)72throws IOException73{74String[] s = split(attribute);75if (s[0].isEmpty())76throw new IllegalArgumentException(attribute);77DynamicFileAttributeView view = getFileAttributeView(file, s[0], options);78if (view == null)79throw new UnsupportedOperationException("View '" + s[0] + "' not available");80view.setAttribute(s[1], value);81}8283@Override84public final Map<String,Object> readAttributes(Path file, String attributes, LinkOption... options)85throws IOException86{87String[] s = split(attributes);88if (s[0].isEmpty())89throw new IllegalArgumentException(attributes);90DynamicFileAttributeView view = getFileAttributeView(file, s[0], options);91if (view == null)92throw new UnsupportedOperationException("View '" + s[0] + "' not available");93return view.readAttributes(s[1].split(","));94}9596/**97* Deletes a file. The {@code failIfNotExists} parameters determines if an98* {@code IOException} is thrown when the file does not exist.99*/100abstract boolean implDelete(Path file, boolean failIfNotExists) throws IOException;101102@Override103public final void delete(Path file) throws IOException {104implDelete(file, true);105}106107@Override108public final boolean deleteIfExists(Path file) throws IOException {109return implDelete(file, false);110}111112/**113* Tests whether a file is a directory.114*115* @return {@code true} if the file is a directory; {@code false} if116* the file does not exist, is not a directory, or it cannot117* be determined if the file is a directory or not.118*/119public boolean isDirectory(Path file) {120try {121return readAttributes(file, BasicFileAttributes.class).isDirectory();122} catch (IOException ioe) {123return false;124}125}126127/**128* Tests whether a file is a regular file with opaque content.129*130* @return {@code true} if the file is a regular file; {@code false} if131* the file does not exist, is not a regular file, or it132* cannot be determined if the file is a regular file or not.133*/134public boolean isRegularFile(Path file) {135try {136return readAttributes(file, BasicFileAttributes.class).isRegularFile();137} catch (IOException ioe) {138return false;139}140}141142/**143* Checks the existence of a file.144*145* @return {@code true} if the file exists; {@code false} if the file does146* not exist or its existence cannot be determined.147*/148public boolean exists(Path file) {149try {150checkAccess(file);151return true;152} catch (IOException ioe) {153return false;154}155}156157/**158* Returns a path name as bytes for a Unix domain socket.159* Different encodings may be used for these names on some platforms.160* If path is empty, then an empty byte[] is returned.161*/162public abstract byte[] getSunPathForSocketFile(Path path);163}164165166