15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.apple.eio; 27 28 import java.io.*; 29 30 /** 31 * Provides functionality to query and modify Mac-specific file attributes. The methods in this class are based on Finder 32 * attributes. These attributes in turn are dependent on HFS and HFS+ file systems. As such, it is important to recognize 33 * their limitation when writing code that must function well across multiple platforms.<p> 34 * 35 * In addition to file name suffixes, Mac OS X can use Finder attributes like file <code>type</code> and <code>creator</code> codes to 36 * identify and handle files. These codes are unique 4-byte identifiers. The file <code>type</code> is a string that describes the 37 * contents of a file. For example, the file type <code>APPL</code> identifies the file as an application and therefore 38 * executable. A file type of <code>TEXT</code> means that the file contains raw text. Any application that can read raw 39 * text can open a file of type <code>TEXT</code>. Applications that use proprietary file types might assign their files a proprietary 40 * file <code>type</code> code. 41 * <p> 42 * To identify the application that can handle a document, the Finder can look at the <code>creator</code>. For example, if a user 43 * double-clicks on a document with the <code>ttxt</code> <code>creator</code>, it opens up in Text Edit, the application registered 44 * with the <code>ttxt</code> <code>creator</code> code. Note that the <code>creator</code> 45 * code can be set to any application, not necessarily the application that created it. For example, if you 46 * use an editor to create an HTML document, you might want to assign a browser's <code>creator</code> code for the file rather than 47 * the HTML editor's <code>creator</code> code. Double-clicking on the document then opens the appropriate browser rather than the 48 *HTML editor. 49 *<p> 50 * If you plan to publicly distribute your application, you must register its creator and any proprietary file types with the Apple 51 * Developer Connection to avoid collisions with codes used by other developers. You can register a codes online at the 52 * <a target=_blank href=http://developer.apple.com/dev/cftype/>Creator Code Registration</a> site. 53 * 54 * @since 1.4 55 */ 56 public class FileManager { 57 static { 58 java.security.AccessController.doPrivileged( 59 new java.security.PrivilegedAction<Void>() { 60 public Void run() { 61 System.loadLibrary("osx"); 62 return null; 63 } 64 }); 65 } 66 67 /** 109 @SuppressWarnings("deprecation") 110 public static int OSTypeToInt(String type) { 111 int result = 0; 112 113 byte b[] = { (byte) 0, (byte) 0, (byte) 0, (byte) 0 }; 114 int len = type.length(); 115 if (len > 0) { 116 if (len > 4) len = 4; 117 type.getBytes(0, len, b, 4 - len); 118 } 119 120 for (int i = 0; i < len; i++) { 121 if (i > 0) result <<= 8; 122 result |= (b[i] & 0xff); 123 } 124 125 return result; 126 } 127 128 /** 129 * Sets the file <code>type</code> and <code>creator</code> codes for a file or folder. 130 * 131 * @since 1.4 132 */ 133 public static void setFileTypeAndCreator(String filename, int type, int creator) throws IOException { 134 SecurityManager security = System.getSecurityManager(); 135 if (security != null) { 136 security.checkWrite(filename); 137 } 138 _setFileTypeAndCreator(filename, type, creator); 139 } 140 private static native void _setFileTypeAndCreator(String filename, int type, int creator) throws IOException; 141 142 /** 143 * Sets the file <code>type</code> code for a file or folder. 144 * 145 * @since 1.4 146 */ 147 public static void setFileType(String filename, int type) throws IOException { 148 SecurityManager security = System.getSecurityManager(); 149 if (security != null) { 150 security.checkWrite(filename); 151 } 152 _setFileType(filename, type); 153 } 154 private static native void _setFileType(String filename, int type) throws IOException; 155 156 /** 157 * Sets the file <code>creator</code> code for a file or folder. 158 * 159 * @since 1.4 160 */ 161 public static void setFileCreator(String filename, int creator) throws IOException { 162 SecurityManager security = System.getSecurityManager(); 163 if (security != null) { 164 security.checkWrite(filename); 165 } 166 _setFileCreator(filename, creator); 167 } 168 private static native void _setFileCreator(String filename, int creator) throws IOException; 169 170 /** 171 * Obtains the file <code>type</code> code for a file or folder. 172 * 173 * @since 1.4 174 */ 175 public static int getFileType(String filename) throws IOException { 176 SecurityManager security = System.getSecurityManager(); 177 if (security != null) { 178 security.checkRead(filename); 179 } 180 return _getFileType(filename); 181 } 182 private static native int _getFileType(String filename) throws IOException; 183 184 /** 185 * Obtains the file <code>creator</code> code for a file or folder. 186 * 187 * @since 1.4 188 */ 189 public static int getFileCreator(String filename) throws IOException { 190 SecurityManager security = System.getSecurityManager(); 191 if (security != null) { 192 security.checkRead(filename); 193 } 194 return _getFileCreator(filename); 195 } 196 private static native int _getFileCreator(String filename) throws IOException; 197 198 199 /** 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 than 203 * one domain. For example, although there is only one <code>root</code> folder, there are multiple <code>pref</code> 204 * folders. If this method is called to find the <code>pref</code> folder, it will return the first one it finds, 205 * the user's preferences folder in <code>~/Library/Preferences</code>. To explicitly locate a folder in a certain 206 * domain use <code>findFolder(short domain, int folderType)</code> or <code>findFolder(short domain, int folderType, 207 * boolean createIfNeeded)</code>. 208 * 209 * @return the path to the folder searched for 210 * 211 * @since 1.4 212 */ 213 public static String findFolder(int folderType) throws FileNotFoundException { 214 return findFolder(kOnAppropriateDisk, folderType); 215 } 216 217 /** 218 * Locates a folder of a particular type, within a given domain. Similar to <code>findFolder(int folderType)</code> 219 * except that the domain to look in can be specified. Valid values for <code>domain</code>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 system 225 * 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 for 233 * 234 * @since 1.4 235 */ 236 public static String findFolder(short domain, int folderType) throws FileNotFoundException { 237 return findFolder(domain, folderType, false); 238 } 239 240 /** 241 * Locates a folder of a particular type within a given domain and optionally creating the folder if it does 242 * not exist. The behavior is similar to <code>findFolder(int folderType)</code> and 243 * <code>findFolder(short domain, int folderType)</code> except that it can create the folder if it does not already exist. 244 * 245 * @param createIfNeeded 246 * set to <code>true</code>, by setting to <code>false</code> the behavior will be the 247 * same as <code>findFolder(short domain, int folderType, boolean createIfNeeded)</code> 248 * @return the path to the folder searched for 249 * 250 * @since 1.4 251 */ 252 public static String findFolder(short domain, int folderType, boolean createIfNeeded) throws FileNotFoundException { 253 final SecurityManager security = System.getSecurityManager(); 254 if (security != null) { 255 security.checkPermission(new RuntimePermission("canExamineFileSystem")); 256 } 257 258 final String foundFolder = _findFolder(domain, folderType, createIfNeeded); 259 if (foundFolder == null) throw new FileNotFoundException("Can't find folder: " + Integer.toHexString(folderType)); 260 return foundFolder; 261 } 262 private static native String _findFolder(short domain, int folderType, boolean createIfNeeded); 263 264 265 /** 266 * Opens the path specified by a URL in the appropriate application for that URL. HTTP URL's (<code>http://</code>) 267 * open in the default browser as set in the Internet pane of System Preferences. File (<code>file://</code>) and 268 * FTP URL's (<code>ftp://</code>) open in the Finder. Note that opening an FTP URL will prompt the user for where 269 * they want to save the downloaded file(s). 270 * 271 * @param url 272 * the URL for the file you want to open, it can either be an HTTP, FTP, or file url 273 * 274 * @deprecated this functionality has been superseded by java.awt.Desktop.browse() and java.awt.Desktop.open() 275 * 276 * @since 1.4 277 */ 278 @Deprecated 279 public static void openURL(String url) throws IOException { 280 SecurityManager security = System.getSecurityManager(); 281 if (security != null) { 282 security.checkPermission(new RuntimePermission("canOpenURLs")); 283 } 284 _openURL(url); 285 } 286 private static native void _openURL(String url) throws IOException; 287 288 /** | 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.apple.eio; 27 28 import java.io.*; 29 30 /** 31 * Provides functionality to query and modify Mac-specific file attributes. The methods in this class are based on Finder 32 * attributes. These attributes in turn are dependent on HFS and HFS+ file systems. As such, it is important to recognize 33 * their limitation when writing code that must function well across multiple platforms.<p> 34 * 35 * In addition to file name suffixes, Mac OS X can use Finder attributes like file {@code type} and {@code creator} codes to 36 * identify and handle files. These codes are unique 4-byte identifiers. The file {@code type} is a string that describes the 37 * contents of a file. For example, the file type {@code APPL} identifies the file as an application and therefore 38 * executable. A file type of {@code TEXT} means that the file contains raw text. Any application that can read raw 39 * text can open a file of type {@code TEXT}. Applications that use proprietary file types might assign their files a proprietary 40 * file {@code type} code. 41 * <p> 42 * To identify the application that can handle a document, the Finder can look at the {@code creator}. For example, if a user 43 * double-clicks on a document with the {@code ttxt creator}, it opens up in Text Edit, the application registered 44 * with the {@code ttxt creator} code. Note that the {@code creator} 45 * code can be set to any application, not necessarily the application that created it. For example, if you 46 * use an editor to create an HTML document, you might want to assign a browser's {@code creator} code for the file rather than 47 * the HTML editor's {@code creator} code. Double-clicking on the document then opens the appropriate browser rather than the 48 *HTML editor. 49 *<p> 50 * If you plan to publicly distribute your application, you must register its creator and any proprietary file types with the Apple 51 * Developer Connection to avoid collisions with codes used by other developers. You can register a codes online at the 52 * <a target=_blank href=http://developer.apple.com/dev/cftype/>Creator Code Registration</a> site. 53 * 54 * @since 1.4 55 */ 56 public class FileManager { 57 static { 58 java.security.AccessController.doPrivileged( 59 new java.security.PrivilegedAction<Void>() { 60 public Void run() { 61 System.loadLibrary("osx"); 62 return null; 63 } 64 }); 65 } 66 67 /** 109 @SuppressWarnings("deprecation") 110 public static int OSTypeToInt(String type) { 111 int result = 0; 112 113 byte b[] = { (byte) 0, (byte) 0, (byte) 0, (byte) 0 }; 114 int len = type.length(); 115 if (len > 0) { 116 if (len > 4) len = 4; 117 type.getBytes(0, len, b, 4 - len); 118 } 119 120 for (int i = 0; i < len; i++) { 121 if (i > 0) result <<= 8; 122 result |= (b[i] & 0xff); 123 } 124 125 return result; 126 } 127 128 /** 129 * Sets the file {@code type} and {@code creator} codes for a file or folder. 130 * 131 * @since 1.4 132 */ 133 public static void setFileTypeAndCreator(String filename, int type, int creator) throws IOException { 134 SecurityManager security = System.getSecurityManager(); 135 if (security != null) { 136 security.checkWrite(filename); 137 } 138 _setFileTypeAndCreator(filename, type, creator); 139 } 140 private static native void _setFileTypeAndCreator(String filename, int type, int creator) throws IOException; 141 142 /** 143 * Sets the file {@code type} code for a file or folder. 144 * 145 * @since 1.4 146 */ 147 public static void setFileType(String filename, int type) throws IOException { 148 SecurityManager security = System.getSecurityManager(); 149 if (security != null) { 150 security.checkWrite(filename); 151 } 152 _setFileType(filename, type); 153 } 154 private static native void _setFileType(String filename, int type) throws IOException; 155 156 /** 157 * Sets the file {@code creator} code for a file or folder. 158 * 159 * @since 1.4 160 */ 161 public static void setFileCreator(String filename, int creator) throws IOException { 162 SecurityManager security = System.getSecurityManager(); 163 if (security != null) { 164 security.checkWrite(filename); 165 } 166 _setFileCreator(filename, creator); 167 } 168 private static native void _setFileCreator(String filename, int creator) throws IOException; 169 170 /** 171 * Obtains the file {@code type} code for a file or folder. 172 * 173 * @since 1.4 174 */ 175 public static int getFileType(String filename) throws IOException { 176 SecurityManager security = System.getSecurityManager(); 177 if (security != null) { 178 security.checkRead(filename); 179 } 180 return _getFileType(filename); 181 } 182 private static native int _getFileType(String filename) throws IOException; 183 184 /** 185 * Obtains the file {@code creator} code for a file or folder. 186 * 187 * @since 1.4 188 */ 189 public static int getFileCreator(String filename) throws IOException { 190 SecurityManager security = System.getSecurityManager(); 191 if (security != null) { 192 security.checkRead(filename); 193 } 194 return _getFileCreator(filename); 195 } 196 private static native int _getFileCreator(String filename) throws IOException; 197 198 199 /** 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 than 203 * 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 certain 206 * domain use {@code findFolder(short domain, int folderType)} or 207 * {@code findFolder(short domain, int folderType, boolean createIfNeeded)}. 208 * 209 * @return the path to the folder searched for 210 * 211 * @since 1.4 212 */ 213 public static String findFolder(int folderType) throws FileNotFoundException { 214 return findFolder(kOnAppropriateDisk, folderType); 215 } 216 217 /** 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 system 225 * 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 for 233 * 234 * @since 1.4 235 */ 236 public static String findFolder(short domain, int folderType) throws FileNotFoundException { 237 return findFolder(domain, folderType, false); 238 } 239 240 /** 241 * Locates a folder of a particular type within a given domain and optionally creating the folder if it does 242 * not exist. The behavior is similar to {@code findFolder(int folderType)} and 243 * {@code findFolder(short domain, int folderType)} except that it can create the folder if it does not already exist. 244 * 245 * @param createIfNeeded 246 * set to {@code true}, by setting to {@code false} the behavior will be the 247 * same as {@code findFolder(short domain, int folderType, boolean createIfNeeded)} 248 * @return the path to the folder searched for 249 * 250 * @since 1.4 251 */ 252 public static String findFolder(short domain, int folderType, boolean createIfNeeded) throws FileNotFoundException { 253 final SecurityManager security = System.getSecurityManager(); 254 if (security != null) { 255 security.checkPermission(new RuntimePermission("canExamineFileSystem")); 256 } 257 258 final String foundFolder = _findFolder(domain, folderType, createIfNeeded); 259 if (foundFolder == null) throw new FileNotFoundException("Can't find folder: " + Integer.toHexString(folderType)); 260 return foundFolder; 261 } 262 private static native String _findFolder(short domain, int folderType, boolean createIfNeeded); 263 264 265 /** 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://}) and 268 * FTP URL's ({@code ftp://}) open in the Finder. Note that opening an FTP URL will prompt the user for where 269 * they want to save the downloaded file(s). 270 * 271 * @param url 272 * the URL for the file you want to open, it can either be an HTTP, FTP, or file url 273 * 274 * @deprecated this functionality has been superseded by java.awt.Desktop.browse() and java.awt.Desktop.open() 275 * 276 * @since 1.4 277 */ 278 @Deprecated 279 public static void openURL(String url) throws IOException { 280 SecurityManager security = System.getSecurityManager(); 281 if (security != null) { 282 security.checkPermission(new RuntimePermission("canOpenURLs")); 283 } 284 _openURL(url); 285 } 286 private static native void _openURL(String url) throws IOException; 287 288 /** |