public class CachedFile extends 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 File |
cacheFile |
protected CachedFile.CachingStrategy |
cachingStrategy |
static long |
DAYS |
static long |
DEFAULT_MAXTIME |
protected String |
destDir |
protected String |
httpAccept |
protected boolean |
initialized |
protected long |
maxAge |
protected String |
name |
Constructor and Description |
---|
CachedFile(String name)
Constructs a CachedFile object from a given filename, URL or internal resource.
|
Modifier and Type | Method and Description |
---|---|
private File |
checkLocal(URL url) |
private static void |
checkOfflineAccess(String urlString) |
static void |
cleanup(String name)
Clear the cache for the given resource.
|
static void |
cleanup(String name,
String destDir)
Clear the cache for the given resource.
|
static HttpURLConnection |
connectFollowingRedirect(URL downloadUrl,
String httpAccept,
Long ifModifiedSince)
Opens a connection for downloading a resource.
|
private Pair<String,InputStream> |
findZipEntryImpl(String extension,
String namepart) |
InputStream |
findZipEntryInputStream(String extension,
String namepart)
Like
findZipEntryPath(java.lang.String, java.lang.String) , but returns the corresponding InputStream. |
String |
findZipEntryPath(String extension,
String namepart)
Looks for a certain entry inside a zip file and returns the entry path.
|
CachedFile.CachingStrategy |
getCachingStrategy() |
String |
getDestDir() |
File |
getFile()
Get local file for the requested resource.
|
String |
getHttpAccept() |
InputStream |
getInputStream()
Get InputStream to the requested resource.
|
long |
getMaxAge() |
String |
getName() |
private static String |
getPrefKey(URL url,
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(String destDir)
Set the destination directory for the cache file.
|
CachedFile |
setHttpAccept(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(String name)
Set the name of the resource.
|
protected long maxAge
protected String httpAccept
protected CachedFile.CachingStrategy cachingStrategy
protected boolean initialized
public static final long DEFAULT_MAXTIME
public static final long DAYS
public CachedFile(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(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(String destDir)
destDir
- the destination directorypublic CachedFile setHttpAccept(String httpAccept)
httpAccept
- the accepted MIME typespublic CachedFile setCachingStrategy(CachedFile.CachingStrategy cachingStrategy)
cachingStrategy
- public long getMaxAge()
public String getDestDir()
public String getHttpAccept()
public CachedFile.CachingStrategy getCachingStrategy()
public InputStream getInputStream() throws IOException
IOException
- when the resource with the given name could not be retrievedpublic File getFile() throws IOException
IOException
- when the resource with the given name could not be retrievedpublic String findZipEntryPath(String extension, 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 InputStream findZipEntryInputStream(String extension, 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<String,InputStream> findZipEntryImpl(String extension, String namepart)
public static void cleanup(String name)
name
- the URLpublic static void cleanup(String name, String destDir)
name
- the URLdestDir
- the destination directory (see setDestDir(java.lang.String)
)private static String getPrefKey(URL url, String destDir)
private File checkLocal(URL url) throws IOException
IOException
private static void checkOfflineAccess(String urlString)
public static HttpURLConnection connectFollowingRedirect(URL downloadUrl, String httpAccept, Long ifModifiedSince) throws MalformedURLException, 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, optionalMalformedURLException
- If a redirected URL is wrongIOException
- If any I/O operation goes wrongOfflineAccessException
- if resource is accessed in offline mode, in any protocol