java.io.Closeable
, java.lang.AutoCloseable
, ResourceFactory
EmptyResource
, FileResource
, PathResource
, ResourceCollection
, URLResource
public abstract class Resource extends java.lang.Object implements ResourceFactory, java.io.Closeable
This class provides a resource abstraction, where a resource may be a file, a URL or an entry in a jar file.
Modifier and Type | Field | Description |
---|---|---|
static boolean |
__defaultUseCaches |
Constructor | Description |
---|---|
Resource() |
Modifier and Type | Method | Description |
---|---|---|
abstract Resource |
addPath(java.lang.String path) |
Returns the resource contained inside the current resource with the
given name.
|
abstract void |
close() |
Release any temporary resources held by the resource.
|
void |
copyTo(java.io.File destination) |
Copy the Resource to the new destination file.
|
abstract boolean |
delete() |
Deletes the given resource
|
java.lang.String |
encode(java.lang.String uri) |
Deprecated.
use
URIUtil or UrlEncoded instead |
abstract boolean |
exists() |
|
protected void |
finalize() |
|
java.net.URI |
getAlias() |
|
java.util.Collection<Resource> |
getAllResources() |
|
java.lang.Object |
getAssociate() |
|
static boolean |
getDefaultUseCaches() |
|
abstract java.io.File |
getFile() |
File representing the given resource.
|
abstract java.io.InputStream |
getInputStream() |
Input stream to the resource
|
java.lang.String |
getListHTML(java.lang.String base,
boolean parent) |
Get the resource list as a HTML directory listing.
|
abstract java.lang.String |
getName() |
The name of the resource.
|
abstract java.nio.channels.ReadableByteChannel |
getReadableByteChannel() |
Readable ByteChannel for the resource.
|
Resource |
getResource(java.lang.String path) |
Get a resource from within this resource.
|
java.net.URI |
getURI() |
URI representing the resource.
|
abstract java.net.URL |
getURL() |
Deprecated.
use {
getURI() .toURL() instead. |
java.lang.String |
getWeakETag() |
Generate a weak ETag reference for this Resource.
|
java.lang.String |
getWeakETag(java.lang.String suffix) |
|
boolean |
isAlias() |
|
abstract boolean |
isContainedIn(Resource r) |
|
static boolean |
isContainedIn(Resource r,
Resource containingResource) |
|
abstract boolean |
isDirectory() |
|
abstract long |
lastModified() |
Time resource was last modified.
|
abstract long |
length() |
Length of the resource.
|
abstract java.lang.String[] |
list() |
list of resource names contained in the given resource.
|
static Resource |
newClassPathResource(java.lang.String resource) |
Find a classpath resource.
|
static Resource |
newClassPathResource(java.lang.String name,
boolean useCaches,
boolean checkParents) |
Find a classpath resource.
|
static Resource |
newResource(java.io.File file) |
|
static Resource |
newResource(java.lang.String resource) |
Construct a resource from a string.
|
static Resource |
newResource(java.lang.String resource,
boolean useCaches) |
Construct a resource from a string.
|
static Resource |
newResource(java.net.URI uri) |
Construct a resource from a uri.
|
static Resource |
newResource(java.net.URL url) |
Construct a resource from a url.
|
static Resource |
newSystemResource(java.lang.String resource) |
Construct a system resource from a string.
|
void |
release() |
Deprecated.
use
close() |
abstract boolean |
renameTo(Resource dest) |
Rename the given resource
|
void |
setAssociate(java.lang.Object o) |
|
static void |
setDefaultUseCaches(boolean useCaches) |
Change the default setting for url connection caches.
|
static java.net.URL |
toURL(java.io.File file) |
Generate a properly encoded URL from a
File instance. |
void |
writeTo(java.io.OutputStream out,
long start,
long count) |
public static void setDefaultUseCaches(boolean useCaches)
useCaches
- true to enable URL connection caches, false otherwise.public static boolean getDefaultUseCaches()
public static Resource newResource(java.net.URI uri) throws java.net.MalformedURLException
uri
- A URI.java.net.MalformedURLException
- Problem accessing URIpublic static Resource newResource(java.net.URL url)
url
- A URL.public static Resource newResource(java.lang.String resource) throws java.net.MalformedURLException, java.io.IOException
resource
- A URL or filename.java.net.MalformedURLException
- Problem accessing URIjava.io.IOException
public static Resource newResource(java.lang.String resource, boolean useCaches) throws java.net.MalformedURLException, java.io.IOException
resource
- A URL or filename.useCaches
- controls URLConnection cachingjava.net.MalformedURLException
- Problem accessing URIjava.io.IOException
public static Resource newResource(java.io.File file)
public static Resource newSystemResource(java.lang.String resource) throws java.io.IOException
resource
- Resource as string representationjava.io.IOException
- Problem accessing resource.public static Resource newClassPathResource(java.lang.String resource)
resource
- the relative name of the resourcepublic static Resource newClassPathResource(java.lang.String name, boolean useCaches, boolean checkParents)
Class.getResource(String)
method is used to lookup the resource. If it is not
found, then the Loader.getResource(String)
method is used.
If it is still not found, then ClassLoader.getSystemResource(String)
is used.
Unlike ClassLoader.getSystemResource(String)
this method does not check for normal resources.name
- The relative name of the resourceuseCaches
- True if URL caches are to be used.checkParents
- True if forced searching of parent Classloaders is performed to work around
loaders with inverted prioritiespublic static boolean isContainedIn(Resource r, Resource containingResource) throws java.net.MalformedURLException
java.net.MalformedURLException
protected void finalize()
finalize
in class java.lang.Object
public abstract boolean isContainedIn(Resource r) throws java.net.MalformedURLException
java.net.MalformedURLException
public final void release()
close()
public abstract void close()
close
in interface java.lang.AutoCloseable
close
in interface java.io.Closeable
public abstract boolean exists()
public abstract boolean isDirectory()
public abstract long lastModified()
public abstract long length()
@Deprecated public abstract java.net.URL getURL()
getURI()
.toURL() instead.public java.net.URI getURI()
public abstract java.io.File getFile() throws java.io.IOException
java.io.IOException
- if unable to get the resource due to permissionspublic abstract java.lang.String getName()
public abstract java.io.InputStream getInputStream() throws java.io.IOException
java.io.IOException
- if unable to open the input streampublic abstract java.nio.channels.ReadableByteChannel getReadableByteChannel() throws java.io.IOException
java.io.IOException
- if unable to open the readable bytechannel for the resource.public abstract boolean delete() throws java.lang.SecurityException
java.lang.SecurityException
- if unable to delete due to permissionspublic abstract boolean renameTo(Resource dest) throws java.lang.SecurityException
dest
- the destination name for the resourcejava.lang.SecurityException
- if unable to rename due to permissionspublic abstract java.lang.String[] list()
public abstract Resource addPath(java.lang.String path) throws java.io.IOException, java.net.MalformedURLException
path
- The path segment to add, which is not encodedjava.io.IOException
- if unable to resolve the pathjava.net.MalformedURLException
- if the resolution of the path fails because the input path parameter is malformed.public Resource getResource(java.lang.String path)
This method is essentially an alias for addPath(String)
, but without checked exceptions.
This method satisfied the ResourceFactory
interface.
getResource
in interface ResourceFactory
path
- The path to the resourceResourceFactory.getResource(java.lang.String)
@Deprecated public java.lang.String encode(java.lang.String uri)
URIUtil
or UrlEncoded
insteaduri
- the uri to encodepublic java.lang.Object getAssociate()
public void setAssociate(java.lang.Object o)
public boolean isAlias()
public java.net.URI getAlias()
public java.lang.String getListHTML(java.lang.String base, boolean parent) throws java.io.IOException
base
- The base URLparent
- True if the parent directory should be includedjava.io.IOException
- if unable to get the list of resources as HTMLpublic void writeTo(java.io.OutputStream out, long start, long count) throws java.io.IOException
out
- the output stream to write tostart
- First byte to writecount
- Bytes to write or -1 for all of them.java.io.IOException
- if unable to copy the Resource to the outputpublic void copyTo(java.io.File destination) throws java.io.IOException
Will not replace existing destination file.
destination
- the destination file to createjava.io.IOException
- if unable to copy the resourcepublic java.lang.String getWeakETag()
public java.lang.String getWeakETag(java.lang.String suffix)
public java.util.Collection<Resource> getAllResources()
public static java.net.URL toURL(java.io.File file) throws java.net.MalformedURLException
File
instance.file
- Target file.java.net.MalformedURLException
- if unable to convert File to URLCopyright © 1995–2018 Webtide. All rights reserved.