Package spark.resource
Class AbstractResource
- java.lang.Object
-
- spark.resource.AbstractResource
-
- All Implemented Interfaces:
InputStreamResource
,Resource
- Direct Known Subclasses:
AbstractFileResolvingResource
public abstract class AbstractResource extends java.lang.Object implements Resource
Convenience base class forResource
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 Constructor Description AbstractResource()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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 obj)
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 returnsnull
, assuming that this resource type does not have a filename.java.net.URI
getURI()
This implementation builds a URI based on the URL returned bygetURL()
.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
isOpen()
This implementation always returnsfalse
.boolean
isReadable()
This implementation always returnstrue
.long
lastModified()
This implementation checks the timestamp of the underlying File, if available.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
-
Methods inherited from interface spark.resource.InputStreamResource
getInputStream
-
Methods inherited from interface spark.resource.Resource
getDescription
-
-
-
-
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.
-
isReadable
public boolean isReadable()
This implementation always returnstrue
.- Specified by:
isReadable
in interfaceResource
- Returns:
- if readable
- See Also:
InputStreamResource.getInputStream()
-
isOpen
public boolean isOpen()
This implementation always returnsfalse
.
-
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.
-
getURI
public java.net.URI getURI() throws java.io.IOException
This implementation builds a URI based on the URL returned bygetURL()
.
-
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.
-
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 interfaceResource
- Returns:
- the content length for this resource.
- Throws:
java.lang.IllegalStateException
- ifInputStreamResource.getInputStream()
returns null.java.io.IOException
- if the resource cannot be resolved (in the file system or as some other known physical resource type)- See Also:
InputStreamResource.getInputStream()
-
lastModified
public long lastModified() throws java.io.IOException
This implementation checks the timestamp of the underlying File, if available.- Specified by:
lastModified
in interfaceResource
- Returns:
- the last-modified timestamp for this 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.IOException
- if the resource cannot be resolved as absolute file path, i.e. if the resource is not available in a file system
-
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 interfaceResource
- 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
public java.lang.String getFilename()
This implementation always returnsnull
, assuming that this resource type does not have a filename.- Specified by:
getFilename
in interfaceResource
- Returns:
- the file name.
-
toString
public java.lang.String toString()
This implementation returns the description of this resource.- Overrides:
toString
in classjava.lang.Object
- See Also:
Resource.getDescription()
-
equals
public boolean equals(java.lang.Object obj)
This implementation compares description strings.- Overrides:
equals
in classjava.lang.Object
- See Also:
Resource.getDescription()
-
hashCode
public int hashCode()
This implementation returns the description's hash code.- Overrides:
hashCode
in classjava.lang.Object
- See Also:
Resource.getDescription()
-
-