|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object sk.baka.ambient.commons.AbstractFileStorage
public class AbstractFileStorage
An abstract file cache, stores files into android application private directory. Thread-safe. Essentially a singleton, there must be at most one instance.
Field Summary | |
---|---|
protected long |
maxStorageSize
Maximum storage size, in bytes. |
protected Map<String,File> |
theCache
A read-only view of the cache. |
Constructor Summary | |
---|---|
AbstractFileStorage(String contentDesc,
String... ext)
Creates new cache instance. |
Method Summary | |
---|---|
protected void |
cleanup()
Cleans up old files. |
void |
close()
Closes the cache object, stopping any pending download operations. |
protected FileOutputStream |
createFile(String name,
String ext)
Registers a file to the cache. |
protected void |
fetchFileAsync(Object fetchInfo)
Attempts to asynchronously download a file. |
protected File |
getCacheFile(String name)
Retrieves cached file. |
protected File |
getCacheFileNull(String name)
Retrieves cached file. |
protected File |
getFile(Object fetchInfo)
Fetches a file. |
protected String[] |
getFilenameAndExt(URL url,
Object fetchInfo)
Retrieves target file name. |
Set<String> |
getNames()
Returns a list of names of objects cached in this storage. |
boolean |
isFull()
Returns true if this storage is full. |
protected boolean |
isProceedWithDownload(Object fetchInfo)
Invoked before the download process starts. |
protected void |
onFileDownloaded(URL url,
Object fetchInfo,
boolean success)
The file was downloaded and is already registered in the storage. |
void |
purge()
Purges the cache - removes all cached files. |
protected void |
registerFile(String name,
String ext)
Registers a file to the cache. |
protected void |
removeFile(File file)
Removes a file from the cache. |
void |
setMaxStorageSize(long maxCacheSize)
Sets maximum cache size, removing files if required. |
protected boolean |
supportsExtension(String ext)
Checks if this storage supports given extension. |
protected URL |
toURL(Object fetchInfo)
Converts given fetch info to an URL object. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected final Map<String,File> theCache
protected long maxStorageSize
Constructor Detail |
---|
public AbstractFileStorage(String contentDesc, String... ext)
ext
- a list of ".whatever" strings - valid extensions.contentDesc
- describes content which this storage holds. Should form a
correct sentence with
"Downloading " + contentDesc + " failed"
.Method Detail |
---|
protected final boolean supportsExtension(String ext)
ext
- the ".extension" format
true
if this storage supports such extensions,
false
otherwise.public final void setMaxStorageSize(long maxCacheSize)
maxCacheSize
- Maximum cache size, in bytes.public final boolean isFull()
true
if this storage is full.
true
if the size of all files in this storage
exceeds maximum storage size
.protected void cleanup()
setMaxStorageSize(long)
method.
protected final FileOutputStream createFile(String name, String ext) throws FileNotFoundException
Context.getFileStreamPath(String)
method.
name
- the file name, without the extensionext
- the ".ext" string.
FileNotFoundException
- if the file does not exist.protected final void registerFile(String name, String ext)
createFile(String, String)
.
name
- the file name, without the extensionext
- the ".ext" string.protected final void removeFile(File file)
Context.getFileStreamPath(String)
method.
file
- the fileprotected final File getCacheFile(String name)
name
- the name
protected final File getCacheFileNull(String name)
null
if no such file
exists.
name
- the name
public final void purge()
public Set<String> getNames()
protected final void fetchFileAsync(Object fetchInfo)
fetchInfo
- contains information on how to fetch the file.
toURL(Object)
is called asynchronously to convert the
fetch info object to an URL. This object must comply to the
contract imposed on a Map
key.protected URL toURL(Object fetchInfo) throws IOException
UnsupportedOperationException
. Must be thread-safe as it is not
invoked from a Handler thread.
fetchInfo
- the fetch info object, never null
.
null
then this download is aborted.
IOException
- if i/o error occursprotected boolean isProceedWithDownload(Object fetchInfo)
fetchInfo
- the fetch info object, never null
.
false
then this download is aborted.public void close()
protected void onFileDownloaded(URL url, Object fetchInfo, boolean success)
url
- the source URLfetchInfo
- the fetch info objectsuccess
- if true
then the file is available in the cache.
If false
then the download process was aborted (
toURL(Object)
returned null
).protected String[] getFilenameAndExt(URL url, Object fetchInfo)
UnsupportedOperationException
. Must be thread-safe as it is not
invoked from a Handler thread.
url
- the source URL. May be null
- in this case the
extension is not needed and returned extension may be
null
or the result may contain only the name.fetchInfo
- the fetch info object
null
is returned
then the object is not downloadable nor storable into the
storage.protected File getFile(Object fetchInfo)
null
.
fetchInfo
- the fetch info object
null
if we are unable to provide any file
now.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |