Package spark.resource
Class AbstractResource
java.lang.Object
spark.resource.AbstractResource
- All Implemented Interfaces:
InputStreamResource,Resource
- Direct Known Subclasses:
AbstractFileResolvingResource
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.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionlongThis implementation reads the entire InputStream to calculate the content length.createRelative(String relativePath) This implementation throws a FileNotFoundException, assuming that relative resources cannot be created for this resource.booleanThis implementation compares description strings.booleanexists()This implementation checks whether a File can be opened, falling back to whether an InputStream can be opened.getFile()This implementation throws a FileNotFoundException, assuming that the resource cannot be resolved to an absolute file path.protected FileDetermine the File to use for timestamp checking.This implementation always returnsnull, assuming that this resource type does not have a filename.getURI()This implementation builds a URI based on the URL returned bygetURL().getURL()This implementation throws a FileNotFoundException, assuming that the resource cannot be resolved to a URL.inthashCode()This implementation returns the description's hash code.booleanisOpen()This implementation always returnsfalse.booleanThis implementation always returnstrue.longThis implementation checks the timestamp of the underlying File, if available.toString()This implementation returns the description of this resource.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface spark.resource.InputStreamResource
getInputStreamMethods inherited from interface spark.resource.Resource
getDescription
-
Constructor Details
-
AbstractResource
public AbstractResource()
-
-
Method Details
-
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. -
isReadable
public boolean isReadable()This implementation always returnstrue.- Specified by:
isReadablein interfaceResource- Returns:
- if readable
- See Also:
-
isOpen
public boolean isOpen()This implementation always returnsfalse. -
getURL
This implementation throws a FileNotFoundException, assuming that the resource cannot be resolved to a URL.- Specified by:
getURLin interfaceResource- Returns:
- a URL handle for this resource.
- Throws:
IOException- if the resource cannot be resolved as URL, i.e. if the resource is not available as descriptor
-
getURI
This implementation builds a URI based on the URL returned bygetURL().- Specified by:
getURIin interfaceResource- Returns:
- a URI handle for this resource.
- Throws:
IOException- if the resource cannot be resolved as URI, i.e. if the resource is not available as descriptor
-
getFile
This implementation throws a FileNotFoundException, assuming that the resource cannot be resolved to an absolute file path.- Specified by:
getFilein interfaceResource- Returns:
- a File handle for this resource.
- Throws:
IOException- if the resource cannot be resolved as absolute file path, i.e. if the resource is not available in a file system
-
contentLength
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:
contentLengthin interfaceResource- Returns:
- the content length for this resource.
- Throws:
IllegalStateException- ifInputStreamResource.getInputStream()returns null.IOException- if the resource cannot be resolved (in the file system or as some other known physical resource type)- See Also:
-
lastModified
This implementation checks the timestamp of the underlying File, if available.- Specified by:
lastModifiedin interfaceResource- Returns:
- the last-modified timestamp for this resource.
- Throws:
IOException- if the resource cannot be resolved (in the file system or as some other known physical resource type)- See Also:
-
getFileForLastModifiedCheck
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:
IOException- if the resource cannot be resolved as absolute file path, i.e. if the resource is not available in a file system
-
createRelative
This implementation throws a FileNotFoundException, assuming that relative resources cannot be created for this resource.- Specified by:
createRelativein interfaceResource- Parameters:
relativePath- the relative path (relative to this resource)- Returns:
- the resource handle for the relative resource
- Throws:
IOException- if the relative resource cannot be determined
-
getFilename
This implementation always returnsnull, assuming that this resource type does not have a filename.- Specified by:
getFilenamein interfaceResource- Returns:
- the file name.
-
toString
This implementation returns the description of this resource. -
equals
This implementation compares description strings. -
hashCode
public int hashCode()This implementation returns the description's hash code.
-