Path: blob/master/src/java.base/share/classes/java/io/FileSystem.java
67794 views
/*1* Copyright (c) 1998, 2022, 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* Tell whether the given abstract pathname is invalid.91*/92public abstract boolean isInvalid(File f);9394/**95* Resolve the given abstract pathname into absolute form. Invoked by the96* getAbsolutePath and getCanonicalPath methods in the File class.97*/98public abstract String resolve(File f);99100public abstract String canonicalize(String path) throws IOException;101102103/* -- Attribute accessors -- */104105/* Constants for simple boolean attributes */106@Native public static final int BA_EXISTS = 0x01;107@Native public static final int BA_REGULAR = 0x02;108@Native public static final int BA_DIRECTORY = 0x04;109@Native public static final int BA_HIDDEN = 0x08;110111/**112* Return the simple boolean attributes for the file or directory denoted113* by the given abstract pathname, or zero if it does not exist or some114* other I/O error occurs.115*/116public abstract int getBooleanAttributes(File f);117118/**119* Checks if all the given boolean attributes are true for the file or120* directory denoted by the given abstract pathname. False if it does not121* exist or some other I/O error occurs.122*/123public boolean hasBooleanAttributes(File f, int attributes) {124return (getBooleanAttributes(f) & attributes) == attributes;125}126127@Native public static final int ACCESS_READ = 0x04;128@Native public static final int ACCESS_WRITE = 0x02;129@Native public static final int ACCESS_EXECUTE = 0x01;130131/**132* Check whether the file or directory denoted by the given abstract133* pathname may be accessed by this process. The second argument specifies134* which access, ACCESS_READ, ACCESS_WRITE or ACCESS_EXECUTE, to check.135* Return false if access is denied or an I/O error occurs136*/137public abstract boolean checkAccess(File f, int access);138/**139* Set on or off the access permission (to owner only or to all) to the file140* or directory denoted by the given abstract pathname, based on the parameters141* enable, access and oweronly.142*/143public abstract boolean setPermission(File f, int access, boolean enable, boolean owneronly);144145/**146* Return the time at which the file or directory denoted by the given147* abstract pathname was last modified, or zero if it does not exist or148* some other I/O error occurs.149*/150public abstract long getLastModifiedTime(File f);151152/**153* Return the length in bytes of the file denoted by the given abstract154* pathname, or zero if it does not exist, is a directory, or some other155* I/O error occurs.156*/157public abstract long getLength(File f);158159160/* -- File operations -- */161162/**163* Create a new empty file with the given pathname. Return164* {@code true} if the file was created and {@code false} if a165* file or directory with the given pathname already exists. Throw an166* IOException if an I/O error occurs.167*/168public abstract boolean createFileExclusively(String pathname)169throws IOException;170171/**172* Delete the file or directory denoted by the given abstract pathname,173* returning {@code true} if and only if the operation succeeds.174*/175public abstract boolean delete(File f);176177/**178* List the elements of the directory denoted by the given abstract179* pathname. Return an array of strings naming the elements of the180* directory if successful; otherwise, return {@code null}.181*/182public abstract String[] list(File f);183184/**185* Create a new directory denoted by the given abstract pathname,186* returning {@code true} if and only if the operation succeeds.187*/188public abstract boolean createDirectory(File f);189190/**191* Rename the file or directory denoted by the first abstract pathname to192* the second abstract pathname, returning {@code true} if and only if193* the operation succeeds.194*/195public abstract boolean rename(File f1, File f2);196197/**198* Set the last-modified time of the file or directory denoted by the199* given abstract pathname, returning {@code true} if and only if the200* operation succeeds.201*/202public abstract boolean setLastModifiedTime(File f, long time);203204/**205* Mark the file or directory denoted by the given abstract pathname as206* read-only, returning {@code true} if and only if the operation207* succeeds.208*/209public abstract boolean setReadOnly(File f);210211212/* -- Filesystem interface -- */213214/**215* List the available filesystem roots.216*/217public abstract File[] listRoots();218219/* -- Disk usage -- */220@Native public static final int SPACE_TOTAL = 0;221@Native public static final int SPACE_FREE = 1;222@Native public static final int SPACE_USABLE = 2;223224public abstract long getSpace(File f, int t);225226/* -- Basic infrastructure -- */227228/**229* Retrieve the maximum length of a component of a file path.230*231* @return The maximum length of a file path component.232*/233public abstract int getNameMax(String path);234235/**236* Compare two abstract pathnames lexicographically.237*/238public abstract int compare(File f1, File f2);239240/**241* Compute the hash code of an abstract pathname.242*/243public abstract int hashCode(File f);244245// Flags for enabling/disabling performance optimizations for file246// name canonicalization247static final boolean useCanonCaches;248static final boolean useCanonPrefixCache;249250private static boolean getBooleanProperty(String prop, boolean defaultVal) {251String value = System.getProperty(prop);252return (value != null) ? Boolean.parseBoolean(value) : defaultVal;253}254255static {256useCanonCaches = getBooleanProperty("sun.io.useCanonCaches", false);257useCanonPrefixCache = useCanonCaches && getBooleanProperty("sun.io.useCanonPrefixCache", false);258}259}260261262