Package com.helger.io.resource
Class ClassPathResource
java.lang.Object
com.helger.io.resource.ClassPathResource
- All Implemented Interfaces:
com.helger.base.classloader.IHasClassLoader,com.helger.base.io.iface.IHasInputStream,com.helger.base.io.iface.IHasInputStreamAndReader,IReadableResource,IResourceBase
@NotThreadSafe
public class ClassPathResource
extends Object
implements IReadableResource, com.helger.base.classloader.IHasClassLoader
Wraps a class path object as a readable resource.
- Author:
- Philip Helger
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionClassPathResource(@NonNull @Nonempty String sPath) Create a new class path resource, using the specified path.ClassPathResource(@NonNull @Nonempty String sPath, @Nullable ClassLoader aClassLoader) Create a new class path resource using the specified path and class loader (optional).ClassPathResource(@NonNull URL aURL) Create a new class path resource, using the specified URL.ClassPathResource(@NonNull URL aURL, @Nullable ClassLoader aClassLoader) Create a new class path resource using the specified URL and class loader (optional). -
Method Summary
Modifier and TypeMethodDescriptionbooleancanRead()Check if this classpath resource can be read.booleancanReadNoCache(@NonNull ClassLoader aClassLoader) Check if this classpath resource can be read using the specified class loader without caching.booleanbooleanexists()Check if this classpath resource exists by trying to resolve its URL.booleanCheck if this classpath resource exists by resolving the URL without using cached results.@Nullable FileGet this classpath resource as aFile.static @Nullable FileGet the file for the specified path using automatic class loader handling.static @Nullable FilegetAsFile(@NonNull @Nonempty String sPath, @NonNull ClassLoader aClassLoader) Get the file for the specified classpath resource using the provided class loader.@Nullable FilegetAsFileNoCache(@NonNull ClassLoader aClassLoader) Get this classpath resource as aFilewithout using cached URLs.@Nullable URLgetAsURL()Get this classpath resource as a URL.@Nullable URLgetAsURLNoCache(@NonNull ClassLoader aClassLoader) Convert the path to a URL without using caching.@Nullable ClassLoaderGet the class loader associated with this resource.@Nullable InputStreamGet the input stream for the specified path using automatic class loader handling.static @Nullable InputStreamgetInputStream(@NonNull @Nonempty String sPath) Get the input stream for the specified path using automatic class loader handling.static @Nullable InputStreamgetInputStream(@NonNull @Nonempty String sPath, @NonNull ClassLoader aClassLoader) Get the input stream of the passed resource using the specified class loader only.@Nullable InputStreamgetInputStreamNoCache(@NonNull ClassLoader aClassLoader) Get the input stream to the this resource, using the passed class loader only.@NonNull @Nonempty StringgetPath()Get the classpath-relative path of this resource.@NonNull ClassPathResourcegetReadableCloneForPath(@NonNull String sPath) Create a newClassPathResourcefor a different path using the same class loader as this resource.@Nullable ReadergetReaderNoCache(@NonNull ClassLoader aClassLoader, @NonNull Charset aCharset) Create aReaderof this resource, using the specified class loader only.@NonNull StringGet the unique resource ID of this classpath resource.static @Nullable StringgetWithoutClassPathPrefix(@Nullable String sPath) Remove any leading explicit classpath resource prefixes.inthashCode()static booleanisExplicitClassPathResource(@Nullable String sName) Check if the passed resource name is an explicit classpath resource.final booleanCheck if this resource can be read multiple times.toString()Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface com.helger.base.classloader.IHasClassLoader
hasClassLoaderMethods inherited from interface com.helger.base.io.iface.IHasInputStream
getBufferedInputStream, withBufferedInputStreamDo, withInputStreamDoMethods inherited from interface com.helger.base.io.iface.IHasInputStreamAndReader
getBufferedReader, getReader
-
Field Details
-
CLASSPATH_PREFIX_LONG
Use this prefix to uniquely identify classpath resources- See Also:
-
CLASSPATH_PREFIX_SHORT
Use this prefix to uniquely identify classpath resources - alternative- See Also:
-
-
Constructor Details
-
ClassPathResource
Create a new class path resource, using the specified URL. Class loader handling is automatic.- Parameters:
aURL- The URL to be used. May not benull.
-
ClassPathResource
Create a new class path resource using the specified URL and class loader (optional).- Parameters:
aURL- The URL to be used. May not benull.aClassLoader- The class loader to use. May benullindicating that automatic class loader handling should be applied.
-
ClassPathResource
Create a new class path resource, using the specified path. Class loader handling is automatic.- Parameters:
sPath- The path to be used. May neither benullnor empty.
-
ClassPathResource
public ClassPathResource(@Nonempty @NonNull @Nonempty String sPath, @Nullable ClassLoader aClassLoader) Create a new class path resource using the specified path and class loader (optional).- Parameters:
sPath- The path to be used. May neither benullnor empty.aClassLoader- The class loader to use. May benullindicating that automatic class loader handling should be applied.
-
-
Method Details
-
getWithoutClassPathPrefix
Remove any leading explicit classpath resource prefixes.- Parameters:
sPath- The source path to strip the class path prefixes from. May benull.- Returns:
nullif the parameter wasnull.- See Also:
-
isExplicitClassPathResource
Check if the passed resource name is an explicit classpath resource. This is the case, if the name starts either withCLASSPATH_PREFIX_LONGorCLASSPATH_PREFIX_SHORT.- Parameters:
sName- The name to check. May benull.- Returns:
trueif the passed name is notnulland an explicit classpath resource.
-
getClassLoader
Get the class loader associated with this resource.- Specified by:
getClassLoaderin interfacecom.helger.base.classloader.IHasClassLoader- Returns:
nullif no specific class loader was provided or if it has been garbage collected.
-
getResourceID
Get the unique resource ID of this classpath resource. If the URL could be resolved, the URL external form is returned; otherwise the path is returned.- Specified by:
getResourceIDin interfaceIResourceBase- Returns:
- The resource ID. Never
null.
-
getPath
Get the classpath-relative path of this resource.- Specified by:
getPathin interfaceIResourceBase- Returns:
- The path. Neither
nullnor empty.
-
getInputStream
Get the input stream for the specified path using automatic class loader handling. The class loaders are iterated in the following order:- Default class loader (usually the context class loader)
- The class loader of this class
- The system class loader
- Parameters:
sPath- The path to be resolved. May neither benullnor empty.- Returns:
nullif the path could not be resolved.
-
getInputStream
public static @Nullable InputStream getInputStream(@Nonempty @NonNull @Nonempty String sPath, @NonNull ClassLoader aClassLoader) Get the input stream of the passed resource using the specified class loader only.- Parameters:
sPath- The path to be resolved. May neither benullnor empty.aClassLoader- The class loader to be used. May not benull.- Returns:
nullif the path could not be resolved using the specified class loader.
-
getInputStream
Get the input stream for the specified path using automatic class loader handling. If no class loader was specified in the constructor, the class loaders are iterated in the following order:- Default class loader (usually the context class loader)
- The class loader of this class
- The system class loader
- Specified by:
getInputStreamin interfacecom.helger.base.io.iface.IHasInputStream- Returns:
nullif no such resource exists.
-
isReadMultiple
public final boolean isReadMultiple()Check if this resource can be read multiple times. Classpath resources always returntrue.- Specified by:
isReadMultiplein interfacecom.helger.base.io.iface.IHasInputStream- Returns:
- Always
true.
-
getInputStreamNoCache
Get the input stream to the this resource, using the passed class loader only.- Parameters:
aClassLoader- The class loader to be used. May not benull.- Returns:
nullif the path could not be resolved.
-
getReaderNoCache
public @Nullable Reader getReaderNoCache(@NonNull ClassLoader aClassLoader, @NonNull Charset aCharset) Create aReaderof this resource, using the specified class loader only.- Parameters:
aClassLoader- The class loader to be used. May not benull.aCharset- The charset to be used for theReader. May not benull.- Returns:
nullif the path could not be resolved.
-
exists
public boolean exists()Check if this classpath resource exists by trying to resolve its URL.- Specified by:
existsin interfaceIResourceBase- Returns:
trueif the resource exists,falseotherwise.
-
existsNoCacheUsage
public boolean existsNoCacheUsage()Check if this classpath resource exists by resolving the URL without using cached results.- Returns:
trueif the resource exists,falseotherwise.
-
getAsURL
Get this classpath resource as a URL. The result is cached after the first resolution.- Specified by:
getAsURLin interfaceIResourceBase- Returns:
nullif the resource could not be resolved.
-
getAsURLNoCache
Convert the path to a URL without using caching. Otherwise the resolution ofgetAsURL()using the constructor supplied class loader would possibly contradict with this resolution.- Parameters:
aClassLoader- The class loader to be used. May not benull.- Returns:
nullif the path could not be resolved to a URL
-
getAsFile
Get the file for the specified path using automatic class loader handling. The class loaders are iterated in the following order:- Default class loader (usually the context class loader)
- The class loader of this class
- The system class loader
- Parameters:
sPath- The path to be resolved. May neither benullnor empty.- Returns:
nullif the path could not be resolved.
-
getAsFile
public static @Nullable File getAsFile(@Nonempty @NonNull @Nonempty String sPath, @NonNull ClassLoader aClassLoader) Get the file for the specified classpath resource using the provided class loader.- Parameters:
sPath- The path to be resolved. May neither benullnor empty.aClassLoader- The class loader to be used. May not benull.- Returns:
nullif the path could not be resolved.
-
getAsFile
Get this classpath resource as aFile. Uses the cached URL if available.- Specified by:
getAsFilein interfaceIResourceBase- Returns:
nullif the resource could not be resolved to a file.
-
getAsFileNoCache
Get this classpath resource as aFilewithout using cached URLs.- Parameters:
aClassLoader- The class loader to be used. May not benull.- Returns:
nullif the resource could not be resolved to a file.
-
canRead
public boolean canRead()Check if this classpath resource can be read.- Returns:
trueif the resource URL can be resolved,falseotherwise.
-
canReadNoCache
Check if this classpath resource can be read using the specified class loader without caching.- Parameters:
aClassLoader- The class loader to be used. May not benull.- Returns:
trueif the resource URL can be resolved,falseotherwise.
-
getReadableCloneForPath
Create a newClassPathResourcefor a different path using the same class loader as this resource.- Specified by:
getReadableCloneForPathin interfaceIReadableResource- Parameters:
sPath- The new path to use. May not benull.- Returns:
- A new
ClassPathResourceinstance. Nevernull.
-
equals
-
hashCode
public int hashCode() -
toString
-