From c380ba2dc1ce891b6e82708cb994ef55aaf1e30d Mon Sep 17 00:00:00 2001 From: eguven Date: Fri, 12 Aug 2016 03:15:41 -0700 Subject: [PATCH] CacheSpan.isEOS (is end of stream) field shows whether the {@link CacheSpan} contains the end of the original stream. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=130084365 --- .../upstream/cache/CacheSpanTest.java | 21 +++++++----- .../exoplayer2/upstream/cache/CacheSpan.java | 34 +++++++++++-------- .../upstream/cache/SimpleCache.java | 2 +- 3 files changed, 33 insertions(+), 24 deletions(-) diff --git a/library/src/androidTest/java/com/google/android/exoplayer2/upstream/cache/CacheSpanTest.java b/library/src/androidTest/java/com/google/android/exoplayer2/upstream/cache/CacheSpanTest.java index 38008c814e..4d000c7619 100644 --- a/library/src/androidTest/java/com/google/android/exoplayer2/upstream/cache/CacheSpanTest.java +++ b/library/src/androidTest/java/com/google/android/exoplayer2/upstream/cache/CacheSpanTest.java @@ -26,10 +26,10 @@ import junit.framework.TestCase; public class CacheSpanTest extends TestCase { public void testCacheFile() throws Exception { - assertCacheSpan(new File("parent"), "key", 0, 0); - assertCacheSpan(new File("parent/"), "key", 1, 2); - assertCacheSpan(new File("parent"), "<>:\"/\\|?*%", 1, 2); - assertCacheSpan(new File("/"), "key", 1, 2); + assertCacheSpan(new File("parent"), "key", 0, 0, true); + assertCacheSpan(new File("parent/"), "key", 1, 2, false); + assertCacheSpan(new File("parent"), "<>:\"/\\|?*%", 1, 2, true); + assertCacheSpan(new File("/"), "key", 1, 2, false); assertNullCacheSpan(new File("parent"), "", 1, 2); assertNullCacheSpan(new File("parent"), "key", -1, 2); @@ -44,7 +44,7 @@ public class CacheSpanTest extends TestCase { + "A standalone carriage-return character \r" + "A next-line character \u0085" + "A line-separator character \u2028" - + "A paragraph-separator character \u2029", 1, 2); + + "A paragraph-separator character \u2029", 1, 2, true); } public void testCacheFileNameRandomData() throws Exception { @@ -54,12 +54,14 @@ public class CacheSpanTest extends TestCase { String key = TestUtil.buildTestString(1000, random); long offset = Math.abs(random.nextLong()); long lastAccessTimestamp = Math.abs(random.nextLong()); - assertCacheSpan(parent, key, offset, lastAccessTimestamp); + boolean isEOS = random.nextBoolean(); + assertCacheSpan(parent, key, offset, lastAccessTimestamp, isEOS); } } - private void assertCacheSpan(File parent, String key, long offset, long lastAccessTimestamp) { - File cacheFile = CacheSpan.getCacheFileName(parent, key, offset, lastAccessTimestamp); + private void assertCacheSpan(File parent, String key, long offset, long lastAccessTimestamp, + boolean isEOS) { + File cacheFile = CacheSpan.getCacheFileName(parent, key, offset, lastAccessTimestamp, isEOS); CacheSpan cacheSpan = CacheSpan.createCacheEntry(cacheFile); String message = cacheFile.toString(); assertNotNull(message, cacheSpan); @@ -67,11 +69,12 @@ public class CacheSpanTest extends TestCase { assertEquals(message, key, cacheSpan.key); assertEquals(message, offset, cacheSpan.position); assertEquals(message, lastAccessTimestamp, cacheSpan.lastAccessTimestamp); + assertEquals(message, isEOS, cacheSpan.isEOS); } private void assertNullCacheSpan(File parent, String key, long offset, long lastAccessTimestamp) { - File cacheFile = CacheSpan.getCacheFileName(parent, key, offset, lastAccessTimestamp); + File cacheFile = CacheSpan.getCacheFileName(parent, key, offset, lastAccessTimestamp, false); CacheSpan cacheSpan = CacheSpan.createCacheEntry(cacheFile); assertNull(cacheFile.toString(), cacheSpan); } diff --git a/library/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheSpan.java b/library/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheSpan.java index 363e19a1d5..1d07ffc10a 100644 --- a/library/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheSpan.java +++ b/library/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheSpan.java @@ -29,7 +29,7 @@ public final class CacheSpan implements Comparable { private static final Pattern CACHE_FILE_PATTERN_V1 = Pattern.compile("^(.+)\\.(\\d+)\\.(\\d+)\\.v1\\.exo$", Pattern.DOTALL); private static final Pattern CACHE_FILE_PATTERN_V2 = - Pattern.compile("^(.+)\\.(\\d+)\\.(\\d+)\\.v2\\.exo$", Pattern.DOTALL); + Pattern.compile("^(.+)\\.(\\d+)(E?)\\.(\\d+)\\.v2\\.exo$", Pattern.DOTALL); /** * The cache key that uniquely identifies the original stream. @@ -55,23 +55,27 @@ public final class CacheSpan implements Comparable { * The last access timestamp, or -1 if {@link #isCached} is false. */ public final long lastAccessTimestamp; + /** + * Whether the {@link CacheSpan} is know to contain the end of the original stream. + */ + public final boolean isEOS; public static File getCacheFileName(File cacheDir, String key, long offset, - long lastAccessTimestamp) { - return new File(cacheDir, - Util.escapeFileName(key) + "." + offset + "." + lastAccessTimestamp + SUFFIX); + long lastAccessTimestamp, boolean isEOS) { + return new File(cacheDir, Util.escapeFileName(key) + "." + offset + (isEOS ? "E" : "") + "." + + lastAccessTimestamp + SUFFIX); } public static CacheSpan createLookup(String key, long position) { - return new CacheSpan(key, position, -1, false, -1, null); + return new CacheSpan(key, position, -1, false, -1, null, false); } public static CacheSpan createOpenHole(String key, long position) { - return new CacheSpan(key, position, -1, false, -1, null); + return new CacheSpan(key, position, -1, false, -1, null, false); } public static CacheSpan createClosedHole(String key, long position, long length) { - return new CacheSpan(key, position, length, false, -1, null); + return new CacheSpan(key, position, length, false, -1, null, false); } /** @@ -87,7 +91,8 @@ public final class CacheSpan implements Comparable { } String key = Util.unescapeFileName(matcher.group(1)); return key == null ? null : createCacheEntry( - key, Long.parseLong(matcher.group(2)), Long.parseLong(matcher.group(3)), file); + key, Long.parseLong(matcher.group(2)), Long.parseLong(matcher.group(4)), file, + "E".equals(matcher.group(3))); } static File upgradeIfNeeded(File file) { @@ -97,25 +102,26 @@ public final class CacheSpan implements Comparable { } String key = matcher.group(1); // Keys were not escaped in version 1. File newCacheFile = getCacheFileName(file.getParentFile(), key, - Long.parseLong(matcher.group(2)), Long.parseLong(matcher.group(3))); + Long.parseLong(matcher.group(2)), Long.parseLong(matcher.group(3)), false); file.renameTo(newCacheFile); return newCacheFile; } private static CacheSpan createCacheEntry(String key, long position, long lastAccessTimestamp, - File file) { - return new CacheSpan(key, position, file.length(), true, lastAccessTimestamp, file); + File file, boolean isEOS) { + return new CacheSpan(key, position, file.length(), true, lastAccessTimestamp, file, isEOS); } // Visible for testing. CacheSpan(String key, long position, long length, boolean isCached, - long lastAccessTimestamp, File file) { + long lastAccessTimestamp, File file, boolean isEOS) { this.key = key; this.position = position; this.length = length; this.isCached = isCached; this.file = file; this.lastAccessTimestamp = lastAccessTimestamp; + this.isEOS = isEOS; } /** @@ -132,9 +138,9 @@ public final class CacheSpan implements Comparable { */ public CacheSpan touch() { long now = System.currentTimeMillis(); - File newCacheFile = getCacheFileName(file.getParentFile(), key, position, now); + File newCacheFile = getCacheFileName(file.getParentFile(), key, position, now, isEOS); file.renameTo(newCacheFile); - return CacheSpan.createCacheEntry(key, position, now, newCacheFile); + return createCacheEntry(key, position, now, newCacheFile, isEOS); } @Override 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 ab36497810..0def07c537 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 @@ -162,7 +162,7 @@ public final class SimpleCache implements Cache { cacheDir.mkdirs(); } evictor.onStartFile(this, key, position, length); - return CacheSpan.getCacheFileName(cacheDir, key, position, System.currentTimeMillis()); + return CacheSpan.getCacheFileName(cacheDir, key, position, System.currentTimeMillis(), false); } @Override