public abstract class Resource extends Object implements ResourceFactory, 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 and Description |
---|---|
static boolean |
__defaultUseCaches |
Constructor and Description |
---|
Resource() |
Modifier and Type | Method and Description |
---|---|
abstract Resource |
addPath(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(File destination) |
abstract boolean |
delete()
Deletes the given resource
|
String |
encode(String uri)
Deprecated.
|
abstract boolean |
exists()
Returns true if the respresened resource exists.
|
protected void |
finalize() |
URI |
getAlias() |
Collection<Resource> |
getAllResources() |
Object |
getAssociate() |
static boolean |
getDefaultUseCaches() |
abstract File |
getFile()
Returns an File representing the given resource or NULL if this
is not possible.
|
abstract InputStream |
getInputStream()
Returns an input stream to the resource
|
String |
getListHTML(String base,
boolean parent)
Get the resource list as a HTML directory listing.
|
abstract String |
getName()
Returns the name of the resource
|
abstract ReadableByteChannel |
getReadableByteChannel()
Returns an readable bytechannel to the resource or null if one is not available.
|
Resource |
getResource(String path)
Get a resource from within this resource.
|
URI |
getURI()
Returns an URI representing the given resource
|
abstract URL |
getURL()
Returns an URL representing the given resource
|
String |
getWeakETag() |
abstract boolean |
isContainedIn(Resource r) |
static boolean |
isContainedIn(Resource r,
Resource containingResource) |
abstract boolean |
isDirectory()
Returns true if the respresenetd resource is a container/directory.
|
abstract long |
lastModified()
Returns the last modified time
|
abstract long |
length()
Return the length of the resource
|
abstract String[] |
list()
Returns a list of resource names contained in the given resource
The resource names are not URL encoded.
|
static Resource |
newClassPathResource(String resource)
Find a classpath resource.
|
static Resource |
newClassPathResource(String name,
boolean useCaches,
boolean checkParents)
Find a classpath resource.
|
static Resource |
newResource(File file) |
static Resource |
newResource(String resource)
Construct a resource from a string.
|
static Resource |
newResource(String resource,
boolean useCaches)
Construct a resource from a string.
|
static Resource |
newResource(URI uri)
Construct a resource from a uri.
|
static Resource |
newResource(URL url)
Construct a resource from a url.
|
static Resource |
newSystemResource(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(Object o) |
static void |
setDefaultUseCaches(boolean useCaches)
Change the default setting for url connection caches.
|
static URL |
toURL(File file)
Generate a properly encoded URL from a
File instance. |
void |
writeTo(OutputStream out,
long start,
long count) |
public static void setDefaultUseCaches(boolean useCaches)
useCaches
- public static boolean getDefaultUseCaches()
public static Resource newResource(URI uri) throws MalformedURLException
uri
- A URI.MalformedURLException
- Problem accessing URIpublic static Resource newResource(URL url)
url
- A URL.public static Resource newResource(String resource) throws MalformedURLException
resource
- A URL or filename.MalformedURLException
- Problem accessing URIpublic static Resource newResource(String resource, boolean useCaches) throws MalformedURLException
resource
- A URL or filename.useCaches
- controls URLConnection cachingMalformedURLException
- Problem accessing URIpublic static Resource newSystemResource(String resource) throws IOException
resource
- Resource as string representationIOException
- Problem accessing resource.public static Resource newClassPathResource(String resource)
public static Resource newClassPathResource(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(Class, 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 MalformedURLException
MalformedURLException
public abstract boolean isContainedIn(Resource r) throws MalformedURLException
MalformedURLException
public final void release()
close()
public abstract void close()
close
in interface Closeable
close
in interface AutoCloseable
public abstract boolean exists()
public abstract boolean isDirectory()
public abstract long lastModified()
public abstract long length()
public abstract URL getURL()
public URI getURI()
public abstract File getFile() throws IOException
IOException
public abstract String getName()
public abstract InputStream getInputStream() throws IOException
IOException
public abstract ReadableByteChannel getReadableByteChannel() throws IOException
IOException
public abstract boolean delete() throws SecurityException
SecurityException
public abstract boolean renameTo(Resource dest) throws SecurityException
SecurityException
public abstract String[] list()
public abstract Resource addPath(String path) throws IOException, MalformedURLException
path
- The path segment to add, which is not encodedIOException
MalformedURLException
public Resource getResource(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)
public Object getAssociate()
public void setAssociate(Object o)
public URI getAlias()
public String getListHTML(String base, boolean parent) throws IOException
base
- The base URLparent
- True if the parent directory should be includedIOException
public void writeTo(OutputStream out, long start, long count) throws IOException
out
- start
- First byte to writecount
- Bytes to write or -1 for all of them.IOException
public void copyTo(File destination) throws IOException
IOException
public String getWeakETag()
public Collection<Resource> getAllResources()
public static URL toURL(File file) throws MalformedURLException
File
instance.file
- Target file.MalformedURLException
Copyright © 1995-2015 Webtide. All Rights Reserved.