public class CachedFile extends java.lang.Object
resource:
) to get
resources from the current *.jar file. (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()
.
Modifier and Type | Class and Description |
---|---|
static class |
CachedFile.CachingStrategy
Caching strategy.
|
Modifier and Type | Field and Description |
---|---|
protected java.io.File |
cacheFile |
protected CachedFile.CachingStrategy |
cachingStrategy |
static long |
DAYS |
static long |
DEFAULT_MAXTIME |
protected java.lang.String |
destDir |
protected java.lang.String |
httpAccept |
protected boolean |
initialized |
protected long |
maxAge |
protected java.lang.String |
name |
Constructor and Description |
---|
CachedFile(java.lang.String name)
Constructs a CachedFile object from a given filename, URL or internal resource.
|
Modifier and Type | Method and Description |
---|---|
private java.io.File |
checkLocal(java.net.URL url) |
private static void |
checkOfflineAccess(java.lang.String urlString) |
static void |
cleanup(java.lang.String name)
Clear the cache for the given resource.
|
static void |
cleanup(java.lang.String name,
java.lang.String destDir)
Clear the cache for the given resource.
|
static java.net.HttpURLConnection |
connectFollowingRedirect(java.net.URL downloadUrl,
java.lang.String httpAccept,
java.lang.Long ifModifiedSince)
Opens a connection for downloading a resource.
|
private Pair<java.lang.String,java.io.InputStream> |
findZipEntryImpl(java.lang.String extension,
java.lang.String namepart) |
java.io.InputStream |
findZipEntryInputStream(java.lang.String extension,
java.lang.String namepart)
Like
findZipEntryPath(java.lang.String, java.lang.String) , but returns the corresponding InputStream. |
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.
|
CachedFile.CachingStrategy |
getCachingStrategy() |
java.lang.String |
getDestDir() |
java.io.File |
getFile()
Get local file for the requested resource.
|
java.lang.String |
getHttpAccept() |
java.io.InputStream |
getInputStream()
Get InputStream to the requested resource.
|
long |
getMaxAge() |
java.lang.String |
getName() |
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.
|
CachedFile |
setCachingStrategy(CachedFile.CachingStrategy cachingStrategy)
Set the caching strategy.
|
CachedFile |
setDestDir(java.lang.String destDir)
Set the destination directory for the cache file.
|
CachedFile |
setHttpAccept(java.lang.String httpAccept)
Set the accepted MIME types sent in the HTTP Accept header.
|
CachedFile |
setMaxAge(long maxAge)
Set maximum age of cache file.
|
CachedFile |
setName(java.lang.String name)
Set the name of the resource.
|
protected java.lang.String name
protected long maxAge
protected java.lang.String destDir
protected java.lang.String httpAccept
protected CachedFile.CachingStrategy cachingStrategy
protected java.io.File cacheFile
protected boolean initialized
public static final long DEFAULT_MAXTIME
public static final long DAYS
public CachedFile(java.lang.String name)
name
- can be:file:///SOME/FILE
the same as abovehttp://...
a URL. It will be cached on disk.resource://SOME/FILE
file from the classpath (usually in the current *.jar)josmdir://SOME/FILE
file inside josm user data directory (since r7058)josmplugindir://SOME/FILE
file inside josm plugin directory (since r7834)public CachedFile setName(java.lang.String name)
name
- can be:file:///SOME/FILE
the same as abovehttp://...
a URL. It will be cached on disk.resource://SOME/FILE
file from the classpath (usually in the current *.jar)josmdir://SOME/FILE
file inside josm user data directory (since r7058)josmplugindir://SOME/FILE
file inside josm plugin directory (since r7834)public CachedFile setMaxAge(long maxAge)
maxAge
- the maximum cache age in secondspublic CachedFile setDestDir(java.lang.String destDir)
destDir
- the destination directorypublic CachedFile setHttpAccept(java.lang.String httpAccept)
httpAccept
- the accepted MIME typespublic CachedFile setCachingStrategy(CachedFile.CachingStrategy cachingStrategy)
cachingStrategy
- public java.lang.String getName()
public long getMaxAge()
public java.lang.String getDestDir()
public java.lang.String getHttpAccept()
public CachedFile.CachingStrategy getCachingStrategy()
public java.io.InputStream getInputStream() throws java.io.IOException
java.io.IOException
- when the resource with the given name could not be retrievedpublic java.io.File getFile() throws java.io.IOException
java.io.IOException
- when the resource with the given name could not be retrievedpublic java.lang.String findZipEntryPath(java.lang.String extension, java.lang.String namepart)
extension
. If more than one files have this
extension, the last file whose name includes namepart
is opened.extension
- the extension of the file we're looking fornamepart
- the name partpublic java.io.InputStream findZipEntryInputStream(java.lang.String extension, java.lang.String namepart)
findZipEntryPath(java.lang.String, java.lang.String)
, but returns the corresponding InputStream.extension
- the extension of the file we're looking fornamepart
- the name partprivate Pair<java.lang.String,java.io.InputStream> findZipEntryImpl(java.lang.String extension, java.lang.String namepart)
public static void cleanup(java.lang.String name)
name
- the URLpublic static void cleanup(java.lang.String name, java.lang.String destDir)
name
- the URLdestDir
- the destination directory (see setDestDir(java.lang.String)
)private static java.lang.String getPrefKey(java.net.URL url, java.lang.String destDir)
private java.io.File checkLocal(java.net.URL url) throws java.io.IOException
java.io.IOException
private static void checkOfflineAccess(java.lang.String urlString)
public static java.net.HttpURLConnection connectFollowingRedirect(java.net.URL downloadUrl, java.lang.String httpAccept, java.lang.Long ifModifiedSince) throws java.net.MalformedURLException, java.io.IOException
Manually follows redirects because
HttpURLConnection.setFollowRedirects(boolean)
fails if the redirect
is going from a http to a https URL, see bug report.
This can cause problems when downloading from certain GitHub URLs.
downloadUrl
- The resource URL to downloadhttpAccept
- The accepted MIME types sent in the HTTP Accept header. Can be null
ifModifiedSince
- The download time of the cache file, optionaljava.net.MalformedURLException
- If a redirected URL is wrongjava.io.IOException
- If any I/O operation goes wrongOfflineAccessException
- if resource is accessed in offline mode, in any protocol