mirror of
https://github.com/samsonjs/media.git
synced 2026-04-27 15:07:40 +00:00
Add percentage field to CacheUtil.CachingCounters
PiperOrigin-RevId: 241524766
This commit is contained in:
parent
9aacef47ad
commit
d5b19694fa
2 changed files with 15 additions and 4 deletions
|
|
@ -103,10 +103,7 @@ public final class ProgressiveDownloader implements Downloader {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public float getDownloadPercentage() {
|
public float getDownloadPercentage() {
|
||||||
long contentLength = cachingCounters.contentLength;
|
return cachingCounters.percentage;
|
||||||
return contentLength == C.LENGTH_UNSET
|
|
||||||
? C.PERCENTAGE_UNSET
|
|
||||||
: ((cachingCounters.totalCachedBytes() * 100f) / contentLength);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -42,6 +42,8 @@ public final class CacheUtil {
|
||||||
public volatile long newlyCachedBytes;
|
public volatile long newlyCachedBytes;
|
||||||
/** The length of the content being cached in bytes, or {@link C#LENGTH_UNSET} if unknown. */
|
/** The length of the content being cached in bytes, or {@link C#LENGTH_UNSET} if unknown. */
|
||||||
public volatile long contentLength = C.LENGTH_UNSET;
|
public volatile long contentLength = C.LENGTH_UNSET;
|
||||||
|
/** The percentage of cached data, or {@link C#PERCENTAGE_UNSET} if unavailable. */
|
||||||
|
public volatile float percentage;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the sum of {@link #alreadyCachedBytes} and {@link #newlyCachedBytes}.
|
* Returns the sum of {@link #alreadyCachedBytes} and {@link #newlyCachedBytes}.
|
||||||
|
|
@ -49,6 +51,16 @@ public final class CacheUtil {
|
||||||
public long totalCachedBytes() {
|
public long totalCachedBytes() {
|
||||||
return alreadyCachedBytes + newlyCachedBytes;
|
return alreadyCachedBytes + newlyCachedBytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Updates {@link #percentage} value using other values. */
|
||||||
|
public void updatePercentage() {
|
||||||
|
// Take local snapshot of the volatile field
|
||||||
|
long contentLength = this.contentLength;
|
||||||
|
percentage =
|
||||||
|
contentLength == C.LENGTH_UNSET
|
||||||
|
? C.PERCENTAGE_UNSET
|
||||||
|
: ((totalCachedBytes() * 100f) / contentLength);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Default buffer size to be used while caching. */
|
/** Default buffer size to be used while caching. */
|
||||||
|
|
@ -105,6 +117,7 @@ public final class CacheUtil {
|
||||||
start += blockLength;
|
start += blockLength;
|
||||||
left -= left == C.LENGTH_UNSET ? 0 : blockLength;
|
left -= left == C.LENGTH_UNSET ? 0 : blockLength;
|
||||||
}
|
}
|
||||||
|
counters.updatePercentage();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -293,6 +306,7 @@ public final class CacheUtil {
|
||||||
}
|
}
|
||||||
totalRead += read;
|
totalRead += read;
|
||||||
counters.newlyCachedBytes += read;
|
counters.newlyCachedBytes += read;
|
||||||
|
counters.updatePercentage();
|
||||||
}
|
}
|
||||||
return totalRead;
|
return totalRead;
|
||||||
} catch (PriorityTaskManager.PriorityTooLowException exception) {
|
} catch (PriorityTaskManager.PriorityTooLowException exception) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue