001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026: package com.sun.midp.util;
027:
028: import javax.microedition.io.Connector;
029: import com.sun.midp.io.j2me.storage.File;
030: import com.sun.midp.io.j2me.storage.RandomAccessStream;
031: import com.sun.midp.security.SecurityToken;
032: import com.sun.midp.security.Permissions;
033: import com.sun.midp.configurator.Constants;
034:
035: /**
036: * The ResourceHandler class is a system level utility class.
037: * Its purpose is to return system resources as an array of bytes
038: * based on a unique String identifier. All methods in this utility
039: * class should be protected through use of the SecurityToken.
040: */
041: public class ResourceHandler {
042: /**
043: * Load a resource from the system and return it as a byte array.
044: * This method is used to load AMS icons.
045: *
046: * @param token the SecurityToken to use to grant permission to
047: * execute this method.
048: * @param resource a String identifier which can uniquely describe
049: * the location of the resource to be loaded.
050: * @return a byte[] containing the resource retrieved from the
051: * system. null if the resource could not be found.
052: */
053: public static byte[] getAmsResource(SecurityToken token,
054: String resource) {
055: return getResourceImpl(token, File
056: .getStorageRoot(Constants.INTERNAL_STORAGE_ID)
057: + resource);
058: }
059:
060: /**
061: * Load a resource from the system and return it as a byte array.
062: * This method is used to load system level resources, such as
063: * images, sounds, properties, etc.
064: *
065: * @param token the SecurityToken to use to grant permission to
066: * execute this method.
067: * @param resource a String identifier which can uniquely describe
068: * the location of the resource to be loaded.
069: * @return a byte[] containing the resource retrieved from the
070: * system. null if the resource could not be found.
071: */
072: public static byte[] getSystemResource(SecurityToken token,
073: String resource) {
074: return getResourceImpl(token, File
075: .getConfigRoot(Constants.INTERNAL_STORAGE_ID)
076: + resource);
077: }
078:
079: /**
080: * Load a system image resource from the system and return it as
081: * a byte array. The images are stored in the configuration
082: * directory ($MIDP_HOME/lib).
083: *
084: * @param token the SecurityToken to use to grant permission to
085: * execute this method.
086: * @param imageName name of the image
087: * @return a byte[] containing the resource retrieved from the
088: * system. null if the resource could not be found.
089: * @throws IllegalArgumentException if imageName contains a "/" or "\\",
090: * or imageName is null or imageName is empty
091: */
092: public static byte[] getSystemImageResource(SecurityToken token,
093: String imageName) {
094: byte[] imageData = getAmsResource(token, imageName + ".raw");
095: if (imageData == null) {
096: imageData = getAmsResource(token, imageName + ".png");
097: }
098:
099: return imageData;
100: }
101:
102: /**
103: * Load a resource from the system and return it as a byte array.
104: * This method is used to load system level resources, such as
105: * images, sounds, properties, etc.
106: *
107: * @param token the SecurityToken to use to grant permission to
108: * execute this method.
109: * @param resourceFilename full path to the file containing the resource.
110: * @return a byte[] containing the resource retrieved from the
111: * system. null if the resource could not be found.
112: */
113: private static byte[] getResourceImpl(SecurityToken token,
114: String resourceFilename) {
115: token.checkIfPermissionAllowed(Permissions.MIDP);
116:
117: byte[] resourceBuffer = null;
118: RandomAccessStream stream = new RandomAccessStream(token);
119:
120: try {
121: stream.connect(resourceFilename, Connector.READ);
122: resourceBuffer = new byte[stream.getSizeOf()];
123: stream.readBytes(resourceBuffer, 0, resourceBuffer.length);
124: } catch (java.io.IOException e) {
125: resourceBuffer = null;
126: } finally {
127: try {
128: stream.disconnect();
129: } catch (java.io.IOException ignored) {
130: }
131: }
132:
133: return resourceBuffer;
134: }
135: }
|