Package org.openstreetmap.josm.io
Class CachedFile
- java.lang.Object
-
- org.openstreetmap.josm.io.CachedFile
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class CachedFile extends java.lang.Object implements java.io.Closeable
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 withgetFile().
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCachedFile.CachingStrategyCaching strategy.
-
Field Summary
Fields Modifier and Type Field Description private HttpClientactiveConnectionprotected java.io.FilecacheFileprotected CachedFile.CachingStrategycachingStrategystatic longDAYSstatic longDEFAULT_MAXTIMEprotected java.lang.StringdestDirprivate booleanfastFailprotected java.lang.StringhttpAcceptprivate java.util.Map<java.lang.String,java.lang.String>httpHeadersprotected booleaninitializedprotected longmaxAgeprotected java.lang.Stringnameprotected java.lang.Stringparameter
-
Constructor Summary
Constructors Constructor Description CachedFile(java.lang.String name)Constructs a CachedFile object from a given filename, URL or internal resource.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private java.io.FilecheckLocal(java.net.URL url)static voidcleanup(java.lang.String name)Clear the cache for the given resource.static voidcleanup(java.lang.String name, java.lang.String destDir)Clear the cache for the given resource.voidclear()Clears the cached filevoidclose()Attempts to disconnect an URL connection.private Pair<java.lang.String,Pair<java.util.zip.ZipFile,java.io.InputStream>>findZipEntryImpl(java.lang.String extension, java.lang.String namepart)Pair<java.util.zip.ZipFile,java.io.InputStream>findZipEntryInputStream(java.lang.String extension, java.lang.String namepart)LikefindZipEntryPath(java.lang.String, java.lang.String), but returns the corresponding InputStream.java.lang.StringfindZipEntryPath(java.lang.String extension, java.lang.String namepart)Looks for a certain entry inside a zip file and returns the entry path.byte[]getByteContent()Get the full content of the requested resource as a byte array.CachedFile.CachingStrategygetCachingStrategy()java.io.BufferedReadergetContentReader()ReturnsgetInputStream()wrapped in a buffered reader.java.lang.StringgetDestDir()java.io.FilegetFile()Get local file for the requested resource.java.lang.StringgetHttpAccept()java.io.InputStreamgetInputStream()Get InputStream to the requested resource.longgetMaxAge()Returns maximum age of cache file.java.lang.StringgetName()private static java.lang.StringgetPrefKey(java.net.URL url, java.lang.String destDir)Get preference key to store the location and age of the cached file.CachedFilesetCachingStrategy(CachedFile.CachingStrategy cachingStrategy)Set the caching strategy.CachedFilesetDestDir(java.lang.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.CachedFilesetHttpAccept(java.lang.String httpAccept)Set the accepted MIME types sent in the HTTP Accept header.CachedFilesetHttpHeaders(java.util.Map<java.lang.String,java.lang.String> headers)Sets the http headers.CachedFilesetMaxAge(long maxAge)Set maximum age of cache file.CachedFilesetName(java.lang.String name)Set the name of the resource.voidsetParam(java.lang.String parameter)Sets additional URL parameter (used e.g.java.lang.StringtoString()private static java.lang.StringtruncatePath(java.lang.String directory, java.lang.String fileName)
-
-
-
Field Detail
-
name
protected java.lang.String name
-
maxAge
protected long maxAge
-
destDir
protected java.lang.String destDir
-
httpAccept
protected java.lang.String httpAccept
-
cachingStrategy
protected CachedFile.CachingStrategy cachingStrategy
-
fastFail
private boolean fastFail
-
activeConnection
private HttpClient activeConnection
-
cacheFile
protected java.io.File cacheFile
-
initialized
protected boolean initialized
-
parameter
protected java.lang.String parameter
-
DEFAULT_MAXTIME
public static final long DEFAULT_MAXTIME
- See Also:
- Constant Field Values
-
DAYS
public static final long DAYS
-
httpHeaders
private final java.util.Map<java.lang.String,java.lang.String> httpHeaders
-
-
Constructor Detail
-
CachedFile
public CachedFile(java.lang.String name)
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 Detail
-
setName
public CachedFile setName(java.lang.String name)
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
public CachedFile setMaxAge(long maxAge)
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
public CachedFile setDestDir(java.lang.String destDir)
Set the destination directory for the cache file. Only applies to URLs.- Parameters:
destDir- the destination directory- Returns:
- this object
-
setHttpAccept
public CachedFile setHttpAccept(java.lang.String httpAccept)
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
public CachedFile setCachingStrategy(CachedFile.CachingStrategy cachingStrategy)
Set the caching strategy. Only applies to URLs.- Parameters:
cachingStrategy- caching strategy- Returns:
- this object
-
setHttpHeaders
public CachedFile setHttpHeaders(java.util.Map<java.lang.String,java.lang.String> headers)
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
public void setFastFail(boolean fastFail)
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
public void setParam(java.lang.String parameter)
Sets additional URL parameter (used e.g. for maps)- Parameters:
parameter- the URL parameter- Since:
- 13536
-
getName
public java.lang.String getName()
-
getMaxAge
public long 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
public java.lang.String getDestDir()
-
getHttpAccept
public java.lang.String getHttpAccept()
-
getCachingStrategy
public CachedFile.CachingStrategy getCachingStrategy()
-
getInputStream
public java.io.InputStream getInputStream() throws java.io.IOException
Get InputStream to the requested resource.- Returns:
- the InputStream
- Throws:
java.io.IOException- when the resource with the given name could not be retrievedjava.nio.file.InvalidPathException- if a Path object cannot be constructed from the inner file path
-
getByteContent
public byte[] getByteContent() throws java.io.IOException
Get the full content of the requested resource as a byte array.- Returns:
- the full content of the requested resource as byte array
- Throws:
java.io.IOException- in case of an I/O error
-
getContentReader
public java.io.BufferedReader getContentReader() throws java.io.IOException
ReturnsgetInputStream()wrapped in a buffered reader.Detects Unicode charset in use utilizing
UTFInputStreamReader.- Returns:
- buffered reader
- Throws:
java.io.IOException- if any I/O error occurs- Since:
- 9411
-
getFile
public java.io.File getFile() throws java.io.IOException
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:
java.io.IOException- when the resource with the given name could not be retrieved
-
findZipEntryPath
public java.lang.String findZipEntryPath(java.lang.String extension, java.lang.String namepart)
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
public Pair<java.util.zip.ZipFile,java.io.InputStream> findZipEntryInputStream(java.lang.String extension, java.lang.String namepart)
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
private Pair<java.lang.String,Pair<java.util.zip.ZipFile,java.io.InputStream>> findZipEntryImpl(java.lang.String extension, java.lang.String namepart)
-
cleanup
public static void cleanup(java.lang.String name)
Clear the cache for the given resource. This forces a fresh download.- Parameters:
name- the URL
-
cleanup
public static void cleanup(java.lang.String name, java.lang.String destDir)
Clear the cache for the given resource. This forces a fresh download.- Parameters:
name- the URLdestDir- the destination directory (seesetDestDir(java.lang.String))
-
getPrefKey
private static java.lang.String getPrefKey(java.net.URL url, java.lang.String destDir)
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
private java.io.File checkLocal(java.net.URL url) throws java.io.IOException
- Throws:
java.io.IOException
-
truncatePath
private static java.lang.String truncatePath(java.lang.String directory, java.lang.String fileName)
-
close
public void close()
Attempts to disconnect an URL connection.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Since:
- 9411
- See Also:
HttpClient.disconnect()
-
clear
public void clear() throws java.io.IOException
Clears the cached file- Throws:
java.io.IOException- if any I/O error occurs- Since:
- 10993
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-