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
    Modifier and Type
    Field
    Description
    static final String
    Use this prefix to uniquely identify classpath resources
    static final String
    Use this prefix to uniquely identify classpath resources - alternative
  • Constructor Summary

    Constructors
    Constructor
    Description
    ClassPathResource(@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 Type
    Method
    Description
    boolean
    Check if this classpath resource can be read.
    boolean
    canReadNoCache(@NonNull ClassLoader aClassLoader)
    Check if this classpath resource can be read using the specified class loader without caching.
    boolean
     
    boolean
    Check if this classpath resource exists by trying to resolve its URL.
    boolean
    Check if this classpath resource exists by resolving the URL without using cached results.
    @Nullable File
    Get this classpath resource as a File.
    static @Nullable File
    getAsFile(@NonNull @Nonempty String sPath)
    Get the file for the specified path using automatic class loader handling.
    static @Nullable File
    getAsFile(@NonNull @Nonempty String sPath, @NonNull ClassLoader aClassLoader)
    Get the file for the specified classpath resource using the provided class loader.
    @Nullable File
    getAsFileNoCache(@NonNull ClassLoader aClassLoader)
    Get this classpath resource as a File without using cached URLs.
    @Nullable URL
    Get this classpath resource as a URL.
    @Nullable URL
    getAsURLNoCache(@NonNull ClassLoader aClassLoader)
    Convert the path to a URL without using caching.
    @Nullable ClassLoader
    Get the class loader associated with this resource.
    @Nullable InputStream
    Get the input stream for the specified path using automatic class loader handling.
    static @Nullable InputStream
    getInputStream(@NonNull @Nonempty String sPath)
    Get the input stream for the specified path using automatic class loader handling.
    static @Nullable InputStream
    getInputStream(@NonNull @Nonempty String sPath, @NonNull ClassLoader aClassLoader)
    Get the input stream of the passed resource using the specified class loader only.
    @Nullable InputStream
    getInputStreamNoCache(@NonNull ClassLoader aClassLoader)
    Get the input stream to the this resource, using the passed class loader only.
    @NonNull @Nonempty String
    Get the classpath-relative path of this resource.
    Create a new ClassPathResource for a different path using the same class loader as this resource.
    @Nullable Reader
    getReaderNoCache(@NonNull ClassLoader aClassLoader, @NonNull Charset aCharset)
    Create a Reader of this resource, using the specified class loader only.
    @NonNull String
    Get the unique resource ID of this classpath resource.
    static @Nullable String
    Remove any leading explicit classpath resource prefixes.
    int
     
    static boolean
    Check if the passed resource name is an explicit classpath resource.
    final boolean
    Check if this resource can be read multiple times.
     

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface com.helger.base.classloader.IHasClassLoader

    hasClassLoader

    Methods inherited from interface com.helger.base.io.iface.IHasInputStream

    getBufferedInputStream, withBufferedInputStreamDo, withInputStreamDo

    Methods inherited from interface com.helger.base.io.iface.IHasInputStreamAndReader

    getBufferedReader, getReader
  • Field Details

    • CLASSPATH_PREFIX_LONG

      public static final String CLASSPATH_PREFIX_LONG
      Use this prefix to uniquely identify classpath resources
      See Also:
    • CLASSPATH_PREFIX_SHORT

      public static final String CLASSPATH_PREFIX_SHORT
      Use this prefix to uniquely identify classpath resources - alternative
      See Also:
  • Constructor Details

    • 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 be null.
    • 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 be null.
      aClassLoader - The class loader to use. May be null indicating that automatic class loader handling should be applied.
    • ClassPathResource

      public ClassPathResource(@Nonempty @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 be null nor 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 be null nor empty.
      aClassLoader - The class loader to use. May be null indicating that automatic class loader handling should be applied.
  • Method Details

    • getWithoutClassPathPrefix

      public static @Nullable 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 be null.
      Returns:
      null if the parameter was null.
      See Also:
    • 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 with CLASSPATH_PREFIX_LONG or CLASSPATH_PREFIX_SHORT.
      Parameters:
      sName - The name to check. May be null.
      Returns:
      true if the passed name is not null and an explicit classpath resource.
    • getClassLoader

      public @Nullable ClassLoader getClassLoader()
      Get the class loader associated with this resource.
      Specified by:
      getClassLoader in interface com.helger.base.classloader.IHasClassLoader
      Returns:
      null if no specific class loader was provided or if it has been garbage collected.
    • getResourceID

      public @NonNull String 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:
      getResourceID in interface IResourceBase
      Returns:
      The resource ID. Never null.
    • getPath

      @Nonempty public @NonNull @Nonempty String getPath()
      Get the classpath-relative path of this resource.
      Specified by:
      getPath in interface IResourceBase
      Returns:
      The path. Neither null nor empty.
    • getInputStream

      public static @Nullable InputStream getInputStream(@Nonempty @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:
      1. Default class loader (usually the context class loader)
      2. The class loader of this class
      3. The system class loader
      Parameters:
      sPath - The path to be resolved. May neither be null nor empty.
      Returns:
      null if 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 be null nor empty.
      aClassLoader - The class loader to be used. May not be null.
      Returns:
      null if the path could not be resolved using the specified class loader.
    • getInputStream

      public @Nullable 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:
      1. Default class loader (usually the context class loader)
      2. The class loader of this class
      3. The system class loader
      Specified by:
      getInputStream in interface com.helger.base.io.iface.IHasInputStream
      Returns:
      null if no such resource exists.
    • isReadMultiple

      public final boolean isReadMultiple()
      Check if this resource can be read multiple times. Classpath resources always return true.
      Specified by:
      isReadMultiple in interface com.helger.base.io.iface.IHasInputStream
      Returns:
      Always true.
    • getInputStreamNoCache

      public @Nullable 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 be null.
      Returns:
      null if the path could not be resolved.
    • getReaderNoCache

      public @Nullable Reader getReaderNoCache(@NonNull ClassLoader aClassLoader, @NonNull Charset aCharset)
      Create a Reader of this resource, using the specified class loader only.
      Parameters:
      aClassLoader - The class loader to be used. May not be null.
      aCharset - The charset to be used for the Reader. May not be null.
      Returns:
      null if the path could not be resolved.
    • exists

      public boolean exists()
      Check if this classpath resource exists by trying to resolve its URL.
      Specified by:
      exists in interface IResourceBase
      Returns:
      true if the resource exists, false otherwise.
    • existsNoCacheUsage

      public boolean existsNoCacheUsage()
      Check if this classpath resource exists by resolving the URL without using cached results.
      Returns:
      true if the resource exists, false otherwise.
    • getAsURL

      public @Nullable URL getAsURL()
      Get this classpath resource as a URL. The result is cached after the first resolution.
      Specified by:
      getAsURL in interface IResourceBase
      Returns:
      null if the resource could not be resolved.
    • getAsURLNoCache

      public @Nullable URL getAsURLNoCache(@NonNull ClassLoader aClassLoader)
      Convert the path to a URL without using caching. Otherwise the resolution of getAsURL() using the constructor supplied class loader would possibly contradict with this resolution.
      Parameters:
      aClassLoader - The class loader to be used. May not be null.
      Returns:
      null if the path could not be resolved to a URL
    • getAsFile

      public static @Nullable File getAsFile(@Nonempty @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:
      1. Default class loader (usually the context class loader)
      2. The class loader of this class
      3. The system class loader
      Parameters:
      sPath - The path to be resolved. May neither be null nor empty.
      Returns:
      null if 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 be null nor empty.
      aClassLoader - The class loader to be used. May not be null.
      Returns:
      null if the path could not be resolved.
    • getAsFile

      public @Nullable File getAsFile()
      Get this classpath resource as a File. Uses the cached URL if available.
      Specified by:
      getAsFile in interface IResourceBase
      Returns:
      null if the resource could not be resolved to a file.
    • getAsFileNoCache

      public @Nullable File getAsFileNoCache(@NonNull ClassLoader aClassLoader)
      Get this classpath resource as a File without using cached URLs.
      Parameters:
      aClassLoader - The class loader to be used. May not be null.
      Returns:
      null if the resource could not be resolved to a file.
    • canRead

      public boolean canRead()
      Check if this classpath resource can be read.
      Returns:
      true if the resource URL can be resolved, false otherwise.
    • canReadNoCache

      public boolean canReadNoCache(@NonNull ClassLoader aClassLoader)
      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 be null.
      Returns:
      true if the resource URL can be resolved, false otherwise.
    • getReadableCloneForPath

      public @NonNull ClassPathResource getReadableCloneForPath(@NonNull String sPath)
      Create a new ClassPathResource for a different path using the same class loader as this resource.
      Specified by:
      getReadableCloneForPath in interface IReadableResource
      Parameters:
      sPath - The new path to use. May not be null.
      Returns:
      A new ClassPathResource instance. Never null.
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object