Applets, Beans, Applications, and Libraries often have resource files associated with them. These Applets, Beans, and so on, want to use these files in a way that is independent of where these Applets, et al., are located.
<P> Existing code in JDK1.0.x uses two types of mechanisms. The first
mechanism is used in applets. <EM>Applet.getCodeBase()</EM> returns a
URL to the base of the code for the applet. This base can be
extended with a "relative path" to point to the desired resource, which
can then be loaded (for example using
<EM>Applet.getAudioClip(url)</EM>).
The second mechanism is used by applications. Applications use
"well known locations" (such as <EM>System.getProperty("user.home")</EM>
or <EM>System.getProperty("java.home"))</EM>. They add "/lib/<resource>" to the location and then open that file.
<P> Until now, JDK1.0.x has not had a mechanism to locate resources that are
independent of the code. That is, JDK1.0.x. has not had the means to locate resources for an applet loaded from the net using
multiple http connects, or for an applet loaded using JAR files, or for a Bean
loaded or a Bean installed in the CLASSPATH, or for a "library" installed in
the CLASSPATH, and so on. The APIs described here provide such a mechanism.
<P> The I18N APIs use this API as a primitive operation to locate
<EM>ResourceBundle</EM>s. See the latest I18N documentation for details.
<H2>Resources, names, and contexts</H2>
<P>A resource is identified by a String. This String, while possibly empty, is a <B>/</B>-separated sequence of substrings, each a valid Java<font size="-2"><sup>TM</sup></font>
Identifier, followed by a name of the form "<shortName>" or
"<shortName>.<extension>". Both "shortName" and
"extension" are composed of valid Java Letters and Numbers (section
3.8 in JLS). If the optional sequence exists, it is separated from
the "shortName" by a <B>/</B>.
<P>The name of a resource is independent of the Java implementation;
in particular, the <B>/</B> is always used as a separator.
However, the Java implementation controls the details of how the
contents of the resource are mapped into a file, database, or other
object containing the actual resource.
<P>The interpretation of a resource name is relative to a ClassLoader
instance. Methods implemented by the ClassLoader do this interpretation.
<H2>System Resources</H2>
<P>A system resource is similar to a system class (section 20.14.5 of
the JLS). A system resource is a resource that is either built-in to the system, or it is kept by the host implementation in, for example, a local
file system. System resources are accessed through special
methods (<EM>getSystemResource</EM> and
<EM>getSystemResourceAsStream</EM>) that consult the base
host implementation.
<P>For example, in a particular implementation, locating a system
resource may involve searching the entries in the CLASSPATH.
Each directory, zip file, or jar file entry in the CLASSPATH is searched for the resource file, and, if found, either an InputStream, or its name,
is returned.
If not found, null is returned. Note that a resource may be found in
a different entry in the CLASSPATH than where the class file was loaded.
<H2>Non-System Resources</H2>
The implementation of <EM>getResource</EM> on a given <EM>ClassLoader</EM>
will depend on the details of the ClassLoader. For example AppletClassLoader
will:
<UL>
<LI>First try to locate the resource as a system resource; then, if not found,
<LI>Search through the resources in ARCHIVES (JAR files) already
loaded in this CODEBASE; then, if not found,
<LI>Use CODEBASE and attempt to locate the resource (which may involve
contacting a remote site).
</UL>
<P> All ClassLoaders will search for a resource first as a system
resource, in a manner analogous to searcing for class files. This
search rule permits overwriting locally any resource. Clients should
choose a resource name that will be unique (using the company or package
name as a prefix, for instance).
<H2>Resource Names</H2>
<P> A common convention for the name of a resource used by a class is
to use the fully qualified name of the package of the class, convert
all "." to "/", and add a resource name of the form "<Name>.<ext>".
To support this, and to simplify handling the details of system
classes (for which <EM>getClassLoader</EM> returns <B>null</B>),
the class Class provides two convenience methods that call the
appropriate methods in <EM>ClassLoader</EM>.
<P> The resource name given to a Class method may have an initial
starting "/" that identifies it as an "absolute" name.
Resource names that do not start with a "/" are "relative".
<P>Absolute names are stripped of their starting "/" and are passed,
without any further modification, to the appropriate ClassLoader
method to locate the resource.
Relative names are modified according to the convention described
previously and then are passed to a ClassLoader method.
<H2>Manipulating Resources</H2>
<P> The method <EM>getResource()</EM> returns a URL for the resource.
The URL (and its representation) is implementation and JVM-instance
specific (the URL obtained in one JVM instance may not work in
another) and may vary depending on the implementation details (it may
also change between JDK1.1 and JDK1.1.1).
Its protocol is (usually) specific to the ClassLoader loading the resource.
If the resource does not exist, a null will be returned; if the resource
is not visible due to security considerations, a null will also be
returned.
<P> If the client code wants to read the contents of the resource
as an InputStream, it can apply the <EM>openStream()</EM> method on
the url. This is common enough to justify adding
<EM>getResourceAsStream()</EM> to Class and ClassLoader.
<em>getResourceAsStream()</em> is semantically identical to
<em>getResource().openStream()</em>, except that IO exceptions are
caught and returned as a null <em>InputStream</em>.
<P> The client code code can also request the contents of the
resource as an object by applying the <EM>getContent()</EM> method
on the url. This is useful when the resource contains the data
for an image, for instance. Note that in this case, the result is
an <em>awt.image.ImageProducer</em> object, not an <em>Image</em> object.
<H2>API Additions to Class</H2>
<P>Specifically, the class <EM>Class</EM> methods are of the following
form:
<PRE>
<CODE>
class Class {
/**
* Find a resource with a given name. Will return null if no
* resource with this name is found. The rules for searching a
* resources associated with a given class are implemented by the
* ClassLoader of the class.
*
* The Class methods delegate to ClassLoader methods, after applying
* a naming convention: if the resource name starts with "/", it is used
* as is. Otherwise, the name of the package is prepended, after
* converting "." to "/".
*
* @see java.lang.ClassLoader
*/
public InputStream getResourceAsStream(String name) {