Class KeyedFileCache

java.lang.Object
com.android.builder.files.KeyedFileCache

public class KeyedFileCache extends Object
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

    Constructors
    Constructor
    Description
    KeyedFileCache(File directory, Function<File,String> keyFunction)
    Creates a new cache.
  • Method Summary

    Modifier and Type
    Method
    Description
    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
    add(File f)
    Adds a file to the cache, replacing any file that had the exact same absolute path.
    void
    Clears the cache.
    static String
    Computes a unique key identifying the path of the file.
    get(File f)
    Obtains the cached file corresponding to the file with the given path.
    void
    Removes any cached version of the given path.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • KeyedFileCache

      public KeyedFileCache(@NonNull File directory, @NonNull Function<File,String> keyFunction)
      Creates a new cache.
      Parameters:
      directory - the directory where the cache is stored
      keyFunction - a function that maps a file to its location in the cache. See fileNameKey(File) for one example.
  • Method Details

    • add

      public void add(@NonNull File f) throws IOException
      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

      @Nullable public File get(@NonNull File f)
      Obtains the cached file corresponding to the file with the given path.
      Parameters:
      f - the path
      Returns:
      the cached file, null if there is no file in the cache that corresponds to the given file
    • remove

      public void remove(@NonNull File f) throws IOException
      Removes any cached version of the given path.
      Parameters:
      f - the path
      Throws:
      IOException - failed to remove the file
    • fileNameKey

      @NonNull public static String fileNameKey(@NonNull File f)
      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

      public void clear() throws IOException
      Clears the cache.
      Throws:
      IOException - failed to clear the cache