Class ClasspathResource

  • All Implemented Interfaces:
    Resource, Serializable

    public class ClasspathResource
    extends BaseResource

    A Resource implementation for resources that exist on the classpath. Resources are loaded either using the supplied class loader or the system class loader if none is supplied.

    Author:
    David B. Bracewell
    See Also:
    Serialized Form
    • Constructor Detail

      • ClasspathResource

        public ClasspathResource​(String resource)
        Constructs a ClasspathResource resource with a given charset, compression and encoding setting
        Parameters:
        resource - The resource
      • ClasspathResource

        public ClasspathResource​(String resource,
                                 @NonNull
                                 @NonNull ClassLoader classLoader)

        Creates a classpath resource that uses the given class loader to load the resource.

        Parameters:
        resource - The path to the resource.
        classLoader - The class loader to use.
    • Method Detail

      • append

        public Resource append​(byte[] byteArray)
                        throws IOException
        Description copied from interface: Resource

        Appends the given byte array content to the resource.

        Parameters:
        byteArray - The content to append
        Returns:
        the resource
        Throws:
        IOException - the io exception
      • asURL

        public Optional<URL> asURL()
        Description copied from interface: Resource
        Gets the resource as a URL.
        Returns:
        A URL representing the resource.
      • baseName

        public String baseName()
        Description copied from interface: Resource
        Gets the name (file name or directory name) of this resource.
        Specified by:
        baseName in interface Resource
        Overrides:
        baseName in class BaseResource
        Returns:
        The name of the file or directory
      • canRead

        public boolean canRead()
        Description copied from interface: Resource
        Returns true if the resource is readable, false if not.
        Specified by:
        canRead in interface Resource
        Overrides:
        canRead in class BaseResource
        Returns:
        True if can read from the resource
      • canWrite

        public boolean canWrite()
        Description copied from interface: Resource
        Returns true if the resource is writable, false if not.
        Specified by:
        canWrite in interface Resource
        Overrides:
        canWrite in class BaseResource
        Returns:
        True if can write to the resource
      • exists

        public boolean exists()
        Description copied from interface: Resource
        Exists boolean.
        Returns:
        True if the resource exists, False if the resource does not exist.
      • getChild

        public Resource getChild​(String relativePath)
        Description copied from interface: Resource

        Creates a new Resource that is relative to this resource.

        Parameters:
        relativePath - The relative path for the new resource.
        Returns:
        A new resource that is relative to this resource.
      • getChildren

        public List<Resource> getChildren​(Pattern filePattern,
                                          boolean recursive)
        Description copied from interface: Resource

        Lists all the resources that are directly under this resource.

        Parameters:
        filePattern - The file matching pattern
        recursive - Gets all children recursively
        Returns:
        A list of all the resources one level under this resource.
      • getParent

        public Resource getParent()
        Description copied from interface: Resource
        Gets parent.
        Returns:
        The parent resource (directory for file, parent directory for a directory)
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object