FileDocCategorySizeDatePackage
File.javaAPI DocphoneME MR2 API (J2ME)9714Wed May 02 18:00:14 BST 2007com.sun.midp.io.j2me.storage

File

public class File extends Object
Provide the methods to manage files in a device's persistant storage.

Fields Summary
private static final char[]
NUMS
Table to speed up the unicodeToAsciiFilename conversion method.
private static String
storageRoot
Caches the storage root to save repeated native method calls.
private static String
configRoot
Caches the configuration root to save repeated native method calls.
Constructors Summary
public File()
Constructs a file object.

        MIDletSuite midletSuite = Scheduler.getScheduler().getMIDletSuite();

        // if a MIDlet suite is not scheduled, assume the JAM is calling.
        if (midletSuite != null) {
            midletSuite.checkIfPermissionAllowed(Permissions.AMS);
        }
    
public File(com.sun.midp.security.SecurityToken callerSecurityToken)
Constructs a file object.

param
callerSecurityToken security token of the caller

        callerSecurityToken.checkIfPermissionAllowed(Permissions.AMS);
    
Methods Summary
public static java.lang.StringasciiFilenameToUnicode(java.lang.String str)
Perform the reverse conversion of unicodeToAscii().

param
str a string previously returned by escape()
return
the original string before the conversion by escape().

        StringBuffer sbuf = new StringBuffer();

        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (c == '%") {
                int v = 0;

                v <<= 4; v += hexValue(str.charAt(i+1));
                v <<= 4; v += hexValue(str.charAt(i+2));
                v <<= 4; v += hexValue(str.charAt(i+3));
                v <<= 4; v += hexValue(str.charAt(i+4));

                i += 4;
                sbuf.append((char)(v & 0x0000ffff));
            } else if (c == '#") {
		// drop c
	    } else {
                sbuf.append(c);
            }
        }

        return sbuf.toString();
    
private static native longavailableStorage(int storageId)
Gets the approximate number of free storage bytes remaining.

param
storageId ID of the storage to check for available space
return
free storage space remaining, in bytes

public synchronized voiddelete(java.lang.String name)
Remove a file from storage if it exists.

param
name name of the file to delete
exception
IOException if an error occurs during delete

        deleteStorage(name);
    
private static native voiddeleteStorage(java.lang.String filename)
Removes a file from storage.

param
filename storage file to delete
exception
IOException if an error occurs during deletion

public synchronized booleanexists(java.lang.String name)
Returns true if storage file name exists.

param
name name of storage file
return
true if the named storage file exists

        return storageExists(name);
    
public longgetBytesAvailableForFiles(int storageId)
Retrieves the approximate space available to grow or create new storage files.

param
storageId ID of the storage to check for available space
return
approximate number of free bytes in storage

        return availableStorage(storageId);
    
public static java.lang.StringgetConfigRoot(int storageId)
Returns the root to build configuration filenames including an needed file separators, abstracting difference of the file systems of development and device platforms. Note the root is never null.

param
storageId ID of the storage the config root of which should be returned
return
root of any configuration filename for accessing device persistant storage.

        if (configRoot == null) {
            configRoot = initConfigRoot(storageId);
        }

        return configRoot;
    
public static java.lang.StringgetStorageRoot(int storageId)
Returns the root to build storage filenames including an needed file separators, abstracting difference of the file systems of development and device platforms. Note the root is never null.

param
storageId ID of the storage the root of which should be returned
return
root of any filename for accessing device persistant storage.


                                                                     
         
        if (storageRoot == null) {
            storageRoot = initStorageRoot(storageId);
        }

        return storageRoot;
    
private static inthexValue(char c)
A utility method that convert a hex character 0-9a-f to the numerical value represented by this hex char.

param
c the character to convert
return
the number represented by the character. E.g., '0' represents the number 0x0, 'a' represents the number 0x0a, etc.

        if (c >= '0" && c <= '9") {
            return ((int)c) - '0";
        } else {
            return ((int)c) - 'a" + 10;
        }
    
private static native java.lang.StringinitConfigRoot(int storageId)
Initializes the configuration root for this file instance.

param
storageId ID of the storage the config root of which should be returned
return
path of the configuration root

private static native java.lang.StringinitStorageRoot(int storageId)
Initializes storage root for this file instance.

param
storageId ID of the storage the root of which should be returned
return
path of the storage root

public synchronized voidrename(java.lang.String oldName, java.lang.String newName)
Replaces the current name of storage, oldName with newName.

param
oldName original name of storage file
param
newName new name for storage file
exception
IOException if an error occurs during rename

        renameStorage(oldName, newName);
    
private static native voidrenameStorage(java.lang.String oldName, java.lang.String newName)
Renames storage file.

param
oldName old name of storage file
param
newName new name for storage file

private static native booleanstorageExists(java.lang.String filename)
Determines if a storage file matching filename exists.

param
filename storage file to match
return
true if storage indicated by szFilename exists

public static java.lang.StringunicodeToAsciiFilename(java.lang.String str)
Convert a file name into a form that can be safely stored on an ANSI-compatible file system. All characters that are not [A-Za-z0-9] are converted into %uuuu, where uuuu is the hex representation of the character's unicode value. Note even though "_" is allowed it is converted because we use it for for internal purposes. Potential file separators are converted so the native layer does not have deal with sub-directory hierarchies.

param
str a string that may contain any character
return
an equivalent string that contains only the "safe" characters.

        StringBuffer sbuf = new StringBuffer();

        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if ((c >= 'a" && c <= 'z") ||
                (c >= '0" && c <= '9")) {
                sbuf.append(c);
            } else if (c >= 'A" && c <= 'Z") {
		sbuf.append('#");
		sbuf.append(c);
	    } else {
                int v = (int)(c & 0xffff);
                sbuf.append('%");
                sbuf.append(NUMS[(v & 0xf000) >> 12]);
                sbuf.append(NUMS[(v & 0x0f00) >>  8]);
                sbuf.append(NUMS[(v & 0x00f0) >>  4]);
                sbuf.append(NUMS[(v & 0x000f) >>  0]);
            }
        }

        return sbuf.toString();