Package com.android.builder.files
Class KeyedFileCache
java.lang.Object
com.android.builder.files.KeyedFileCache
File cache that stored files based on the given key function. The general contract of the
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 Summary
ConstructorsConstructorDescriptionKeyedFileCache(File directory, Function<File, String> keyFunction) Creates a new cache. -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(com.android.builder.files.ZipCentralDirectory centralDirectory) Adds a file to the cache, replacing any file that had the exact same absolute path.voidAdds a file to the cache, replacing any file that had the exact same absolute path.voidclear()Clears the cache.static StringfileNameKey(File f) Computes a unique key identifying the path of the file.Obtains the cached file corresponding to the file with the given path.voidRemoves any cached version of the given path.
-
Constructor Details
-
KeyedFileCache
Creates a new cache.- Parameters:
directory- the directory where the cache is storedkeyFunction- a function that maps a file to its location in the cache. SeefileNameKey(File)for one example.
-
-
Method Details
-
add
Adds a file to the cache, replacing any file that had the exact same absolute path.- Parameters:
f- the file to add- Throws:
IOException- failed to copy the file into the cache
-
add
public void add(@NonNull com.android.builder.files.ZipCentralDirectory centralDirectory) throws IOException Adds a file to the cache, replacing any file that had the exact same absolute path.- Parameters:
centralDirectory- the file to add- Throws:
IOException- failed to copy the file into the cache
-
get
Obtains the cached file corresponding to the file with the given path.- Parameters:
f- the path- Returns:
- the cached file,
nullif there is no file in the cache that corresponds to the given file
-
remove
Removes any cached version of the given path.- Parameters:
f- the path- Throws:
IOException- failed to remove the file
-
fileNameKey
Computes a unique key identifying the path of the file.WARNING: this is dangerous to use with normalized gradle inputs that discard the absolute path of files.
- Parameters:
f- the path- Returns:
- the unique key
-
clear
Clears the cache.- Throws:
IOException- failed to clear the cache
-