Class ClassPathResource

    • 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 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 Detail

      • 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.
      • 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.
      • 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 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.
      • canRead

        public boolean canRead()
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object