Path: blob/master/src/java.base/share/classes/java/io/FileSystem.java
41152 views
/*1* Copyright (c) 1998, 2019, 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.io;2627import java.lang.annotation.Native;2829/**30* Package-private abstract class for the local filesystem abstraction.31*/3233abstract class FileSystem {3435/* -- Normalization and construction -- */3637/**38* Return the local filesystem's name-separator character.39*/40public abstract char getSeparator();4142/**43* Return the local filesystem's path-separator character.44*/45public abstract char getPathSeparator();4647/**48* Convert the given pathname string to normal form. If the string is49* already in normal form then it is simply returned.50*/51public abstract String normalize(String path);5253/**54* Compute the length of this pathname string's prefix. The pathname55* string must be in normal form.56*/57public abstract int prefixLength(String path);5859/**60* Resolve the child pathname string against the parent.61* Both strings must be in normal form, and the result62* will be in normal form.63*/64public abstract String resolve(String parent, String child);6566/**67* Return the parent pathname string to be used when the parent-directory68* argument in one of the two-argument File constructors is the empty69* pathname.70*/71public abstract String getDefaultParent();7273/**74* Post-process the given URI path string if necessary. This is used on75* win32, e.g., to transform "/c:/foo" into "c:/foo". The path string76* still has slash separators; code in the File class will translate them77* after this method returns.78*/79public abstract String fromURIPath(String path);808182/* -- Path operations -- */8384/**85* Tell whether or not the given abstract pathname is absolute.86*/87public abstract boolean isAbsolute(File f);8889/**90* Resolve the given abstract pathname into absolute form. Invoked by the91* getAbsolutePath and getCanonicalPath methods in the File class.92*/93public abstract String resolve(File f);9495public abstract String canonicalize(String path) throws IOException;969798/* -- Attribute accessors -- */99100/* Constants for simple boolean attributes */101@Native public static final int BA_EXISTS = 0x01;102@Native public static final int BA_REGULAR = 0x02;103@Native public static final int BA_DIRECTORY = 0x04;104@Native public static final int BA_HIDDEN = 0x08;105106/**107* Return the simple boolean attributes for the file or directory denoted108* by the given abstract pathname, or zero if it does not exist or some109* other I/O error occurs.110*/111public abstract int getBooleanAttributes(File f);112113/**114* Checks if all the given boolean attributes are true for the file or115* directory denoted by the given abstract pathname. False if it does not116* exist or some other I/O error occurs.117*/118public boolean hasBooleanAttributes(File f, int attributes) {119return (getBooleanAttributes(f) & attributes) == attributes;120}121122@Native public static final int ACCESS_READ = 0x04;123@Native public static final int ACCESS_WRITE = 0x02;124@Native public static final int ACCESS_EXECUTE = 0x01;125126/**127* Check whether the file or directory denoted by the given abstract128* pathname may be accessed by this process. The second argument specifies129* which access, ACCESS_READ, ACCESS_WRITE or ACCESS_EXECUTE, to check.130* Return false if access is denied or an I/O error occurs131*/132public abstract boolean checkAccess(File f, int access);133/**134* Set on or off the access permission (to owner only or to all) to the file135* or directory denoted by the given abstract pathname, based on the parameters136* enable, access and oweronly.137*/138public abstract boolean setPermission(File f, int access, boolean enable, boolean owneronly);139140/**141* Return the time at which the file or directory denoted by the given142* abstract pathname was last modified, or zero if it does not exist or143* some other I/O error occurs.144*/145public abstract long getLastModifiedTime(File f);146147/**148* Return the length in bytes of the file denoted by the given abstract149* pathname, or zero if it does not exist, is a directory, or some other150* I/O error occurs.151*/152public abstract long getLength(File f);153154155/* -- File operations -- */156157/**158* Create a new empty file with the given pathname. Return159* {@code true} if the file was created and {@code false} if a160* file or directory with the given pathname already exists. Throw an161* IOException if an I/O error occurs.162*/163public abstract boolean createFileExclusively(String pathname)164throws IOException;165166/**167* Delete the file or directory denoted by the given abstract pathname,168* returning {@code true} if and only if the operation succeeds.169*/170public abstract boolean delete(File f);171172/**173* List the elements of the directory denoted by the given abstract174* pathname. Return an array of strings naming the elements of the175* directory if successful; otherwise, return {@code null}.176*/177public abstract String[] list(File f);178179/**180* Create a new directory denoted by the given abstract pathname,181* returning {@code true} if and only if the operation succeeds.182*/183public abstract boolean createDirectory(File f);184185/**186* Rename the file or directory denoted by the first abstract pathname to187* the second abstract pathname, returning {@code true} if and only if188* the operation succeeds.189*/190public abstract boolean rename(File f1, File f2);191192/**193* Set the last-modified time of the file or directory denoted by the194* given abstract pathname, returning {@code true} if and only if the195* operation succeeds.196*/197public abstract boolean setLastModifiedTime(File f, long time);198199/**200* Mark the file or directory denoted by the given abstract pathname as201* read-only, returning {@code true} if and only if the operation202* succeeds.203*/204public abstract boolean setReadOnly(File f);205206207/* -- Filesystem interface -- */208209/**210* List the available filesystem roots.211*/212public abstract File[] listRoots();213214/* -- Disk usage -- */215@Native public static final int SPACE_TOTAL = 0;216@Native public static final int SPACE_FREE = 1;217@Native public static final int SPACE_USABLE = 2;218219public abstract long getSpace(File f, int t);220221/* -- Basic infrastructure -- */222223/**224* Retrieve the maximum length of a component of a file path.225*226* @return The maximum length of a file path component.227*/228public abstract int getNameMax(String path);229230/**231* Compare two abstract pathnames lexicographically.232*/233public abstract int compare(File f1, File f2);234235/**236* Compute the hash code of an abstract pathname.237*/238public abstract int hashCode(File f);239240// Flags for enabling/disabling performance optimizations for file241// name canonicalization242static final boolean useCanonCaches;243static final boolean useCanonPrefixCache;244245private static boolean getBooleanProperty(String prop, boolean defaultVal) {246String value = System.getProperty(prop);247return (value != null) ? Boolean.parseBoolean(value) : defaultVal;248}249250static {251useCanonCaches = getBooleanProperty("sun.io.useCanonCaches", false);252useCanonPrefixCache = useCanonCaches && getBooleanProperty("sun.io.useCanonPrefixCache", false);253}254}255256257