public class KeyedFileCache
extends java.lang.Object
KeyedFileCache is that files are stored and can be later retrieved using the file to derive a
unique key using the given key function. For example:
File cacheDir = ... // some directory.
KeyedFileCache cache = new KeyedFileCache(cacheDir, KeyedFileCache::fileNameKey);
File a = new File(...); // some file in the filesystem.
cache.add(a);
// Modify file "a".
File b = cache.get(a); // "b" will be a different file whose
// contents are those of "a" before
// being modified.
A custom API for zip files (add(ZipCentralDirectory) allows to only store a zip file's
Central Directory Record as this is generally only what is needed from the cache.| Constructor and Description |
|---|
KeyedFileCache(java.io.File directory,
java.util.function.Function<java.io.File,java.lang.String> keyFunction)
Creates a new cache.
|
| Modifier and Type | Method and Description |
|---|---|
void |
add(java.io.File f)
Adds a file to the cache, replacing any file that had the exact same absolute path.
|
void |
add(com.android.builder.files.ZipCentralDirectory centralDirectory)
Adds a file to the cache, replacing any file that had the exact same absolute path.
|
void |
clear()
Clears the cache.
|
static java.lang.String |
fileNameKey(java.io.File f)
Computes a unique key identifying the path of the file.
|
java.io.File |
get(java.io.File f)
Obtains the cached file corresponding to the file with the given path.
|
void |
remove(java.io.File f)
Removes any cached version of the given path.
|
public KeyedFileCache(@NonNull
java.io.File directory,
@NonNull
java.util.function.Function<java.io.File,java.lang.String> keyFunction)
directory - the directory where the cache is storedkeyFunction - a function that maps a file to its location in the cache. See fileNameKey(File) for one example.public void add(@NonNull
java.io.File f)
throws java.io.IOException
f - the file to addjava.io.IOException - failed to copy the file into the cachepublic void add(@NonNull
com.android.builder.files.ZipCentralDirectory centralDirectory)
throws java.io.IOException
centralDirectory - the file to addjava.io.IOException - failed to copy the file into the cache@Nullable
public java.io.File get(@NonNull
java.io.File f)
f - the pathnull if there is no file in the cache that corresponds to
the given filepublic void remove(@NonNull
java.io.File f)
throws java.io.IOException
f - the pathjava.io.IOException - failed to remove the file@NonNull
public static java.lang.String fileNameKey(@NonNull
java.io.File f)
WARNING: this is dangerous to use with normalized gradle inputs that discard the absolute path of files.
f - the pathpublic void clear()
throws java.io.IOException
java.io.IOException - failed to clear the cache