Commit fd979790 by olly Committed by Andrew Lewis

Some no-op CacheContentIndex cleanup

Push Cipher and SecretKeySpec instantiation into AtomicFileStorage,
since SQLiteStorage no longer needs them.

PiperOrigin-RevId: 233043754
parent 42e69151
...@@ -116,7 +116,7 @@ import org.checkerframework.checker.nullness.compatqual.NullableType; ...@@ -116,7 +116,7 @@ import org.checkerframework.checker.nullness.compatqual.NullableType;
* @param cacheDir Directory where the index file is kept. * @param cacheDir Directory where the index file is kept.
* @param secretKey 16 byte AES key for reading and writing the cache index. * @param secretKey 16 byte AES key for reading and writing the cache index.
*/ */
public CachedContentIndex(File cacheDir, byte[] secretKey) { public CachedContentIndex(File cacheDir, @Nullable byte[] secretKey) {
this(cacheDir, secretKey, secretKey != null); this(cacheDir, secretKey, secretKey != null);
} }
...@@ -128,27 +128,12 @@ import org.checkerframework.checker.nullness.compatqual.NullableType; ...@@ -128,27 +128,12 @@ import org.checkerframework.checker.nullness.compatqual.NullableType;
* @param encrypt Whether the index will be encrypted when written. Must be false if {@code * @param encrypt Whether the index will be encrypted when written. Must be false if {@code
* secretKey} is null. * secretKey} is null.
*/ */
public CachedContentIndex(File cacheDir, byte[] secretKey, boolean encrypt) { public CachedContentIndex(File cacheDir, @Nullable byte[] secretKey, boolean encrypt) {
Cipher cipher = null;
SecretKeySpec secretKeySpec = null;
if (secretKey != null) {
Assertions.checkArgument(secretKey.length == 16);
try {
cipher = getCipher();
secretKeySpec = new SecretKeySpec(secretKey, "AES");
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
throw new IllegalStateException(e); // Should never happen.
}
} else {
Assertions.checkState(!encrypt);
}
keyToContent = new HashMap<>(); keyToContent = new HashMap<>();
idToKey = new SparseArray<>(); idToKey = new SparseArray<>();
removedIds = new SparseBooleanArray(); removedIds = new SparseBooleanArray();
Random random = new Random();
Storage atomicFileStorage = Storage atomicFileStorage =
new AtomicFileStorage( new AtomicFileStorage(new File(cacheDir, FILE_NAME_ATOMIC), secretKey, encrypt);
new File(cacheDir, FILE_NAME_ATOMIC), random, encrypt, cipher, secretKeySpec);
// Storage sqliteStorage = new SQLiteStorage(databaseProvider); // Storage sqliteStorage = new SQLiteStorage(databaseProvider);
storage = atomicFileStorage; storage = atomicFileStorage;
previousStorage = null; previousStorage = null;
...@@ -430,25 +415,33 @@ import org.checkerframework.checker.nullness.compatqual.NullableType; ...@@ -430,25 +415,33 @@ import org.checkerframework.checker.nullness.compatqual.NullableType;
/** {@link Storage} implementation that uses an {@link AtomicFile}. */ /** {@link Storage} implementation that uses an {@link AtomicFile}. */
private static class AtomicFileStorage implements Storage { private static class AtomicFileStorage implements Storage {
private final Random random;
private final boolean encrypt; private final boolean encrypt;
@Nullable private final Cipher cipher; @Nullable private final Cipher cipher;
@Nullable private final SecretKeySpec secretKeySpec; @Nullable private final SecretKeySpec secretKeySpec;
@Nullable private final Random random;
private final AtomicFile atomicFile; private final AtomicFile atomicFile;
private boolean changed; private boolean changed;
@Nullable private ReusableBufferedOutputStream bufferedOutputStream; @Nullable private ReusableBufferedOutputStream bufferedOutputStream;
public AtomicFileStorage( public AtomicFileStorage(File file, @Nullable byte[] secretKey, boolean encrypt) {
File file, Cipher cipher = null;
Random random, SecretKeySpec secretKeySpec = null;
boolean encrypt, if (secretKey != null) {
@Nullable Cipher cipher, Assertions.checkArgument(secretKey.length == 16);
@Nullable SecretKeySpec secretKeySpec) { try {
this.random = random; cipher = getCipher();
secretKeySpec = new SecretKeySpec(secretKey, "AES");
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
throw new IllegalStateException(e); // Should never happen.
}
} else {
Assertions.checkArgument(!encrypt);
}
this.encrypt = encrypt; this.encrypt = encrypt;
this.cipher = cipher; this.cipher = cipher;
this.secretKeySpec = secretKeySpec; this.secretKeySpec = secretKeySpec;
random = encrypt ? new Random() : null;
atomicFile = new AtomicFile(file); atomicFile = new AtomicFile(file);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment