org.springframework.core.io

Class AbstractResource

  • java.lang.Object
    • org.springframework.core.io.AbstractResource
    • Constructor Summary

      Constructors 
      Constructor and Description
      AbstractResource() 
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method and Description
      long contentLength()
      This implementation reads the entire InputStream to calculate the content length.
      Resource createRelative(java.lang.String relativePath)
      This implementation throws a FileNotFoundException, assuming that relative resources cannot be created for this resource.
      boolean equals(java.lang.Object other)
      This implementation compares description strings.
      boolean exists()
      This implementation checks whether a File can be opened, falling back to whether an InputStream can be opened.
      java.io.File getFile()
      This implementation throws a FileNotFoundException, assuming that the resource cannot be resolved to an absolute file path.
      protected java.io.File getFileForLastModifiedCheck()
      Determine the File to use for timestamp checking.
      java.lang.String getFilename()
      This implementation always returns null, assuming that this resource type does not have a filename.
      java.net.URI getURI()
      This implementation builds a URI based on the URL returned by getURL().
      java.net.URL getURL()
      This implementation throws a FileNotFoundException, assuming that the resource cannot be resolved to a URL.
      int hashCode()
      This implementation returns the description's hash code.
      boolean isFile()
      This implementation always returns false.
      boolean isOpen()
      This implementation always returns false.
      boolean isReadable()
      This implementation always returns true for a resource that exists (revised as of 5.1).
      long lastModified()
      This implementation checks the timestamp of the underlying File, if available.
      java.nio.channels.ReadableByteChannel readableChannel()
      This implementation returns Channels.newChannel(InputStream) with the result of InputStreamSource.getInputStream().
      java.lang.String toString()
      This implementation returns the description of this resource.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • AbstractResource

        public AbstractResource()
    • Method Detail

      • exists

        public boolean exists()
        This implementation checks whether a File can be opened, falling back to whether an InputStream can be opened. This will cover both directories and content resources.
        Specified by:
        exists in interface Resource
      • isOpen

        public boolean isOpen()
        This implementation always returns false.
        Specified by:
        isOpen in interface Resource
      • getURL

        public java.net.URL getURL()
                            throws java.io.IOException
        This implementation throws a FileNotFoundException, assuming that the resource cannot be resolved to a URL.
        Specified by:
        getURL in interface Resource
        Throws:
        java.io.IOException - if the resource cannot be resolved as URL, i.e. if the resource is not available as descriptor
      • getURI

        public java.net.URI getURI()
                            throws java.io.IOException
        This implementation builds a URI based on the URL returned by getURL().
        Specified by:
        getURI in interface Resource
        Throws:
        java.io.IOException - if the resource cannot be resolved as URI, i.e. if the resource is not available as descriptor
      • getFile

        public java.io.File getFile()
                             throws java.io.IOException
        This implementation throws a FileNotFoundException, assuming that the resource cannot be resolved to an absolute file path.
        Specified by:
        getFile in interface Resource
        Throws:
        java.io.FileNotFoundException - if the resource cannot be resolved as absolute file path, i.e. if the resource is not available in a file system
        java.io.IOException - in case of general resolution/reading failures
        See Also:
        InputStreamSource.getInputStream()
      • readableChannel

        public java.nio.channels.ReadableByteChannel readableChannel()
                                                              throws java.io.IOException
        This implementation returns Channels.newChannel(InputStream) with the result of InputStreamSource.getInputStream().

        This is the same as in Resource's corresponding default method but mirrored here for efficient JVM-level dispatching in a class hierarchy.

        Specified by:
        readableChannel in interface Resource
        Returns:
        the byte channel for the underlying resource (must not be null)
        Throws:
        java.io.FileNotFoundException - if the underlying resource doesn't exist
        java.io.IOException - if the content channel could not be opened
        See Also:
        InputStreamSource.getInputStream()
      • contentLength

        public long contentLength()
                           throws java.io.IOException
        This implementation reads the entire InputStream to calculate the content length. Subclasses will almost always be able to provide a more optimal version of this, e.g. checking a File length.
        Specified by:
        contentLength in interface Resource
        Throws:
        java.io.IOException - if the resource cannot be resolved (in the file system or as some other known physical resource type)
        See Also:
        InputStreamSource.getInputStream()
      • lastModified

        public long lastModified()
                          throws java.io.IOException
        This implementation checks the timestamp of the underlying File, if available.
        Specified by:
        lastModified in interface Resource
        Throws:
        java.io.IOException - if the resource cannot be resolved (in the file system or as some other known physical resource type)
        See Also:
        getFileForLastModifiedCheck()
      • getFileForLastModifiedCheck

        protected java.io.File getFileForLastModifiedCheck()
                                                    throws java.io.IOException
        Determine the File to use for timestamp checking.

        The default implementation delegates to getFile().

        Returns:
        the File to use for timestamp checking (never null)
        Throws:
        java.io.FileNotFoundException - if the resource cannot be resolved as an absolute file path, i.e. is not available in a file system
        java.io.IOException - in case of general resolution/reading failures
      • createRelative

        public Resource createRelative(java.lang.String relativePath)
                                throws java.io.IOException
        This implementation throws a FileNotFoundException, assuming that relative resources cannot be created for this resource.
        Specified by:
        createRelative in interface Resource
        Parameters:
        relativePath - the relative path (relative to this resource)
        Returns:
        the resource handle for the relative resource
        Throws:
        java.io.IOException - if the relative resource cannot be determined
      • getFilename

        @Nullable
        public java.lang.String getFilename()
        This implementation always returns null, assuming that this resource type does not have a filename.
        Specified by:
        getFilename in interface Resource
      • equals

        public boolean equals(java.lang.Object other)
        This implementation compares description strings.
        Overrides:
        equals in class java.lang.Object
        See Also:
        Resource.getDescription()
      • hashCode

        public int hashCode()
        This implementation returns the description's hash code.
        Overrides:
        hashCode in class java.lang.Object
        See Also:
        Resource.getDescription()
      • toString

        public java.lang.String toString()
        This implementation returns the description of this resource.
        Overrides:
        toString in class java.lang.Object
        See Also:
        Resource.getDescription()