Path: blob/master/src/java.desktop/macosx/classes/com/apple/eio/FileManager.java
41153 views
/*1* Copyright (c) 2011, 2021, 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 com.apple.eio;2627import java.io.*;2829/**30* Provides functionality to query and modify Mac-specific file attributes. The methods in this class are based on Finder31* attributes. These attributes in turn are dependent on HFS and HFS+ file systems. As such, it is important to recognize32* their limitation when writing code that must function well across multiple platforms.<p>33*34* In addition to file name suffixes, Mac OS X can use Finder attributes like file {@code type} and {@code creator} codes to35* identify and handle files. These codes are unique 4-byte identifiers. The file {@code type} is a string that describes the36* contents of a file. For example, the file type {@code APPL} identifies the file as an application and therefore37* executable. A file type of {@code TEXT} means that the file contains raw text. Any application that can read raw38* text can open a file of type {@code TEXT}. Applications that use proprietary file types might assign their files a proprietary39* file {@code type} code.40* <p>41* To identify the application that can handle a document, the Finder can look at the {@code creator}. For example, if a user42* double-clicks on a document with the {@code ttxt creator}, it opens up in Text Edit, the application registered43* with the {@code ttxt creator} code. Note that the {@code creator}44* code can be set to any application, not necessarily the application that created it. For example, if you45* use an editor to create an HTML document, you might want to assign a browser's {@code creator} code for the file rather than46* the HTML editor's {@code creator} code. Double-clicking on the document then opens the appropriate browser rather than the47*HTML editor.48*<p>49* If you plan to publicly distribute your application, you must register its creator and any proprietary file types with the Apple50* Developer Connection to avoid collisions with codes used by other developers. You can register a codes online at the51* <a target=_blank href=http://developer.apple.com/dev/cftype/>Creator Code Registration</a> site.52*53* @since 1.454*/55@SuppressWarnings("removal")56public class FileManager {57static {58java.security.AccessController.doPrivileged(59new java.security.PrivilegedAction<Void>() {60public Void run() {61System.loadLibrary("osx");62return null;63}64});65}6667/**68* The default69* @since Java for Mac OS X 10.5 - 1.570* @since Java for Mac OS X 10.5 Update 1 - 1.671*/72public static final short kOnAppropriateDisk = -32767;73/**74* Read-only system hierarchy.75* @since Java for Mac OS X 10.5 - 1.576* @since Java for Mac OS X 10.5 Update 1 - 1.677*/78public static final short kSystemDomain = -32766;79/**80* All users of a single machine have access to these resources.81* @since Java for Mac OS X 10.5 - 1.582* @since Java for Mac OS X 10.5 Update 1 - 1.683*/84public static final short kLocalDomain = -32765;85/**86* All users configured to use a common network server has access to these resources.87* @since Java for Mac OS X 10.5 - 1.588* @since Java for Mac OS X 10.5 Update 1 - 1.689*/90public static final short kNetworkDomain = -32764;91/**92* Read/write. Resources that are private to the user.93* @since Java for Mac OS X 10.5 - 1.594* @since Java for Mac OS X 10.5 Update 1 - 1.695*/96public static final short kUserDomain = -32763;979899/**100* Converts an OSType (e.g. "macs"101* from {@literal <CarbonCore/Folders.h>}) into an int.102*103* @param type the 4 character type to convert.104* @return an int representing the 4 character value105*106* @since Java for Mac OS X 10.5 - 1.5107* @since Java for Mac OS X 10.5 Update 1 - 1.6108*/109@SuppressWarnings("deprecation")110public static int OSTypeToInt(String type) {111int result = 0;112113byte[] b = { (byte) 0, (byte) 0, (byte) 0, (byte) 0 };114int len = type.length();115if (len > 0) {116if (len > 4) len = 4;117type.getBytes(0, len, b, 4 - len);118}119120for (int i = 0; i < len; i++) {121if (i > 0) result <<= 8;122result |= (b[i] & 0xff);123}124125return result;126}127128/**129* Sets the file {@code type} and {@code creator} codes for a file or folder.130*131* @since 1.4132*/133public static void setFileTypeAndCreator(String filename, int type, int creator) throws IOException {134SecurityManager security = System.getSecurityManager();135if (security != null) {136security.checkWrite(filename);137}138_setFileTypeAndCreator(filename, type, creator);139}140private static native void _setFileTypeAndCreator(String filename, int type, int creator) throws IOException;141142/**143* Sets the file {@code type} code for a file or folder.144*145* @since 1.4146*/147public static void setFileType(String filename, int type) throws IOException {148SecurityManager security = System.getSecurityManager();149if (security != null) {150security.checkWrite(filename);151}152_setFileType(filename, type);153}154private static native void _setFileType(String filename, int type) throws IOException;155156/**157* Sets the file {@code creator} code for a file or folder.158*159* @since 1.4160*/161public static void setFileCreator(String filename, int creator) throws IOException {162SecurityManager security = System.getSecurityManager();163if (security != null) {164security.checkWrite(filename);165}166_setFileCreator(filename, creator);167}168private static native void _setFileCreator(String filename, int creator) throws IOException;169170/**171* Obtains the file {@code type} code for a file or folder.172*173* @since 1.4174*/175public static int getFileType(String filename) throws IOException {176SecurityManager security = System.getSecurityManager();177if (security != null) {178security.checkRead(filename);179}180return _getFileType(filename);181}182private static native int _getFileType(String filename) throws IOException;183184/**185* Obtains the file {@code creator} code for a file or folder.186*187* @since 1.4188*/189public static int getFileCreator(String filename) throws IOException {190SecurityManager security = System.getSecurityManager();191if (security != null) {192security.checkRead(filename);193}194return _getFileCreator(filename);195}196private static native int _getFileCreator(String filename) throws IOException;197198199/**200* Locates a folder of a particular type. Mac OS X recognizes certain specific folders that have distinct purposes.201* For example, the user's desktop or temporary folder. These folders have corresponding codes. Given one of these codes,202* this method returns the path to that particular folder. Certain folders of a given type may appear in more than203* one domain. For example, although there is only one {@code root} folder, there are multiple {@code pref}204* folders. If this method is called to find the {@code pref} folder, it will return the first one it finds,205* the user's preferences folder in {@code ~/Library/Preferences}. To explicitly locate a folder in a certain206* domain use {@code findFolder(short domain, int folderType)} or207* {@code findFolder(short domain, int folderType, boolean createIfNeeded)}.208*209* @return the path to the folder searched for210*211* @since 1.4212*/213public static String findFolder(int folderType) throws FileNotFoundException {214return findFolder(kOnAppropriateDisk, folderType);215}216217/**218* Locates a folder of a particular type, within a given domain. Similar to {@code findFolder(int folderType)}219* except that the domain to look in can be specified. Valid values for {@code domain} include:220* <dl>221* <dt>user</dt>222* <dd>The User domain contains resources specific to the user who is currently logged in</dd>223* <dt>local</dt>224* <dd>The Local domain contains resources shared by all users of the system but are not needed for the system225* itself to run.</dd>226* <dt>network</dt>227* <dd>The Network domain contains resources shared by users of a local area network.</dd>228* <dt>system</dt>229* <dd>The System domain contains the operating system resources installed by Apple.</dd>230* </dl>231*232* @return the path to the folder searched for233*234* @since 1.4235*/236public static String findFolder(short domain, int folderType) throws FileNotFoundException {237return findFolder(domain, folderType, false);238}239240/**241* Locates a folder of a particular type within a given domain and optionally creating the folder if it does242* not exist. The behavior is similar to {@code findFolder(int folderType)} and243* {@code findFolder(short domain, int folderType)} except that it can create the folder if it does not already exist.244*245* @param createIfNeeded246* set to {@code true}, by setting to {@code false} the behavior will be the247* same as {@code findFolder(short domain, int folderType, boolean createIfNeeded)}248* @return the path to the folder searched for249*250* @since 1.4251*/252public static String findFolder(short domain, int folderType, boolean createIfNeeded) throws FileNotFoundException {253final SecurityManager security = System.getSecurityManager();254if (security != null) {255security.checkPermission(new RuntimePermission("canExamineFileSystem"));256}257258final String foundFolder = _findFolder(domain, folderType, createIfNeeded);259if (foundFolder == null) throw new FileNotFoundException("Can't find folder: " + Integer.toHexString(folderType));260return foundFolder;261}262private static native String _findFolder(short domain, int folderType, boolean createIfNeeded);263264265/**266* Opens the path specified by a URL in the appropriate application for that URL. HTTP URL's ({@code http://})267* open in the default browser as set in the Internet pane of System Preferences. File ({@code file://}) and268* FTP URL's ({@code ftp://}) open in the Finder. Note that opening an FTP URL will prompt the user for where269* they want to save the downloaded file(s).270*271* @param url272* the URL for the file you want to open, it can either be an HTTP, FTP, or file url273*274* @deprecated this functionality has been superseded by java.awt.Desktop.browse() and java.awt.Desktop.open()275*276* @since 1.4277*/278@Deprecated279public static void openURL(String url) throws IOException {280SecurityManager security = System.getSecurityManager();281if (security != null) {282security.checkPermission(new RuntimePermission("canOpenURLs"));283}284_openURL(url);285}286private static native void _openURL(String url) throws IOException;287288/**289* @return full pathname for the resource identified by a given name.290*291* @since 1.4292*/293public static String getResource(String resourceName) throws FileNotFoundException {294return getResourceFromBundle(resourceName, null, null);295}296297/**298* @return full pathname for the resource identified by a given name and located in the specified bundle subdirectory.299*300* @since 1.4301*/302public static String getResource(String resourceName, String subDirName) throws FileNotFoundException {303return getResourceFromBundle(resourceName, subDirName, null);304}305306/**307* Returns the full pathname for the resource identified by the given name and file extension308* and located in the specified bundle subdirectory.309*310* If extension is an empty string or null, the returned pathname is the first one encountered where the311* file name exactly matches name.312*313* If subpath is null, this method searches the top-level nonlocalized resource directory (typically Resources)314* and the top-level of any language-specific directories. For example, suppose you have a modern bundle and315* specify "Documentation" for the subpath parameter. This method would first look in the316* Contents/Resources/Documentation directory of the bundle, followed by the Documentation subdirectories of317* each language-specific .lproj directory. (The search order for the language-specific directories318* corresponds to the user's preferences.) This method does not recurse through any other subdirectories at319* any of these locations. For more details see the AppKit NSBundle documentation.320*321* @return full pathname for the resource identified by the given name and file extension and located in the specified bundle subdirectory.322*323* @since 1.4324*/325public static String getResource(String resourceName, String subDirName, String type) throws FileNotFoundException {326return getResourceFromBundle(resourceName, subDirName, type);327}328329private static native String getNativeResourceFromBundle(String resourceName, String subDirName, String type) throws FileNotFoundException;330private static String getResourceFromBundle(String resourceName, String subDirName, String type) throws FileNotFoundException {331final SecurityManager security = System.getSecurityManager();332if (security != null) security.checkPermission(new RuntimePermission("canReadBundle"));333334final String resourceFromBundle = getNativeResourceFromBundle(resourceName, subDirName, type);335if (resourceFromBundle == null) throw new FileNotFoundException(resourceName);336return resourceFromBundle;337}338339/**340* Obtains the path to the current application's NSBundle, may not341* return a valid path if Java was launched from the command line.342*343* @return full pathname of the NSBundle of the current application executable.344*345* @since Java for Mac OS X 10.5 Update 1 - 1.6346* @since Java for Mac OS X 10.5 Update 2 - 1.5347*/348public static String getPathToApplicationBundle() {349SecurityManager security = System.getSecurityManager();350if (security != null) security.checkPermission(new RuntimePermission("canReadBundle"));351return getNativePathToApplicationBundle();352}353354private static native String getNativePathToApplicationBundle();355356/**357* Moves the specified file to the Trash358*359* @param file the file360* @return returns true if the NSFileManager successfully moved the file to the Trash.361* @throws FileNotFoundException362*363* @since Java for Mac OS X 10.6 Update 1 - 1.6364* @since Java for Mac OS X 10.5 Update 6 - 1.6, 1.5365*/366public static boolean moveToTrash(final File file) throws FileNotFoundException {367if (file == null) throw new FileNotFoundException();368final String fileName = file.getAbsolutePath();369370final SecurityManager security = System.getSecurityManager();371if (security != null) security.checkDelete(fileName);372373return _moveToTrash(fileName);374}375376private static native boolean _moveToTrash(String fileName);377378/**379* Reveals the specified file in the Finder380*381* @param file382* the file to reveal383* @return returns true if the NSFileManager successfully revealed the file in the Finder.384* @throws FileNotFoundException385*386* @since Java for Mac OS X 10.6 Update 1 - 1.6387* @since Java for Mac OS X 10.5 Update 6 - 1.6, 1.5388*/389public static boolean revealInFinder(final File file) throws FileNotFoundException {390if (file == null || !file.exists()) throw new FileNotFoundException();391final String fileName = file.getAbsolutePath();392393final SecurityManager security = System.getSecurityManager();394if (security != null) security.checkRead(fileName);395396return _revealInFinder(fileName);397}398399private static native boolean _revealInFinder(String fileName);400}401402403