Class ResourceTracker
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.
- Version:
- $Revision: 1.22 $
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final DownloadOptionsprivate static final Loggerprivate final booleanwhether to download parts before requestedthe resources known about by this resource trackerprivate final UpdatePolicy -
Constructor Summary
ConstructorsConstructorDescriptionCreates a resource tracker that does not prefetch resources.ResourceTracker(boolean prefetch) Creates a resource tracker that does not prefetch resources.ResourceTracker(boolean prefetch, DownloadOptions downloadOptions, UpdatePolicy updatePolicy) Creates a resource tracker. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddDownloadListener(URL resourceUrl, URL[] allResources, DownloadServiceListener listener) voidaddResource(URL location, VersionString version) voidaddResource(URL location, VersionString version, UpdatePolicy updatePolicy) Add a resource identified by the specified location and version.private booleanaddToResources(Resource resource) booleancheckResource(URL location) Returns whether a resource is available for use (ie, can be accessed with the getCacheFile method).longgetAmountRead(URL location) Returns the number of bytes downloaded for a resource.getCacheFile(URL location) Returns a file containing the downloaded resource.private static FilegetCacheFile(Resource resource) getCacheURL(URL location) Returns a URL pointing to the cached location of the resource, or the resource itself if it is a non-cacheable resource.private intprivate intgetPercentageDownloaded(URL[] allResources) private ResourcegetResource(URL location) Return the resource matching the specified URL.private Resource[]getResources(URL[] urls) longgetTotalSize(URL location) Returns the number of total size in bytes of a resource, or -1 it the size is not known.voidremoveResource(URL location) Removes a resource from the tracker.private voidstartDownloadingIfPrefetch(Resource resource) triggerDownloadFor(Resource resource, Executor downloadExecutor) private voidwaitForCompletion(Resource... resources) Wait for some resources.voidwaitForResources(URL... urls) Wait for a group of resources to be downloaded and made available locally.
-
Field Details
-
LOG
-
resources
-
prefetch
private final boolean prefetchwhether to download parts before requested -
downloadOptions
-
updatePolicy
-
-
Constructor Details
-
ResourceTracker
public ResourceTracker()Creates a resource tracker that does not prefetch resources. -
ResourceTracker
public ResourceTracker(boolean prefetch) Creates a resource tracker that does not prefetch resources. -
ResourceTracker
public ResourceTracker(boolean prefetch, DownloadOptions downloadOptions, UpdatePolicy updatePolicy) Creates a resource tracker.- Parameters:
prefetch- whether to download resources before requested.
-
-
Method Details
-
addResource
-
addResource
Add a resource identified by the specified location and version. The tracker only downloads one version of a given resource per instance (ie cannot download both versions 1 and 2 of a resource in the same tracker).- Parameters:
location- the location of the resourceversion- the resource versionupdatePolicy- whether to check for updates if already in cache
-
addToResources
- Returns:
trueif no resource with the given URL is currently tracked.
-
startDownloadingIfPrefetch
-
removeResource
Removes a resource from the tracker. This method is useful to allow memory to be reclaimed, but calling this method is not required as resources are reclaimed when the tracker is collected.- Parameters:
location- location of resource to be removed- Throws:
IllegalResourceDescriptorException- if the resource is not being tracked
-
getCacheURL
Returns a URL pointing to the cached location of the resource, or the resource itself if it is a non-cacheable resource.If the resource has not downloaded yet, the method will block until it has been transferred to the cache.
- Parameters:
location- the resource location- Returns:
- the resource, or null if it could not be downloaded
- Throws:
IllegalResourceDescriptorException- if the resource is not being tracked- See Also:
-
getCacheFile
Returns a file containing the downloaded resource. If the resource is non-cacheable then null is returned unless the resource is a local file (the original file is returned).If the resource has not downloaded yet, the method will block until it has been transferred to the cache.
- Parameters:
location- the resource location- Returns:
- a local file containing the resource, or null
- Throws:
IllegalResourceDescriptorException- if the resource is not being tracked- See Also:
-
getCacheFile
-
waitForResources
Wait for a group of resources to be downloaded and made available locally.- Parameters:
urls- the resources to wait for- Throws:
InterruptedException- if thread is interruptedIllegalResourceDescriptorException- if the resource is not being tracked
-
getAmountRead
Returns the number of bytes downloaded for a resource.- Parameters:
location- the resource location- Returns:
- the number of bytes transferred
- Throws:
IllegalResourceDescriptorException- if the resource is not being tracked
-
checkResource
Returns whether a resource is available for use (ie, can be accessed with the getCacheFile method).- Parameters:
location- the resource location- Returns:
- resource availability
- Throws:
IllegalResourceDescriptorException- if the resource is not being tracked
-
getTotalSize
Returns the number of total size in bytes of a resource, or -1 it the size is not known.- Parameters:
location- the resource location- Returns:
- the number of bytes, or -1
- Throws:
IllegalResourceDescriptorException- if the resource is not being tracked
-
getResources
-
getResource
Return the resource matching the specified URL.- Throws:
IllegalResourceDescriptorException- if the resource is not being tracked
-
waitForCompletion
Wait for some resources.- Parameters:
resources- the resources to wait for- Throws:
InterruptedException- if another thread interrupted the wait
-
getConfiguredThreadCount
private int getConfiguredThreadCount() -
triggerDownloadFor
-
addDownloadListener
public void addDownloadListener(URL resourceUrl, URL[] allResources, DownloadServiceListener listener) -
getPercentageDownloaded
-