Skip to content

Commit d977932

Browse files
committed
add error cache listener
1 parent acdf747 commit d977932

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.danikula.videocache;
2+
3+
import java.io.File;
4+
5+
/**
6+
* Listener for cache error.
7+
*
8+
* @author Sasha Karamyshev (sheckspir88@gmail.com)
9+
*/
10+
public interface ErrorCacheListener {
11+
12+
void onCacheUnavailable(Throwable throwable);
13+
}

Diff for: library/src/main/java/com/danikula/videocache/HttpProxyCache.java

+17-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ class HttpProxyCache extends ProxyCache {
2323

2424
private final HttpUrlSource source;
2525
private final FileCache cache;
26-
private CacheListener listener;
26+
private CacheListener cacheListener;
27+
private ErrorCacheListener errorListener;
2728

2829
public HttpProxyCache(HttpUrlSource source, FileCache cache) {
2930
super(source, cache);
@@ -32,7 +33,11 @@ public HttpProxyCache(HttpUrlSource source, FileCache cache) {
3233
}
3334

3435
public void registerCacheListener(CacheListener cacheListener) {
35-
this.listener = cacheListener;
36+
this.cacheListener = cacheListener;
37+
}
38+
39+
public void registerErrorListener(ErrorCacheListener errorCacheListener) {
40+
this.errorListener = errorCacheListener;
3641
}
3742

3843
public void processRequest(GetRequest request, Socket socket) throws IOException, ProxyCacheException {
@@ -105,8 +110,16 @@ private String format(String pattern, Object... args) {
105110

106111
@Override
107112
protected void onCachePercentsAvailableChanged(int percents) {
108-
if (listener != null) {
109-
listener.onCacheAvailable(cache.file, source.getUrl(), percents);
113+
if (cacheListener != null) {
114+
cacheListener.onCacheAvailable(cache.file, source.getUrl(), percents);
115+
}
116+
}
117+
118+
@Override
119+
protected void onErrorCache(Throwable throwable) {
120+
super.onErrorCache(throwable);
121+
if (errorListener != null) {
122+
errorListener.onCacheUnavailable(throwable);
110123
}
111124
}
112125
}

Diff for: library/src/main/java/com/danikula/videocache/ProxyCache.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ private void notifyNewCacheDataAvailable(long cacheAvailable, long sourceAvailab
101101
}
102102
}
103103

104+
private void notifyCacheError(Throwable throwable){
105+
onErrorCache(throwable);
106+
}
107+
104108
protected void onCacheAvailable(long cacheAvailable, long sourceLength) {
105109
boolean zeroLengthSource = sourceLength == 0;
106110
int percents = zeroLengthSource ? 100 : (int) ((float) cacheAvailable / sourceLength * 100);
@@ -115,6 +119,10 @@ protected void onCacheAvailable(long cacheAvailable, long sourceLength) {
115119
protected void onCachePercentsAvailableChanged(int percentsAvailable) {
116120
}
117121

122+
protected void onErrorCache(Throwable throwable) {
123+
// do nothing
124+
}
125+
118126
private void readSource() {
119127
long sourceAvailable = -1;
120128
long offset = 0;
@@ -139,9 +147,12 @@ private void readSource() {
139147
} catch (Throwable e) {
140148
readSourceErrorsCount.incrementAndGet();
141149
onError(e);
150+
notifyCacheError(e);
142151
} finally {
143152
closeSource();
144-
notifyNewCacheDataAvailable(offset, sourceAvailable);
153+
synchronized (wc) {
154+
wc.notifyAll();
155+
}
145156
}
146157
}
147158

0 commit comments

Comments
 (0)