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:
027: package com.sun.midp.installer;
028:
029: import java.lang.String;
030:
031: import java.io.IOException;
032:
033: import com.sun.midp.midlet.MIDletStateHandler;
034: import com.sun.midp.midlet.MIDletSuite;
035:
036: import com.sun.midp.security.SecurityToken;
037: import com.sun.midp.security.Permissions;
038:
039: import com.sun.midp.io.Util;
040:
041: /**
042: * This class provides a Java API for reading an entry from a Jar file stored
043: * on the file system.
044: */
045: public class JarReader {
046: /**
047: * Returns the content of the given entry in the JAR file on the
048: * file system given by jarFilePath.
049: *
050: * @param securityToken token with permission to install software
051: * @param jarFilePath file pathname of the JAR file to read. May
052: * be a relative pathname.
053: * @param entryName name of the entry to return.
054: *
055: * @return the content of the given entry in a byte array or null if
056: * the entry was not found
057: *
058: * @exception IOException if JAR is corrupt or not found
059: * @exception IOException if the entry does not exist.
060: * @exception SecurityException if the caller does not have permission
061: * to install software.
062: */
063: public static byte[] readJarEntry(SecurityToken securityToken,
064: String jarFilePath, String entryName) throws IOException {
065:
066: if (securityToken == null) {
067: MIDletStateHandler midletStateHandler = MIDletStateHandler
068: .getMidletStateHandler();
069: MIDletSuite midletSuite = midletStateHandler
070: .getMIDletSuite();
071:
072: midletSuite.checkIfPermissionAllowed(Permissions.AMS);
073: } else {
074: securityToken.checkIfPermissionAllowed(Permissions.AMS);
075: }
076:
077: if (entryName.charAt(0) == '/') {
078: /*
079: * Strip off the leading directory separator, or the
080: * resource will not be found in the JAR.
081: */
082: entryName = entryName.substring(1, entryName.length());
083: }
084:
085: return readJarEntry0(jarFilePath, entryName);
086: }
087:
088: /**
089: * Returns the content of the given entry in the JAR file on the
090: * file system given by jarFilePath.
091: *
092: * @param jarFilePath file pathname of the JAR file to read. May
093: * be a relative pathname.
094: * @param entryName name of the entry to return.
095: *
096: * @return the content of the given entry in a byte array or null if
097: * the entry was not found
098: *
099: * @exception IOException if JAR is corrupt or not found
100: * @exception IOException if the entry does not exist.
101: * @exception SecurityException if the caller does not have permission
102: * to install software.
103: */
104: public static byte[] readJarEntry(String jarFilePath,
105: String entryName) throws IOException {
106:
107: return readJarEntry(null, jarFilePath, entryName);
108: }
109:
110: /**
111: * Performs the same function as readJarEntry.
112: *
113: * @param localJarFilePath file pathname of the JAR file to read. May
114: * be a relative pathname.
115: * @param localEntryName name of the entry to return.
116: *
117: * @return the content of the given entry in a byte array or null if
118: * the entry was not found
119: *
120: * @exception IOException if JAR is corrupt or not found
121: */
122: private static native byte[] readJarEntry0(String localJarFilePath,
123: String localEntryName) throws IOException;
124: }
|