org.springframework.core.io
abstract public class: AbstractResource [javadoc |
source]
java.lang.Object
org.springframework.core.io.AbstractResource
All Implemented Interfaces:
Resource
Direct Known Subclasses:
ByteArrayResource, InputStreamResource, FileSystemContextResource, ServletContextResource, PortletContextResource, ClassPathResource, FileSystemResource, BeanDefinitionResource, DescriptiveResource, UrlResource, ClassPathContextResource
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.
- author:
Juergen - Hoeller
- since:
28.12.2003 -
| Method from org.springframework.core.io.AbstractResource Summary: |
|---|
|
createRelative, equals, exists, getFile, getFileForLastModifiedCheck, getFilename, getURI, getURL, hashCode, isOpen, isReadable, lastModified, toString |
| Method from org.springframework.core.io.AbstractResource Detail: |
public Resource createRelative(String relativePath) throws IOException {
throw new FileNotFoundException("Cannot create a relative resource for " + getDescription());
}
This implementation throws a FileNotFoundException, assuming
that relative resources cannot be created for this resource. |
public boolean equals(Object obj) {
return (obj == this ||
(obj instanceof Resource && ((Resource) obj).getDescription().equals(getDescription())));
}
This implementation compares description strings. |
public boolean exists() {
// Try file existence: can we find the file in the file system?
try {
return getFile().exists();
}
catch (IOException ex) {
// Fall back to stream existence: can we open the stream?
try {
InputStream is = getInputStream();
is.close();
return true;
}
catch (Throwable isEx) {
return false;
}
}
}
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. |
public File getFile() throws IOException {
throw new FileNotFoundException(getDescription() + " cannot be resolved to absolute file path");
}
This implementation throws a FileNotFoundException, assuming
that the resource cannot be resolved to an absolute file path. |
protected File getFileForLastModifiedCheck() throws IOException {
return getFile();
}
|
public String getFilename() throws IllegalStateException {
throw new IllegalStateException(getDescription() + " does not carry a filename");
}
This implementation always throws IllegalStateException,
assuming that the resource does not carry a filename. |
public URI getURI() throws IOException {
URL url = getURL();
try {
return ResourceUtils.toURI(url);
}
catch (URISyntaxException ex) {
throw new NestedIOException("Invalid URI [" + url + "]", ex);
}
}
This implementation builds a URI based on the URL returned
by #getURL() . |
public URL getURL() throws IOException {
throw new FileNotFoundException(getDescription() + " cannot be resolved to URL");
}
This implementation throws a FileNotFoundException, assuming
that the resource cannot be resolved to a URL. |
public int hashCode() {
return getDescription().hashCode();
}
This implementation returns the description's hash code. |
public boolean isOpen() {
return false;
}
This implementation always returns false. |
public boolean isReadable() {
return true;
}
This implementation always returns true. |
public long lastModified() throws IOException {
long lastModified = getFileForLastModifiedCheck().lastModified();
if (lastModified == 0L) {
throw new FileNotFoundException(getDescription() +
" cannot be resolved in the file system for resolving its last-modified timestamp");
}
return lastModified;
}
This implementation checks the timestamp of the underlying File,
if available. |
public String toString() {
return getDescription();
}
This implementation returns the description of this resource. |