Package com.helger.commons.io.resource
Class ClassPathResource
java.lang.Object
com.helger.commons.io.resource.ClassPathResource
- All Implemented Interfaces:
IHasInputStream,IHasInputStreamAndReader,IReadableResource,IResourceBase,IHasClassLoader
@NotThreadSafe
public class ClassPathResource
extends Object
implements IReadableResource, IHasClassLoader
Wraps a class path object as a readable resource.
- Author:
- Philip Helger
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionClassPathResource(String sPath) Create a new class path resource, using the specified path.ClassPathResource(String sPath, ClassLoader aClassLoader) Create a new class path resource using the specified path and class loader (optional).ClassPathResource(URL aURL) Create a new class path resource, using the specified URL.ClassPathResource(URL aURL, ClassLoader aClassLoader) Create a new class path resource using the specified URL and class loader (optional). -
Method Summary
Modifier and TypeMethodDescriptionbooleancanRead()booleancanReadNoCache(ClassLoader aClassLoader) booleanbooleanexists()booleanstatic FileGet the file for the specified path using automatic class loader handling.static FilegetAsFile(String sPath, ClassLoader aClassLoader) getAsFileNoCache(ClassLoader aClassLoader) getAsURL()getAsURLNoCache(ClassLoader aClassLoader) Convert the path to a URL without using caching.Get the input stream for the specified path using automatic class loader handling.static InputStreamgetInputStream(String sPath) Get the input stream for the specified path using automatic class loader handling.static InputStreamgetInputStream(String sPath, ClassLoader aClassLoader) Get the input stream of the passed resource using the specified class loader only.getInputStreamNoCache(ClassLoader aClassLoader) Get the input stream to the this resource, using the passed class loader only.getPath()getReadableCloneForPath(String sPath) Get a new resource of the same implementation type as this object but for a different path.getReaderNoCache(ClassLoader aClassLoader, Charset aCharset) Create aReaderof this resource, using the specified class loader only.static StringgetWithoutClassPathPrefix(String sPath) Remove any leading explicit classpath resource prefixes.inthashCode()static booleanCheck if the passed resource name is an explicit classpath resource.final booleanCheck if theInputStreamfromIHasInputStream.getInputStream()andIHasInputStream.getBufferedInputStream()can be acquired more than once.toString()Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface com.helger.commons.lang.IHasClassLoader
hasClassLoaderMethods inherited from interface com.helger.commons.io.IHasInputStream
getBufferedInputStream, withBufferedInputStreamDo, withInputStreamDoMethods inherited from interface com.helger.commons.io.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
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
- Specified by:
getClassLoaderin interfaceIHasClassLoader- Returns:
- The class loader to use. May be
null.
-
getResourceID
- Specified by:
getResourceIDin interfaceIResourceBase- Returns:
- A non-
nullresource ID used e.g. for system IDs in XML resolving.
-
getPath
- Specified by:
getPathin interfaceIResourceBase- Returns:
- The requested path. Never
null
-
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
@Nullable public static InputStream getInputStream(@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 interfaceIHasInputStream- Returns:
nullif no such resource exists.
-
isReadMultiple
public final boolean isReadMultiple()Description copied from interface:IHasInputStreamCheck if theInputStreamfromIHasInputStream.getInputStream()andIHasInputStream.getBufferedInputStream()can be acquired more than once.- Specified by:
isReadMultiplein interfaceIHasInputStream- Returns:
trueif the input stream can be acquired more than once,falseif not.
-
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
@Nullable public 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()- Specified by:
existsin interfaceIResourceBase- Returns:
trueif the resource exists,falseotherwise.
-
existsNoCacheUsage
public boolean existsNoCacheUsage() -
getAsURL
- Specified by:
getAsURLin interfaceIResourceBase- Returns:
- the URL representation of this resource. May be
nullif this resource cannot be represented as an URL.
-
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
-
getAsFile
- Specified by:
getAsFilein interfaceIResourceBase- Returns:
- the File representation of this resource. May be
nullif this resource cannot be represented as a file.
-
getAsFileNoCache
-
canRead
public boolean canRead() -
canReadNoCache
-
getReadableCloneForPath
Description copied from interface:IReadableResourceGet a new resource of the same implementation type as this object but for a different path.- Specified by:
getReadableCloneForPathin interfaceIReadableResource- Parameters:
sPath- The new path to use. May not benull.- Returns:
- The resource of the same implementation but a different path. May
not be
null.
-
equals
-
hashCode
public int hashCode() -
toString
-