net.sourceforge.jnlp.cache
public class ResourceTracker extends java.lang.Object
The resource tracker can be configured to prefetch resources, which are downloaded in the order added to the media tracker.
Multiple threads are used to download and cache resources that are actively being waited for (blocking a caller) or those that have been started downloading by calling the startDownload method. Resources that are prefetched are downloaded one at a time and only if no other trackers have requested downloads. This allows the tracker to start downloading many items without using many system resources, but still quickly download items as needed.
Constructor and Description |
---|
ResourceTracker()
Creates a resource tracker that does not prefetch resources.
|
ResourceTracker(boolean prefetch)
Creates a resource tracker.
|
Modifier and Type | Method and Description |
---|---|
void |
addDownloadListener(DownloadListener listener)
Adds the listener to the list of objects interested in
receivind DownloadEvents.
|
void |
addResource(java.net.URL location,
Version version,
DownloadOptions options,
UpdatePolicy updatePolicy)
Add a resource identified by the specified location and
version.
|
boolean |
checkResource(java.net.URL location)
Returns whether a resource is available for use (ie, can be
accessed with the getCacheFile method).
|
protected void |
fireDownloadEvent(Resource resource)
Fires the download event corresponding to the resource's
state.
|
long |
getAmountRead(java.net.URL location)
Returns the number of bytes downloaded for a resource.
|
java.io.File |
getCacheFile(java.net.URL location)
Returns a file containing the downloaded resource.
|
java.net.URL |
getCacheURL(java.net.URL location)
Returns a URL pointing to the cached location of the
resource, or the resource itself if it is a non-cacheable
resource.
|
java.io.InputStream |
getInputStream(java.net.URL location)
Returns an input stream that reads the contents of the
resource.
|
long |
getTotalSize(java.net.URL location)
Returns the number of total size in bytes of a resource, or
-1 it the size is not known.
|
static java.net.URL |
normalizeUrl(java.net.URL u,
boolean debug) |
void |
removeDownloadListener(DownloadListener listener)
Removes a download listener.
|
void |
removeResource(java.net.URL location)
Removes a resource from the tracker.
|
boolean |
startResource(java.net.URL location)
Starts loading the resource if it is not already being
downloaded or already cached.
|
protected void |
startThread()
Start a new download thread if there are not too many threads
already running.
|
boolean |
waitForResource(java.net.URL location,
long timeout)
Wait for a particular resource to be downloaded and made
available.
|
boolean |
waitForResources(java.net.URL[] urls,
long timeout)
Wait for a group of resources to be downloaded and made
available locally.
|
public ResourceTracker()
public ResourceTracker(boolean prefetch)
prefetch
- whether to download resources before requested.public void addResource(java.net.URL location, Version version, DownloadOptions options, UpdatePolicy updatePolicy)
location
- the location of the resourceversion
- the resource versionupdatePolicy
- whether to check for updates if already in cachepublic void removeResource(java.net.URL location)
IllegalResourceDescriptorException
- if the resource is not being trackedpublic void addDownloadListener(DownloadListener listener)
listener
- the listener to add.public void removeDownloadListener(DownloadListener listener)
listener
- the listener to remove.protected void fireDownloadEvent(Resource resource)
public java.net.URL getCacheURL(java.net.URL location)
If the resource has not downloaded yet, the method will block until it has been transferred to the cache.
location
- the resource locationIllegalResourceDescriptorException
- if the resource is not being trackedCacheUtil.isCacheable(java.net.URL, net.sourceforge.jnlp.Version)
public java.io.File getCacheFile(java.net.URL location)
If the resource has not downloaded yet, the method will block until it has been transferred to the cache.
location
- the resource locationIllegalResourceDescriptorException
- if the resource is not being trackedCacheUtil.isCacheable(java.net.URL, net.sourceforge.jnlp.Version)
public java.io.InputStream getInputStream(java.net.URL location) throws java.io.IOException
This method will block while the resource is downloaded to the cache.
java.io.IOException
- if there was an error opening the streamIllegalResourceDescriptorException
- if the resource is not being trackedpublic boolean waitForResources(java.net.URL[] urls, long timeout) throws java.lang.InterruptedException
urls
- the resources to wait fortimeout
- the time in ms to wait before returning, 0 for no timeoutIllegalResourceDescriptorException
- if the resource is not being trackedjava.lang.InterruptedException
public boolean waitForResource(java.net.URL location, long timeout) throws java.lang.InterruptedException
location
- the resource to wait fortimeout
- the timeout, or 0 to wait until completedjava.lang.InterruptedException
- if another thread interrupted the waitIllegalResourceDescriptorException
- if the resource is not being trackedpublic long getAmountRead(java.net.URL location)
location
- the resource locationIllegalResourceDescriptorException
- if the resource is not being trackedpublic boolean checkResource(java.net.URL location)
IllegalResourceDescriptorException
- if the resource is not being trackedpublic boolean startResource(java.net.URL location)
IllegalResourceDescriptorException
- if the resource is not being trackedpublic long getTotalSize(java.net.URL location)
location
- the resource locationIllegalResourceDescriptorException
- if the resource is not being trackedprotected void startThread()
Calls to this method should be synchronized on lock.
public static java.net.URL normalizeUrl(java.net.URL u, boolean debug) throws java.net.MalformedURLException, java.io.UnsupportedEncodingException
java.net.MalformedURLException
java.io.UnsupportedEncodingException