com.mucommander.file.util
Class ResourceLoader

java.lang.Object
  extended by com.mucommander.file.util.ResourceLoader

public class ResourceLoader
extends java.lang.Object

This class provides methods to load resources located within reach of a ClassLoader. Those resources can reside either in a JAR file or in a local directory -- all methods of this class are agnostic to either type of location.

The ResourceAsURL and ResourceAsStream methods are akin to those of java.lang.Class and java.lang.ClassLoader, albeit easier to work with. But the real fun lies in the ResourceAsFile methods which allow to manipulate resources as regular files -- again, whether they be in a regular directory or in a JAR file. Finally, the getRootPackageAsFile(Class) method allows to list and manipulate the resource files contained in a particular classpath's location, including the .class files.

Author:
Maxence Bernard

Constructor Summary
ResourceLoader()
           
 
Method Summary
static java.lang.ClassLoader getDefaultClassLoader()
          Returns the default ClassLoader that is used by methods without a ClassLoader argument.
static AbstractFile getResourceAsFile(java.lang.String path)
          Finds the resource with the given path and returns an AbstractFile that gives full access to it, or null if the resource couldn't be located.
static AbstractFile getResourceAsFile(java.lang.String path, java.lang.ClassLoader classLoader)
          Finds the resource with the given path and returns an AbstractFile that gives full access to it, or null if the resource couldn't be located.
static java.io.InputStream getResourceAsStream(java.lang.String path)
          Finds the resource with the given path and returns an InputStream to read from it, or null if the resource couldn't be located.
static java.io.InputStream getResourceAsStream(java.lang.String path, java.lang.ClassLoader classLoader)
          Finds the resource with the given path and returns an InputStream to read from it, or null The given ClassLoader is used for locating the resource.
static java.net.URL getResourceAsURL(java.lang.String path)
          Finds the resource with the given path and returns a URL pointing to its location, or null if the resource couldn't be located.
static java.net.URL getResourceAsURL(java.lang.String path, java.lang.ClassLoader classLoader)
          Finds the resource with the given path and returns a URL pointing to its location, or null if the resource couldn't be located.
static AbstractFile getRootPackageAsFile(java.lang.Class aClass)
          Returns an AbstractFile to the root package of the given Class.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ResourceLoader

public ResourceLoader()
Method Detail

getDefaultClassLoader

public static java.lang.ClassLoader getDefaultClassLoader()
Returns the default ClassLoader that is used by methods without a ClassLoader argument. This default ClassLoader is the one that loaded this class, and not the system ClassLoader.

Returns:
the default ClassLoader that is used by methods without a ClassLoader argument

getResourceAsURL

public static java.net.URL getResourceAsURL(java.lang.String path)
Finds the resource with the given path and returns a URL pointing to its location, or null if the resource couldn't be located. The ClassLoader returned by getDefaultClassLoader() is used for locating the resource.

The given path may or may not start with a leading slash character ('/'), this doesn't affect the way it is interpreted.

Parameters:
path - a path to the resource, relative to the default ClassLoader's classpath
Returns:
a URL pointing to the resource, or null if the resource couldn't be located

getResourceAsURL

public static java.net.URL getResourceAsURL(java.lang.String path,
                                            java.lang.ClassLoader classLoader)
Finds the resource with the given path and returns a URL pointing to its location, or null if the resource couldn't be located. The given ClassLoader is used for locating the resource. If it is null, the ClassLoader returned by getDefaultClassLoader() is used.

The given path may or may not start with a leading slash character ('/'), this doesn't affect the way it is interpreted.

Parameters:
path - a path to the resource, relative to the given ClassLoader's classpath
classLoader - the ClassLoader used for locating the resource
Returns:
a URL pointing to the resource, or null if the resource couldn't be located

getResourceAsStream

public static java.io.InputStream getResourceAsStream(java.lang.String path)
Finds the resource with the given path and returns an InputStream to read from it, or null if the resource couldn't be located. The ClassLoader returned by getDefaultClassLoader() is used for locating the resource.

The given path may or may not start with a leading slash character ('/'), this doesn't affect the way it is interpreted.

Parameters:
path - a path to the resource, relative to the default ClassLoader's classpath
Returns:
an InputStream that allows to read from the resource, or null if the resource couldn't be located

getResourceAsStream

public static java.io.InputStream getResourceAsStream(java.lang.String path,
                                                      java.lang.ClassLoader classLoader)
Finds the resource with the given path and returns an InputStream to read from it, or null The given ClassLoader is used for locating the resource. If it is null, the ClassLoader returned by getDefaultClassLoader() is used.

The given path may or may not start with a leading slash character ('/'), this doesn't affect the way it is interpreted.

Parameters:
path - a path to the resource, relative to the given ClassLoader's classpath
classLoader - the Class whose ClassLoader is used for locating the resource
Returns:
an InputStream that allows to read from the resource, or null if the resource couldn't be located

getResourceAsFile

public static AbstractFile getResourceAsFile(java.lang.String path)
Finds the resource with the given path and returns an AbstractFile that gives full access to it, or null if the resource couldn't be located. The ClassLoader returned by getDefaultClassLoader() is used for locating the resource.

The given path may or may not start with a leading slash character ('/'), this doesn't affect the way it is interpreted. Also noteworthy is this method may be slower than getResourceAsStream(String) if the resource is located inside a JAR file, because the Zip file headers will have to be parsed the first time the archive is accessed. So this latter method should be favored if the file is simply used for reading the resource.

Parameters:
path - a path to the resource, relative to the default ClassLoader's classpath
Returns:
an AbstractFile that allows to access the resource, or null if the resource couldn't be located

getResourceAsFile

public static AbstractFile getResourceAsFile(java.lang.String path,
                                             java.lang.ClassLoader classLoader)
Finds the resource with the given path and returns an AbstractFile that gives full access to it, or null if the resource couldn't be located. The given ClassLoader is used for locating the resource. If it is null, the ClassLoader returned by getDefaultClassLoader() is used.

The given path may or may not start with a leading slash character ('/'), this doesn't affect the way it is interpreted. Also noteworthy is this method may be slower than getResourceAsStream(String) if the resource is located inside a JAR file, because the Zip file headers will have to be parsed the first time the archive is accessed. So this latter method should be favored if the file is simply used for reading the resource.

Parameters:
path - a path to the resource, relative to the given ClassLoader's classpath
classLoader - the ClassLoader is used for locating the resource
Returns:
an AbstractFile that allows to access the resource, or null if the resource couldn't be located

getRootPackageAsFile

public static AbstractFile getRootPackageAsFile(java.lang.Class aClass)
Returns an AbstractFile to the root package of the given Class. For example, if the specified Class is java.lang.Object's, the returned file will be the Java runtime JAR file, which on most platforms is $JAVA_HOME/lib/jre/rt.jar.
The returned file can be used to list or manipulate all resource files contained in a particular classpath's location, including the .class files.

Parameters:
aClass - the class for which to locate the root package.
Returns:
an AbstractFile to the root package of the given Class


This file is part of muCommander - Copyright (C) 2002-2008 Maxence Bernard