Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/macosx/classes/com/apple/eio/FileManager.java
38831 views
/*1* Copyright (c) 2011, 2012, 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</code> and <code>creator</code> codes to35* identify and handle files. These codes are unique 4-byte identifiers. The file <code>type</code> is a string that describes the36* contents of a file. For example, the file type <code>APPL</code> identifies the file as an application and therefore37* executable. A file type of <code>TEXT</code> means that the file contains raw text. Any application that can read raw38* text can open a file of type <code>TEXT</code>. Applications that use proprietary file types might assign their files a proprietary39* file <code>type</code> code.40* <p>41* To identify the application that can handle a document, the Finder can look at the <code>creator</code>. For example, if a user42* double-clicks on a document with the <code>ttxt</code> <code>creator</code>, it opens up in Text Edit, the application registered43* with the <code>ttxt</code> <code>creator</code> code. Note that the <code>creator</code>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> code for the file rather than46* the HTML editor's <code>creator</code> 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*/55public class FileManager {56static {57java.security.AccessController.doPrivileged(58new java.security.PrivilegedAction<Void>() {59public Void run() {60System.loadLibrary("osx");61return null;62}63});64}6566/**67* The default68* @since Java for Mac OS X 10.5 - 1.569* @since Java for Mac OS X 10.5 Update 1 - 1.670*/71public final static short kOnAppropriateDisk = -32767;72/**73* Read-only system hierarchy.74* @since Java for Mac OS X 10.5 - 1.575* @since Java for Mac OS X 10.5 Update 1 - 1.676*/77public final static short kSystemDomain = -32766;78/**79* All users of a single machine have access to these resources.80* @since Java for Mac OS X 10.5 - 1.581* @since Java for Mac OS X 10.5 Update 1 - 1.682*/83public final static short kLocalDomain = -32765;84/**85* All users configured to use a common network server has access to these resources.86* @since Java for Mac OS X 10.5 - 1.587* @since Java for Mac OS X 10.5 Update 1 - 1.688*/89public final static short kNetworkDomain = -32764;90/**91* Read/write. Resources that are private to the user.92* @since Java for Mac OS X 10.5 - 1.593* @since Java for Mac OS X 10.5 Update 1 - 1.694*/95public final static short kUserDomain = -32763;969798/**99* Converts an OSType (e.g. "macs" from <CarbonCore/Folders.h>) into an int.100*101* @param type the 4 character type to convert.102* @return an int representing the 4 character value103*104* @since Java for Mac OS X 10.5 - 1.5105* @since Java for Mac OS X 10.5 Update 1 - 1.6106*/107@SuppressWarnings("deprecation")108public static int OSTypeToInt(String type) {109int result = 0;110111byte b[] = { (byte) 0, (byte) 0, (byte) 0, (byte) 0 };112int len = type.length();113if (len > 0) {114if (len > 4) len = 4;115type.getBytes(0, len, b, 4 - len);116}117118for (int i = 0; i < len; i++) {119if (i > 0) result <<= 8;120result |= (b[i] & 0xff);121}122123return result;124}125126/**127* Sets the file <code>type</code> and <code>creator</code> codes for a file or folder.128*129* @since 1.4130*/131public static void setFileTypeAndCreator(String filename, int type, int creator) throws IOException {132SecurityManager security = System.getSecurityManager();133if (security != null) {134security.checkWrite(filename);135}136_setFileTypeAndCreator(filename, type, creator);137}138private static native void _setFileTypeAndCreator(String filename, int type, int creator) throws IOException;139140/**141* Sets the file <code>type</code> code for a file or folder.142*143* @since 1.4144*/145public static void setFileType(String filename, int type) throws IOException {146SecurityManager security = System.getSecurityManager();147if (security != null) {148security.checkWrite(filename);149}150_setFileType(filename, type);151}152private static native void _setFileType(String filename, int type) throws IOException;153154/**155* Sets the file <code>creator</code> code for a file or folder.156*157* @since 1.4158*/159public static void setFileCreator(String filename, int creator) throws IOException {160SecurityManager security = System.getSecurityManager();161if (security != null) {162security.checkWrite(filename);163}164_setFileCreator(filename, creator);165}166private static native void _setFileCreator(String filename, int creator) throws IOException;167168/**169* Obtains the file <code>type</code> code for a file or folder.170*171* @since 1.4172*/173public static int getFileType(String filename) throws IOException {174SecurityManager security = System.getSecurityManager();175if (security != null) {176security.checkRead(filename);177}178return _getFileType(filename);179}180private static native int _getFileType(String filename) throws IOException;181182/**183* Obtains the file <code>creator</code> code for a file or folder.184*185* @since 1.4186*/187public static int getFileCreator(String filename) throws IOException {188SecurityManager security = System.getSecurityManager();189if (security != null) {190security.checkRead(filename);191}192return _getFileCreator(filename);193}194private static native int _getFileCreator(String filename) throws IOException;195196197/**198* Locates a folder of a particular type. Mac OS X recognizes certain specific folders that have distinct purposes.199* For example, the user's desktop or temporary folder. These folders have corresponding codes. Given one of these codes,200* this method returns the path to that particular folder. Certain folders of a given type may appear in more than201* one domain. For example, although there is only one <code>root</code> folder, there are multiple <code>pref</code>202* folders. If this method is called to find the <code>pref</code> folder, it will return the first one it finds,203* the user's preferences folder in <code>~/Library/Preferences</code>. To explicitly locate a folder in a certain204* domain use <code>findFolder(short domain, int folderType)</code> or <code>findFolder(short domain, int folderType,205* boolean createIfNeeded)</code>.206*207* @return the path to the folder searched for208*209* @since 1.4210*/211public static String findFolder(int folderType) throws FileNotFoundException {212return findFolder(kOnAppropriateDisk, folderType);213}214215/**216* Locates a folder of a particular type, within a given domain. Similar to <code>findFolder(int folderType)</code>217* except that the domain to look in can be specified. Valid values for <code>domain</code>include:218* <dl>219* <dt>user</dt>220* <dd>The User domain contains resources specific to the user who is currently logged in</dd>221* <dt>local</dt>222* <dd>The Local domain contains resources shared by all users of the system but are not needed for the system223* itself to run.</dd>224* <dt>network</dt>225* <dd>The Network domain contains resources shared by users of a local area network.</dd>226* <dt>system</dt>227* <dd>The System domain contains the operating system resources installed by Apple.</dd>228* </dl>229*230* @return the path to the folder searched for231*232* @since 1.4233*/234public static String findFolder(short domain, int folderType) throws FileNotFoundException {235return findFolder(domain, folderType, false);236}237238/**239* Locates a folder of a particular type within a given domain and optionally creating the folder if it does240* not exist. The behavior is similar to <code>findFolder(int folderType)</code> and241* <code>findFolder(short domain, int folderType)</code> except that it can create the folder if it does not already exist.242*243* @param createIfNeeded244* set to <code>true</code>, by setting to <code>false</code> the behavior will be the245* same as <code>findFolder(short domain, int folderType, boolean createIfNeeded)</code>246* @return the path to the folder searched for247*248* @since 1.4249*/250public static String findFolder(short domain, int folderType, boolean createIfNeeded) throws FileNotFoundException {251final SecurityManager security = System.getSecurityManager();252if (security != null) {253security.checkPermission(new RuntimePermission("canExamineFileSystem"));254}255256final String foundFolder = _findFolder(domain, folderType, createIfNeeded);257if (foundFolder == null) throw new FileNotFoundException("Can't find folder: " + Integer.toHexString(folderType));258return foundFolder;259}260private static native String _findFolder(short domain, int folderType, boolean createIfNeeded);261262263/**264* Opens the path specified by a URL in the appropriate application for that URL. HTTP URL's (<code>http://</code>)265* open in the default browser as set in the Internet pane of System Preferences. File (<code>file://</code>) and266* FTP URL's (<code>ftp://</code>) open in the Finder. Note that opening an FTP URL will prompt the user for where267* they want to save the downloaded file(s).268*269* @param url270* the URL for the file you want to open, it can either be an HTTP, FTP, or file url271*272* @deprecated this functionality has been superseded by java.awt.Desktop.browse() and java.awt.Desktop.open()273*274* @since 1.4275*/276@Deprecated277public static void openURL(String url) throws IOException {278SecurityManager security = System.getSecurityManager();279if (security != null) {280security.checkPermission(new RuntimePermission("canOpenURLs"));281}282_openURL(url);283}284private static native void _openURL(String url) throws IOException;285286/**287* @return full pathname for the resource identified by a given name.288*289* @since 1.4290*/291public static String getResource(String resourceName) throws FileNotFoundException {292return getResourceFromBundle(resourceName, null, null);293}294295/**296* @return full pathname for the resource identified by a given name and located in the specified bundle subdirectory.297*298* @since 1.4299*/300public static String getResource(String resourceName, String subDirName) throws FileNotFoundException {301return getResourceFromBundle(resourceName, subDirName, null);302}303304/**305* Returns the full pathname for the resource identified by the given name and file extension306* and located in the specified bundle subdirectory.307*308* If extension is an empty string or null, the returned pathname is the first one encountered where the309* file name exactly matches name.310*311* If subpath is null, this method searches the top-level nonlocalized resource directory (typically Resources)312* and the top-level of any language-specific directories. For example, suppose you have a modern bundle and313* specify "Documentation" for the subpath parameter. This method would first look in the314* Contents/Resources/Documentation directory of the bundle, followed by the Documentation subdirectories of315* each language-specific .lproj directory. (The search order for the language-specific directories316* corresponds to the user's preferences.) This method does not recurse through any other subdirectories at317* any of these locations. For more details see the AppKit NSBundle documentation.318*319* @return full pathname for the resource identified by the given name and file extension and located in the specified bundle subdirectory.320*321* @since 1.4322*/323public static String getResource(String resourceName, String subDirName, String type) throws FileNotFoundException {324return getResourceFromBundle(resourceName, subDirName, type);325}326327private static native String getNativeResourceFromBundle(String resourceName, String subDirName, String type) throws FileNotFoundException;328private static String getResourceFromBundle(String resourceName, String subDirName, String type) throws FileNotFoundException {329final SecurityManager security = System.getSecurityManager();330if (security != null) security.checkPermission(new RuntimePermission("canReadBundle"));331332final String resourceFromBundle = getNativeResourceFromBundle(resourceName, subDirName, type);333if (resourceFromBundle == null) throw new FileNotFoundException(resourceName);334return resourceFromBundle;335}336337/**338* Obtains the path to the current application's NSBundle, may not339* return a valid path if Java was launched from the command line.340*341* @return full pathname of the NSBundle of the current application executable.342*343* @since Java for Mac OS X 10.5 Update 1 - 1.6344* @since Java for Mac OS X 10.5 Update 2 - 1.5345*/346public static String getPathToApplicationBundle() {347SecurityManager security = System.getSecurityManager();348if (security != null) security.checkPermission(new RuntimePermission("canReadBundle"));349return getNativePathToApplicationBundle();350}351352private static native String getNativePathToApplicationBundle();353354/**355* Moves the specified file to the Trash356*357* @param file358* @return returns true if the NSFileManager successfully moved the file to the Trash.359* @throws FileNotFoundException360*361* @since Java for Mac OS X 10.6 Update 1 - 1.6362* @since Java for Mac OS X 10.5 Update 6 - 1.6, 1.5363*/364public static boolean moveToTrash(final File file) throws FileNotFoundException {365if (file == null || !file.exists()) throw new FileNotFoundException();366final String fileName = file.getAbsolutePath();367368final SecurityManager security = System.getSecurityManager();369if (security != null) security.checkWrite(fileName);370371return _moveToTrash(fileName);372}373374private static native boolean _moveToTrash(String fileName);375376/**377* Reveals the specified file in the Finder378*379* @param file380* the file to reveal381* @return returns true if the NSFileManager successfully revealed the file in the Finder.382* @throws FileNotFoundException383*384* @since Java for Mac OS X 10.6 Update 1 - 1.6385* @since Java for Mac OS X 10.5 Update 6 - 1.6, 1.5386*/387public static boolean revealInFinder(final File file) throws FileNotFoundException {388if (file == null || !file.exists()) throw new FileNotFoundException();389final String fileName = file.getAbsolutePath();390391final SecurityManager security = System.getSecurityManager();392if (security != null) security.checkRead(fileName);393394return _revealInFinder(fileName);395}396397private static native boolean _revealInFinder(String fileName);398}399400401