Top Description Constructors Methods
org.springframework.core.io

public abstract Class AbstractResource

extends Object
implements Resource
Class Inheritance
All Implemented Interfaces
org.springframework.core.io.Resource, org.springframework.core.io.InputStreamSource
Known Direct Subclasses
org.springframework.core.io.ByteArrayResource, org.springframework.core.io.DescriptiveResource, org.springframework.core.io.FileSystemResource, org.springframework.core.io.InputStreamResource, org.springframework.core.io.ModuleResource, org.springframework.core.io.PathResource, org.springframework.core.io.VfsResource, org.springframework.core.io.AbstractFileResolvingResource
Imports
java.io.File, .FileNotFoundException, .IOException, .InputStream, java.net.URI, .URISyntaxException, .URL, java.nio.channels.Channels, .ReadableByteChannel, java.util.function.Supplier, org.apache.commons.logging.Log, .LogFactory, org.springframework.lang.Nullable, org.springframework.util.ResourceUtils

Convenience base class for Resource implementations, pre-implementing typical behavior.

The "exists" method will check whether a File or InputStream can be opened; "isOpen" will always return false; "getURL" and "getFile" throw an exception; and "toString" will return the description.

Authors
Juergen Hoeller, Sam Brannen
Since
28.12.2003

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public long
contentLength()

Implements org.springframework.core.io.Resource.contentLength.

This method reads the entire InputStream to determine the content length.
public Resource
createRelative(String
the relative path (relative to this resource)
relativePath
)

Implements org.springframework.core.io.Resource.createRelative.

This implementation throws a FileNotFoundException, assuming that relative resources cannot be created for this resource.
private void
debug(Supplier<String> message, Throwable ex)

Lazily access the logger for debug logging in case of an exception.

public boolean
equals(Object
the reference object with which to compare.
other
)

Overrides java.lang.Object.equals.

This implementation compares description strings.
public boolean
exists()

Implements org.springframework.core.io.Resource.exists.

This implementation checks whether a File can be opened, falling back to whether an InputStream can be opened.
public File
getFile()

Implements org.springframework.core.io.Resource.getFile.

This implementation throws a FileNotFoundException, assuming that the resource cannot be resolved to an absolute file path.
protected File

Returns:

the File to use for timestamp checking (never null)
getFileForLastModifiedCheck
()

Determine the File to use for timestamp checking.

public String
getFilename()

Implements org.springframework.core.io.Resource.getFilename.

This implementation always returns null, assuming that this resource type does not have a filename.
public URI
getURI()

Implements org.springframework.core.io.Resource.getURI.

This implementation builds a URI based on the URL returned by getURL().
public URL
getURL()

Implements org.springframework.core.io.Resource.getURL.

This implementation throws a FileNotFoundException, assuming that the resource cannot be resolved to a URL.
public int
hashCode()

Overrides java.lang.Object.hashCode.

This implementation returns the description's hash code.
public boolean
isFile()

Overrides default org.springframework.core.io.Resource.isFile.

This implementation always returns false.
public boolean
isOpen()

Overrides default org.springframework.core.io.Resource.isOpen.

This implementation always returns false.
public boolean
isReadable()

Overrides default org.springframework.core.io.Resource.isReadable.

This implementation always returns true for a resource that exists (revised as of 5.1).
public long
lastModified()

Implements org.springframework.core.io.Resource.lastModified.

This implementation checks the timestamp of the underlying File, if available.
public ReadableByteChannel
public String
toString()

Overrides java.lang.Object.toString.

This implementation returns the description of this resource.
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Constructor Detail

AbstractResourceback to summary
public AbstractResource()

Method Detail

contentLengthback to summary
public long contentLength() throws IOException

Implements org.springframework.core.io.Resource.contentLength.

This method reads the entire InputStream to determine the content length.

For a custom subclass of InputStreamResource, we strongly recommend overriding this method with a more optimal implementation, e.g. checking File length, or possibly simply returning -1 if the stream can only be read once.

Annotations
@Override
Exceptions
IOException:

Doc from org.springframework.core.io.Resource.contentLength.

if the resource cannot be resolved (in the file system or as some other known physical resource type)

See Also
getInputStream()
createRelativeback to summary
public Resource createRelative(String relativePath) throws IOException

Implements org.springframework.core.io.Resource.createRelative.

This implementation throws a FileNotFoundException, assuming that relative resources cannot be created for this resource.

Parameters
relativePath:String

Doc from org.springframework.core.io.Resource.createRelative.

the relative path (relative to this resource)

Returns:Resource

Doc from org.springframework.core.io.Resource.createRelative.

the resource handle for the relative resource

Annotations
@Override
Exceptions
IOException:

Doc from org.springframework.core.io.Resource.createRelative.

if the relative resource cannot be determined

debugback to summary
private void debug(Supplier<String> message, Throwable ex)

Lazily access the logger for debug logging in case of an exception.

equalsback to summary
public boolean equals(Object other)

Overrides java.lang.Object.equals.

This implementation compares description strings.

Parameters
other:Object

Doc from java.lang.Object.equals.

the reference object with which to compare.

Returns:boolean

Doc from java.lang.Object.equals.

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
See Also
getDescription()
existsback to summary
public boolean exists()

Implements org.springframework.core.io.Resource.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.

Annotations
@Override
getFileback to summary
public File getFile() throws IOException

Implements org.springframework.core.io.Resource.getFile.

This implementation throws a FileNotFoundException, assuming that the resource cannot be resolved to an absolute file path.

Annotations
@Override
Exceptions
IOException:

Doc from org.springframework.core.io.Resource.getFile.

in case of general resolution/reading failures

getFileForLastModifiedCheckback to summary
protected File getFileForLastModifiedCheck() throws IOException

Determine the File to use for timestamp checking.

The default implementation delegates to getFile().

Returns:File

the File to use for timestamp checking (never null)

Exceptions
IOException:
in case of general resolution/reading failures
FileNotFoundException:
if the resource cannot be resolved as an absolute file path, i.e. is not available in a file system
getFilenameback to summary
public String getFilename()

Implements org.springframework.core.io.Resource.getFilename.

This implementation always returns null, assuming that this resource type does not have a filename.

Annotations
@Override
@Nullable
getURIback to summary
public URI getURI() throws IOException

Implements org.springframework.core.io.Resource.getURI.

This implementation builds a URI based on the URL returned by getURL().

Annotations
@Override
Exceptions
IOException:

Doc from org.springframework.core.io.Resource.getURI.

if the resource cannot be resolved as URI, i.e. if the resource is not available as a descriptor

getURLback to summary
public URL getURL() throws IOException

Implements org.springframework.core.io.Resource.getURL.

This implementation throws a FileNotFoundException, assuming that the resource cannot be resolved to a URL.

Annotations
@Override
Exceptions
IOException:

Doc from org.springframework.core.io.Resource.getURL.

if the resource cannot be resolved as URL, i.e. if the resource is not available as a descriptor

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

This implementation returns the description's hash code.

Returns:int

Doc from java.lang.Object.hashCode.

a hash code value for this object.

Annotations
@Override
See Also
getDescription()
isFileback to summary
public boolean isFile()

Overrides default org.springframework.core.io.Resource.isFile.

This implementation always returns false.

Annotations
@Override
isOpenback to summary
public boolean isOpen()

Overrides default org.springframework.core.io.Resource.isOpen.

This implementation always returns false.

Annotations
@Override
isReadableback to summary
public boolean isReadable()

Overrides default org.springframework.core.io.Resource.isReadable.

This implementation always returns true for a resource that exists (revised as of 5.1).

Annotations
@Override
lastModifiedback to summary
public long lastModified() throws IOException

Implements org.springframework.core.io.Resource.lastModified.

This implementation checks the timestamp of the underlying File, if available.

Annotations
@Override
Exceptions
IOException:

Doc from org.springframework.core.io.Resource.lastModified.

if the resource cannot be resolved (in the file system or as some other known physical resource type)

See Also
getFileForLastModifiedCheck()
readableChannelback to summary
public ReadableByteChannel readableChannel() throws IOException

Overrides default org.springframework.core.io.Resource.readableChannel.

This implementation returns Channels#newChannel(InputStream) with the result of getInputStream().

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

Returns:ReadableByteChannel

Doc from org.springframework.core.io.Resource.readableChannel.

the byte channel for the underlying resource (must not be null)

Annotations
@Override
Exceptions
IOException:

Doc from org.springframework.core.io.Resource.readableChannel.

if the content channel could not be opened

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

This implementation returns the description of this resource.

Returns:String

Doc from java.lang.Object.toString.

a string representation of the object.

Annotations
@Override
See Also
getDescription()