Break the inheritance of FullSegmentEncryptionKeyCache

Use composition instead.

This makes the null-handling more explicit & complete (previous implementation
tried to prevent null values, but didn't override all mutation methods
e.g. replace(), putIfAbsent() etc.)

PiperOrigin-RevId: 274778513
This commit is contained in:
ibaker 2019-10-15 12:46:22 +01:00 committed by Oliver Woodman
parent f133766b40
commit 0ba91811d1

View File

@ -43,7 +43,6 @@ import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* Source of Hls (possibly adaptive) chunks. * Source of Hls (possibly adaptive) chunks.
@ -505,7 +504,7 @@ import java.util.Map;
if (keyUri == null) { if (keyUri == null) {
return null; return null;
} }
if (keyCache.containsKey(keyUri)) { if (keyCache.containsUri(keyUri)) {
// The key is present in the key cache. We re-insert it to prevent it from being evicted by // The key is present in the key cache. We re-insert it to prevent it from being evicted by
// the following key addition. Note that removal of the key is necessary to affect the // the following key addition. Note that removal of the key is necessary to affect the
// eviction order. // eviction order.
@ -660,29 +659,63 @@ import java.util.Map;
* addition, once the cache's size exceeds {@link #KEY_CACHE_SIZE}, the oldest item (according to * addition, once the cache's size exceeds {@link #KEY_CACHE_SIZE}, the oldest item (according to
* insertion order) is removed. * insertion order) is removed.
*/ */
private static final class FullSegmentEncryptionKeyCache extends LinkedHashMap<Uri, byte[]> { private static final class FullSegmentEncryptionKeyCache {
private final LinkedHashMap<Uri, byte[]> backingMap;
public FullSegmentEncryptionKeyCache() { public FullSegmentEncryptionKeyCache() {
super( backingMap =
/* initialCapacity= */ KEY_CACHE_SIZE * 2, /* loadFactor= */ 1, /* accessOrder= */ false); new LinkedHashMap<Uri, byte[]>(
/* initialCapacity= */ KEY_CACHE_SIZE + 1,
/* loadFactor= */ 1,
/* accessOrder= */ false) {
@Override
protected boolean removeEldestEntry(Entry<Uri, byte[]> eldest) {
return size() > KEY_CACHE_SIZE;
}
};
} }
@Override /**
public byte[] get(Object keyUri) { * Returns the {@code encryptionKey} cached against this {@code uri}, or null if {@code uri} is
if (keyUri == null) { * null or not present in the cache.
*/
@Nullable
public byte[] get(@Nullable Uri uri) {
if (uri == null) {
return null; return null;
} }
return super.get(keyUri); return backingMap.get(uri);
} }
@Override /**
public byte[] put(Uri keyUri, byte[] key) { * Inserts an entry into the cache.
return super.put(keyUri, Assertions.checkNotNull(key)); *
* @throws NullPointerException if {@code uri} or {@code encryptionKey} are null.
*/
@Nullable
public byte[] put(Uri uri, byte[] encryptionKey) {
return backingMap.put(Assertions.checkNotNull(uri), Assertions.checkNotNull(encryptionKey));
} }
@Override /**
protected boolean removeEldestEntry(Map.Entry<Uri, byte[]> entry) { * Returns true if {@code uri} is present in the cache.
return size() > KEY_CACHE_SIZE; *
* @throws NullPointerException if {@code uri} is null.
*/
public boolean containsUri(Uri uri) {
return backingMap.containsKey(Assertions.checkNotNull(uri));
}
/**
* Removes {@code uri} from the cache. If {@code uri} was present in the cahce, this returns the
* corresponding {@code encryptionKey}, otherwise null.
*
* @throws NullPointerException if {@code uri} is null.
*/
@Nullable
public byte[] remove(Uri uri) {
return backingMap.remove(Assertions.checkNotNull(uri));
} }
} }
} }