From a6e277011617ff0b466925d08a4f98c14143d09d Mon Sep 17 00:00:00 2001 From: eguven Date: Mon, 7 Nov 2016 05:14:38 -0800 Subject: [PATCH] Upgrade SimpleCacheSpan files during createCacheEntry call. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=138379386 --- .../upstream/cache/CacheDataSourceTest.java | 2 +- .../upstream/cache/SimpleCacheSpanTest.java | 4 +- .../upstream/cache/SimpleCache.java | 9 +--- .../upstream/cache/SimpleCacheSpan.java | 42 +++++++++---------- 4 files changed, 26 insertions(+), 31 deletions(-) diff --git a/library/src/androidTest/java/com/google/android/exoplayer2/upstream/cache/CacheDataSourceTest.java b/library/src/androidTest/java/com/google/android/exoplayer2/upstream/cache/CacheDataSourceTest.java index c0d9570d7a..18e39be93c 100644 --- a/library/src/androidTest/java/com/google/android/exoplayer2/upstream/cache/CacheDataSourceTest.java +++ b/library/src/androidTest/java/com/google/android/exoplayer2/upstream/cache/CacheDataSourceTest.java @@ -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); } } diff --git a/library/src/androidTest/java/com/google/android/exoplayer2/upstream/cache/SimpleCacheSpanTest.java b/library/src/androidTest/java/com/google/android/exoplayer2/upstream/cache/SimpleCacheSpanTest.java index 6ccfc9dee9..a4fbb2af4d 100644 --- a/library/src/androidTest/java/com/google/android/exoplayer2/upstream/cache/SimpleCacheSpanTest.java +++ b/library/src/androidTest/java/com/google/android/exoplayer2/upstream/cache/SimpleCacheSpanTest.java @@ -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()); diff --git a/library/src/main/java/com/google/android/exoplayer2/upstream/cache/SimpleCache.java b/library/src/main/java/com/google/android/exoplayer2/upstream/cache/SimpleCache.java index f21929a748..53a44a5797 100644 --- a/library/src/main/java/com/google/android/exoplayer2/upstream/cache/SimpleCache.java +++ b/library/src/main/java/com/google/android/exoplayer2/upstream/cache/SimpleCache.java @@ -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) { diff --git a/library/src/main/java/com/google/android/exoplayer2/upstream/cache/SimpleCacheSpan.java b/library/src/main/java/com/google/android/exoplayer2/upstream/cache/SimpleCacheSpan.java index deac524e2a..47aefc7820 100644 --- a/library/src/main/java/com/google/android/exoplayer2/upstream/cache/SimpleCacheSpan.java +++ b/library/src/main/java/com/google/android/exoplayer2/upstream/cache/SimpleCacheSpan.java @@ -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,