Package org.openstreetmap.josm.io
Class CachedFile
java.lang.Object
org.openstreetmap.josm.io.CachedFile
- All Implemented Interfaces:
Closeable,AutoCloseable
Downloads a file and caches it on disk in order to reduce network load.
Supports URLs, local files, and a custom scheme (
resource:) to get
resources from the current JOSM *.jar file as well as plugins *.jar files.
(Local caching is only done for URLs.)
The mirrored file is only downloaded if it has been more than 7 days since last download. (Time can be configured.)
The file content is normally accessed with getInputStream(), but
you can also get the mirrored copy with getFile().
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate HttpClientprotected Fileprotected CachedFile.CachingStrategystatic final longstatic final longprotected Stringprivate booleanprotected Stringprotected booleanprotected longprotected Stringprotected String -
Constructor Summary
ConstructorsConstructorDescriptionCachedFile(String name) Constructs a CachedFile object from a given filename, URL or internal resource. -
Method Summary
Modifier and TypeMethodDescriptionprivate FilecheckLocal(URL url) static voidClear the cache for the given resource.static voidClear the cache for the given resource.voidclear()Clears the cached filevoidclose()Attempts to disconnect an URL connection.private Pair<String,Pair<ZipFile, InputStream>> findZipEntryImpl(String extension, String namepart) findZipEntryInputStream(String extension, String namepart) LikefindZipEntryPath(java.lang.String, java.lang.String), but returns the corresponding InputStream.findZipEntryPath(String extension, String namepart) Looks for a certain entry inside a zip file and returns the entry path.byte[]Get the full content of the requested resource as a byte array.ReturnsgetInputStream()wrapped in a buffered reader.getFile()Get local file for the requested resource.Get InputStream to the requested resource.longReturns maximum age of cache file.getName()private static StringgetPrefKey(URL url, String destDir) Get preference key to store the location and age of the cached file.setCachingStrategy(CachedFile.CachingStrategy cachingStrategy) Set the caching strategy.setDestDir(String destDir) Set the destination directory for the cache file.voidsetFastFail(boolean fastFail) Sets whether opening HTTP connections should fail fast, i.e., whether alow connect timeoutshould be used.setHttpAccept(String httpAccept) Set the accepted MIME types sent in the HTTP Accept header.setHttpHeaders(Map<String, String> headers) Sets the http headers.setMaxAge(long maxAge) Set maximum age of cache file.Set the name of the resource.voidSets additional URL parameter (used e.g.toString()private static StringtruncatePath(String directory, String fileName)
-
Field Details
-
name
-
maxAge
-
destDir
-
httpAccept
-
cachingStrategy
-
fastFail
-
activeConnection
-
cacheFile
-
initialized
-
parameter
-
DEFAULT_MAXTIME
- See Also:
-
DAYS
-
httpHeaders
-
-
Constructor Details
-
CachedFile
Constructs a CachedFile object from a given filename, URL or internal resource.- Parameters:
name- can be:- relative or absolute file name
file:///SOME/FILEthe same as abovehttp://...a URL. It will be cached on disk.resource://SOME/FILEfile from the classpath (usually in the current *.jar)josmdir://SOME/FILEfile inside josm user data directory (since r7058)josmplugindir://SOME/FILEfile inside josm plugin directory (since r7834)
-
-
Method Details
-
setName
Set the name of the resource.- Parameters:
name- can be:- relative or absolute file name
file:///SOME/FILEthe same as abovehttp://...a URL. It will be cached on disk.resource://SOME/FILEfile from the classpath (usually in the current *.jar)josmdir://SOME/FILEfile inside josm user data directory (since r7058)josmplugindir://SOME/FILEfile inside josm plugin directory (since r7834)
- Returns:
- this object
-
setMaxAge
Set maximum age of cache file. Only applies to URLs. When this time has passed after the last download of the file, the cache is considered stale and a new download will be attempted.- Parameters:
maxAge- the maximum cache age in seconds- Returns:
- this object
-
setDestDir
Set the destination directory for the cache file. Only applies to URLs.- Parameters:
destDir- the destination directory- Returns:
- this object
-
setHttpAccept
Set the accepted MIME types sent in the HTTP Accept header. Only applies to URLs.- Parameters:
httpAccept- the accepted MIME types- Returns:
- this object
-
setCachingStrategy
Set the caching strategy. Only applies to URLs.- Parameters:
cachingStrategy- caching strategy- Returns:
- this object
-
setHttpHeaders
Sets the http headers. Only applies to URL pointing to http or https resources- Parameters:
headers- that should be sent together with request- Returns:
- this object
-
setFastFail
Sets whether opening HTTP connections should fail fast, i.e., whether alow connect timeoutshould be used.- Parameters:
fastFail- whether opening HTTP connections should fail fast
-
setParam
Sets additional URL parameter (used e.g. for maps)- Parameters:
parameter- the URL parameter- Since:
- 13536
-
getName
-
getMaxAge
Returns maximum age of cache file. Only applies to URLs. When this time has passed after the last download of the file, the cache is considered stale and a new download will be attempted.- Returns:
- the maximum cache age in seconds
-
getDestDir
-
getHttpAccept
-
getCachingStrategy
-
getInputStream
Get InputStream to the requested resource.- Returns:
- the InputStream
- Throws:
IOException- when the resource with the given name could not be retrievedInvalidPathException- if a Path object cannot be constructed from the inner file path
-
getByteContent
Get the full content of the requested resource as a byte array.- Returns:
- the full content of the requested resource as byte array
- Throws:
IOException- in case of an I/O error
-
getContentReader
ReturnsgetInputStream()wrapped in a buffered reader.Detects Unicode charset in use utilizing
UTFInputStreamReader.- Returns:
- buffered reader
- Throws:
IOException- if any I/O error occurs- Since:
- 9411
-
getFile
Get local file for the requested resource.- Returns:
- The local cache file for URLs. If the resource is a local file, returns just that file.
- Throws:
IOException- when the resource with the given name could not be retrieved
-
findZipEntryPath
Looks for a certain entry inside a zip file and returns the entry path. Replies a file in the top level directory of the ZIP file which has an extensionextension. If more than one files have this extension, the last file whose name includesnamepartis opened.- Parameters:
extension- the extension of the file we're looking fornamepart- the name part- Returns:
- The zip entry path of the matching file.
nullif this cached file doesn't represent a zip file or if there was no matching file in the ZIP file.
-
findZipEntryInputStream
LikefindZipEntryPath(java.lang.String, java.lang.String), but returns the corresponding InputStream.- Parameters:
extension- the extension of the file we're looking fornamepart- the name part- Returns:
- InputStream to the matching file.
nullif this cached file doesn't represent a zip file or if there was no matching file in the ZIP file. The returned ZipFile must be closed after use. - Since:
- 19372
-
findZipEntryImpl
-
cleanup
Clear the cache for the given resource. This forces a fresh download.- Parameters:
name- the URL
-
cleanup
Clear the cache for the given resource. This forces a fresh download.- Parameters:
name- the URLdestDir- the destination directory (seesetDestDir(java.lang.String))
-
getPrefKey
Get preference key to store the location and age of the cached file. 2 resources that point to the same url, but that are to be stored in different directories will not share a cache file.- Parameters:
url- URLdestDir- destination directory- Returns:
- Preference key
-
checkLocal
- Throws:
IOException
-
truncatePath
-
close
Attempts to disconnect an URL connection.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Since:
- 9411
- See Also:
-
clear
Clears the cached file- Throws:
IOException- if any I/O error occurs- Since:
- 10993
-
toString
-