mirror of
https://github.com/androidx/media.git
synced 2025-05-04 14:10:40 +08:00
Upgrade SimpleCacheSpan files during createCacheEntry call.
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=138379386
This commit is contained in:
parent
ff77d1e72c
commit
a6e2770116
@ -54,7 +54,7 @@ public class CacheDataSourceTest extends InstrumentationTestCase {
|
||||
assertReadDataContentLength(cacheDataSource, false, false);
|
||||
File[] files = cacheDir.listFiles();
|
||||
for (File file : files) {
|
||||
if (file.getName().endsWith(SimpleCacheSpan.SUFFIX)) {
|
||||
if (!file.getName().equals(CachedContentIndex.FILE_NAME)) {
|
||||
assertTrue(file.length() <= MAX_CACHE_FILE_SIZE);
|
||||
}
|
||||
}
|
||||
|
@ -83,7 +83,9 @@ public class SimpleCacheSpanTest extends InstrumentationTestCase {
|
||||
File wrongEscapedV2file = createTestFile("asd%za.3.4.v2.exo");
|
||||
File v1File = createTestFile("asd\u00aa.5.6.v1.exo");
|
||||
|
||||
SimpleCacheSpan.upgradeOldFiles(cacheDir, index);
|
||||
for (File file : cacheDir.listFiles()) {
|
||||
SimpleCacheSpan.createCacheEntry(file, index);
|
||||
}
|
||||
|
||||
assertTrue(v3file.exists());
|
||||
assertFalse(v2file.exists());
|
||||
|
@ -227,21 +227,14 @@ public final class SimpleCache implements Cache {
|
||||
|
||||
index.load();
|
||||
|
||||
SimpleCacheSpan.upgradeOldFiles(cacheDir, index);
|
||||
|
||||
File[] files = cacheDir.listFiles();
|
||||
if (files == null) {
|
||||
return;
|
||||
}
|
||||
for (File file : files) {
|
||||
String name = file.getName();
|
||||
if (!name.endsWith(SimpleCacheSpan.SUFFIX)) {
|
||||
if (!name.equals(CachedContentIndex.FILE_NAME)) {
|
||||
file.delete(); // Delete unknown files
|
||||
}
|
||||
if (file.getName().equals(CachedContentIndex.FILE_NAME)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
SimpleCacheSpan span = file.length() > 0
|
||||
? SimpleCacheSpan.createCacheEntry(file, index) : null;
|
||||
if (span != null) {
|
||||
|
@ -27,14 +27,13 @@ import java.util.regex.Pattern;
|
||||
*/
|
||||
/*package*/ final class SimpleCacheSpan extends CacheSpan {
|
||||
|
||||
private static final String FILE_EXTENSION = "exo";
|
||||
public static final String SUFFIX = ".v3." + FILE_EXTENSION;
|
||||
private static final String SUFFIX = ".v3.exo";
|
||||
private static final Pattern CACHE_FILE_PATTERN_V1 = Pattern.compile(
|
||||
"^(.+)\\.(\\d+)\\.(\\d+)\\.v1\\." + FILE_EXTENSION + "$", Pattern.DOTALL);
|
||||
"^(.+)\\.(\\d+)\\.(\\d+)\\.v1\\.exo$", Pattern.DOTALL);
|
||||
private static final Pattern CACHE_FILE_PATTERN_V2 = Pattern.compile(
|
||||
"^(.+)\\.(\\d+)\\.(\\d+)\\.v2\\." + FILE_EXTENSION + "$", Pattern.DOTALL);
|
||||
"^(.+)\\.(\\d+)\\.(\\d+)\\.v2\\.exo$", Pattern.DOTALL);
|
||||
private static final Pattern CACHE_FILE_PATTERN_V3 = Pattern.compile(
|
||||
"^(\\d+)\\.(\\d+)\\.(\\d+)\\.v3\\." + FILE_EXTENSION + "$", Pattern.DOTALL);
|
||||
"^(\\d+)\\.(\\d+)\\.(\\d+)\\.v3\\.exo$", Pattern.DOTALL);
|
||||
|
||||
public static File getCacheFile(File cacheDir, int id, long position,
|
||||
long lastAccessTimestamp) {
|
||||
@ -54,7 +53,7 @@ import java.util.regex.Pattern;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a cache span from an underlying cache file.
|
||||
* Creates a cache span from an underlying cache file. Upgrades the file if necessary.
|
||||
*
|
||||
* @param file The cache file.
|
||||
* @param index Cached content index.
|
||||
@ -62,7 +61,15 @@ import java.util.regex.Pattern;
|
||||
* present in the content index.
|
||||
*/
|
||||
public static SimpleCacheSpan createCacheEntry(File file, CachedContentIndex index) {
|
||||
Matcher matcher = CACHE_FILE_PATTERN_V3.matcher(file.getName());
|
||||
String name = file.getName();
|
||||
if (!name.endsWith(SUFFIX)) {
|
||||
file = upgradeFile(file, index);
|
||||
if (file == null) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Matcher matcher = CACHE_FILE_PATTERN_V3.matcher(name);
|
||||
if (!matcher.matches()) {
|
||||
return null;
|
||||
}
|
||||
@ -73,36 +80,29 @@ import java.util.regex.Pattern;
|
||||
Long.parseLong(matcher.group(3)), file);
|
||||
}
|
||||
|
||||
/** Upgrades span files with old versions. */
|
||||
public static void upgradeOldFiles(File cacheDir, CachedContentIndex index) {
|
||||
for (File file : cacheDir.listFiles()) {
|
||||
String name = file.getName();
|
||||
if (!name.endsWith(SUFFIX) && name.endsWith(FILE_EXTENSION)) {
|
||||
upgradeFile(file, index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void upgradeFile(File file, CachedContentIndex index) {
|
||||
private static File upgradeFile(File file, CachedContentIndex index) {
|
||||
String key;
|
||||
String filename = file.getName();
|
||||
Matcher matcher = CACHE_FILE_PATTERN_V2.matcher(filename);
|
||||
if (matcher.matches()) {
|
||||
key = Util.unescapeFileName(matcher.group(1));
|
||||
if (key == null) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
matcher = CACHE_FILE_PATTERN_V1.matcher(filename);
|
||||
if (!matcher.matches()) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
key = matcher.group(1); // Keys were not escaped in version 1.
|
||||
}
|
||||
|
||||
File newCacheFile = getCacheFile(file.getParentFile(), index.assignIdForKey(key),
|
||||
Long.parseLong(matcher.group(2)), Long.parseLong(matcher.group(3)));
|
||||
file.renameTo(newCacheFile);
|
||||
if (!file.renameTo(newCacheFile)) {
|
||||
return null;
|
||||
}
|
||||
return newCacheFile;
|
||||
}
|
||||
|
||||
private SimpleCacheSpan(String key, long position, long length, long lastAccessTimestamp,
|
||||
|
Loading…
x
Reference in New Issue
Block a user