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 Modifier and Type Field Description static StringCLASSPATH_PREFIX_LONGUse this prefix to uniquely identify classpath resourcesstatic StringCLASSPATH_PREFIX_SHORTUse this prefix to uniquely identify classpath resources - alternative
-
Constructor Summary
Constructors Constructor Description ClassPathResource(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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancanRead()booleancanReadNoCache(ClassLoader aClassLoader)booleanequals(Object o)booleanexists()booleanexistsNoCacheUsage()FilegetAsFile()static FilegetAsFile(String sPath)Get the file for the specified path using automatic class loader handling.static FilegetAsFile(String sPath, ClassLoader aClassLoader)FilegetAsFileNoCache(ClassLoader aClassLoader)URLgetAsURL()URLgetAsURLNoCache(ClassLoader aClassLoader)Convert the path to a URL without using caching.ClassLoadergetClassLoader()InputStreamgetInputStream()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.InputStreamgetInputStreamNoCache(ClassLoader aClassLoader)Get the input stream to the this resource, using the passed class loader only.StringgetPath()ClassPathResourcegetReadableCloneForPath(String sPath)Get a new resource of the same implementation type as this object but for a different path.ReadergetReaderNoCache(ClassLoader aClassLoader, Charset aCharset)Create aReaderof this resource, using the specified class loader only.StringgetResourceID()static StringgetWithoutClassPathPrefix(String sPath)Remove any leading explicit classpath resource prefixes.inthashCode()static booleanisExplicitClassPathResource(String sName)Check if the passed resource name is an explicit classpath resource.booleanisReadMultiple()Check if theInputStreamfromIHasInputStream.getInputStream()andIHasInputStream.getBufferedInputStream()can be acquired more than once.StringtoString()-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface com.helger.commons.lang.IHasClassLoader
hasClassLoader
-
Methods inherited from interface com.helger.commons.io.IHasInputStream
getBufferedInputStream
-
Methods inherited from interface com.helger.commons.io.IHasInputStreamAndReader
getBufferedReader, getReader
-
-
-
-
Field Detail
-
CLASSPATH_PREFIX_LONG
public static final String CLASSPATH_PREFIX_LONG
Use this prefix to uniquely identify classpath resources- See Also:
- Constant Field Values
-
CLASSPATH_PREFIX_SHORT
public static final String CLASSPATH_PREFIX_SHORT
Use this prefix to uniquely identify classpath resources - alternative- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ClassPathResource
public ClassPathResource(@Nonnull URL aURL)
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
public ClassPathResource(@Nonnull URL aURL, @Nullable ClassLoader aClassLoader)
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
public ClassPathResource(@Nonnull @Nonempty String sPath)
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(@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 Detail
-
getWithoutClassPathPrefix
@Nullable public static String getWithoutClassPathPrefix(@Nullable String sPath)
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:
CLASSPATH_PREFIX_LONG,CLASSPATH_PREFIX_SHORT
-
isExplicitClassPathResource
public static boolean isExplicitClassPathResource(@Nullable String sName)
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
@Nullable public ClassLoader getClassLoader()
- Specified by:
getClassLoaderin interfaceIHasClassLoader- Returns:
- The class loader to use. May be
null.
-
getResourceID
@Nonnull public String getResourceID()
- Specified by:
getResourceIDin interfaceIResourceBase- Returns:
- A non-
nullresource ID used e.g. for system IDs in XML resolving.
-
getPath
@Nonnull @Nonempty public String getPath()
- Specified by:
getPathin interfaceIResourceBase- Returns:
- The requested path. Never
null
-
getInputStream
@Nullable public static InputStream getInputStream(@Nonnull @Nonempty String sPath)
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
@Nullable public InputStream 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
@Nullable public InputStream getInputStreamNoCache(@Nonnull ClassLoader aClassLoader)
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
@Nullable public URL getAsURL()
- Specified by:
getAsURLin interfaceIResourceBase- Returns:
- the URL representation of this resource. May be
nullif this resource cannot be represented as an URL.
-
getAsURLNoCache
@Nullable public URL getAsURLNoCache(@Nonnull ClassLoader aClassLoader)
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
@Nullable public static File getAsFile(@Nonnull @Nonempty String sPath)
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
@Nullable public static File getAsFile(@Nonnull @Nonempty String sPath, @Nonnull ClassLoader aClassLoader)
-
getAsFile
@Nullable public File getAsFile()
- Specified by:
getAsFilein interfaceIResourceBase- Returns:
- the File representation of this resource. May be
nullif this resource cannot be represented as a file.
-
getAsFileNoCache
@Nullable public File getAsFileNoCache(@Nonnull ClassLoader aClassLoader)
-
canRead
public boolean canRead()
-
canReadNoCache
public boolean canReadNoCache(@Nonnull ClassLoader aClassLoader)
-
getReadableCloneForPath
@Nonnull public ClassPathResource getReadableCloneForPath(@Nonnull String sPath)
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.
-
-