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 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(@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(@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

      @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 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

      @Nullable public ClassLoader getClassLoader()
      Specified by:
      getClassLoader in interface IHasClassLoader
      Returns:
      The class loader to use. May be null.
    • getResourceID

      @Nonnull public String getResourceID()
      Specified by:
      getResourceID in interface IResourceBase
      Returns:
      A non-null resource ID used e.g. for system IDs in XML resolving.
    • getPath

      @Nonnull @Nonempty public String getPath()
      Specified by:
      getPath in interface IResourceBase
      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:
      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

      @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 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

      @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:
      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 IHasInputStream
      Returns:
      null if no such resource exists.
    • isReadMultiple

      public final boolean isReadMultiple()
      Description copied from interface: IHasInputStream
      Check if the InputStream from IHasInputStream.getInputStream() and IHasInputStream.getBufferedInputStream() can be acquired more than once.
      Specified by:
      isReadMultiple in interface IHasInputStream
      Returns:
      true if the input stream can be acquired more than once, false if 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 be null.
      Returns:
      null if the path could not be resolved.
    • getReaderNoCache

      @Nullable public 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()
      Specified by:
      exists in interface IResourceBase
      Returns:
      true if the resource exists, false otherwise.
    • existsNoCacheUsage

      public boolean existsNoCacheUsage()
    • getAsURL

      @Nullable public URL getAsURL()
      Specified by:
      getAsURL in interface IResourceBase
      Returns:
      the URL representation of this resource. May be null if 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 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

      @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:
      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

      @Nullable public static File getAsFile(@Nonnull @Nonempty String sPath, @Nonnull ClassLoader aClassLoader)
    • getAsFile

      @Nullable public File getAsFile()
      Specified by:
      getAsFile in interface IResourceBase
      Returns:
      the File representation of this resource. May be null if 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: IReadableResource
      Get a new resource of the same implementation type as this object but for a different path.
      Specified by:
      getReadableCloneForPath in interface IReadableResource
      Parameters:
      sPath - The new path to use. May not be null.
      Returns:
      The resource of the same implementation but a different path. May not be 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