mirror of
https://github.com/zhigang1992/AndroidVideoCache.git
synced 2026-06-15 18:07:51 +08:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
33b7e5e9d3 | ||
|
|
0aad13f118 | ||
|
|
a270460c8d |
@@ -21,7 +21,7 @@ repositories {
|
||||
jcenter()
|
||||
}
|
||||
dependencies {
|
||||
compile 'com.danikula:videocache:2.3.2'
|
||||
compile 'com.danikula:videocache:2.3.3'
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ publish {
|
||||
userOrg = 'alexeydanilov'
|
||||
groupId = 'com.danikula'
|
||||
artifactId = 'videocache'
|
||||
publishVersion = '2.3.2'
|
||||
publishVersion = '2.3.3'
|
||||
description = 'Cache support for android VideoView'
|
||||
website = 'https://github.com/danikula/AndroidVideoCache'
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ public class HttpProxyCacheServer {
|
||||
|
||||
private void makeSureServerWorks() {
|
||||
int maxPingAttempts = 3;
|
||||
int delay = 200;
|
||||
int delay = 300;
|
||||
int pingAttempts = 0;
|
||||
while (pingAttempts < maxPingAttempts) {
|
||||
try {
|
||||
@@ -107,7 +107,7 @@ public class HttpProxyCacheServer {
|
||||
pingAttempts++;
|
||||
delay *= 2;
|
||||
}
|
||||
Log.e(LOG_TAG, "Shutdown server… Error pinging server [attempt: " + pingAttempts + ", timeout: " + delay + "]. " +
|
||||
Log.e(LOG_TAG, "Shutdown server… Error pinging server [attempts: " + pingAttempts + ", max timeout: " + delay / 2 + "]. " +
|
||||
"If you see this message, please, email me danikula@gmail.com");
|
||||
shutdown();
|
||||
}
|
||||
|
||||
@@ -58,7 +58,7 @@ public class HttpUrlSource implements Source {
|
||||
@Override
|
||||
public void open(int offset) throws ProxyCacheException {
|
||||
try {
|
||||
connection = openConnection(offset, "GET", -1);
|
||||
connection = openConnection(offset, -1);
|
||||
mime = connection.getContentType();
|
||||
inputStream = new BufferedInputStream(connection.getInputStream(), DEFAULT_BUFFER_SIZE);
|
||||
length = readSourceAvailableBytes(connection, offset, connection.getResponseCode());
|
||||
@@ -76,7 +76,13 @@ public class HttpUrlSource implements Source {
|
||||
@Override
|
||||
public void close() throws ProxyCacheException {
|
||||
if (connection != null) {
|
||||
connection.disconnect();
|
||||
try {
|
||||
connection.disconnect();
|
||||
} catch (NullPointerException e) {
|
||||
// https://github.com/danikula/AndroidVideoCache/issues/32
|
||||
// https://github.com/danikula/AndroidVideoCache/issues/29
|
||||
throw new ProxyCacheException("Error disconnecting HttpUrlConnection", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,7 +105,7 @@ public class HttpUrlSource implements Source {
|
||||
HttpURLConnection urlConnection = null;
|
||||
InputStream inputStream = null;
|
||||
try {
|
||||
urlConnection = openConnection(0, "HEAD", 10000);
|
||||
urlConnection = openConnection(0, 10000);
|
||||
length = urlConnection.getContentLength();
|
||||
mime = urlConnection.getContentType();
|
||||
inputStream = urlConnection.getInputStream();
|
||||
@@ -114,7 +120,7 @@ public class HttpUrlSource implements Source {
|
||||
}
|
||||
}
|
||||
|
||||
private HttpURLConnection openConnection(int offset, String method, int timeout) throws IOException, ProxyCacheException {
|
||||
private HttpURLConnection openConnection(int offset, int timeout) throws IOException, ProxyCacheException {
|
||||
HttpURLConnection connection;
|
||||
boolean redirected;
|
||||
int redirectCount = 0;
|
||||
@@ -122,7 +128,6 @@ public class HttpUrlSource implements Source {
|
||||
do {
|
||||
Log.d(LOG_TAG, "Open connection " + (offset > 0 ? " with offset " + offset : "") + " to " + url);
|
||||
connection = (HttpURLConnection) new URL(url).openConnection();
|
||||
connection.setRequestMethod(method);
|
||||
if (offset > 0) {
|
||||
connection.setRequestProperty("Range", "bytes=" + offset + "-");
|
||||
}
|
||||
|
||||
@@ -100,10 +100,11 @@ class ProxyCache {
|
||||
}
|
||||
}
|
||||
|
||||
protected void onCacheAvailable(long cacheAvailable, long sourceAvailable) {
|
||||
int percents = (int) (cacheAvailable * 100 / sourceAvailable);
|
||||
protected void onCacheAvailable(long cacheAvailable, long sourceLength) {
|
||||
boolean zeroLengthSource = sourceLength == 0;
|
||||
int percents = zeroLengthSource ? 100 : (int) (cacheAvailable * 100 / sourceLength);
|
||||
boolean percentsChanged = percents != percentsAvailable;
|
||||
boolean sourceLengthKnown = sourceAvailable >= 0;
|
||||
boolean sourceLengthKnown = sourceLength >= 0;
|
||||
if (sourceLengthKnown && percentsChanged) {
|
||||
onCachePercentsAvailableChanged(percents);
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ dependencies {
|
||||
// compile project(':library')
|
||||
compile 'com.android.support:support-v4:23.1.0'
|
||||
compile 'org.androidannotations:androidannotations-api:3.3.2'
|
||||
compile 'com.danikula:videocache:2.3.2'
|
||||
compile 'com.danikula:videocache:2.3.3'
|
||||
compile 'com.viewpagerindicator:library:2.4.2-SNAPSHOT@aar'
|
||||
apt 'org.androidannotations:androidannotations:3.3.2'
|
||||
}
|
||||
|
||||
@@ -211,9 +211,8 @@ public class HttpProxyCacheServerTest {
|
||||
}
|
||||
|
||||
private Pair<File, Response> readProxyData(String url, int offset) throws IOException {
|
||||
File externalCacheDir = RuntimeEnvironment.application.getExternalCacheDir();
|
||||
File file = file(externalCacheDir, url);
|
||||
HttpProxyCacheServer proxy = newProxy(externalCacheDir);
|
||||
File file = file(cacheFolder, url);
|
||||
HttpProxyCacheServer proxy = newProxy(cacheFolder);
|
||||
|
||||
Response response = readProxyResponse(proxy, url, offset);
|
||||
proxy.shutdown();
|
||||
|
||||
@@ -12,6 +12,7 @@ import org.robolectric.RobolectricGradleTestRunner;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.net.Socket;
|
||||
|
||||
import static com.danikula.videocache.support.ProxyCacheTestUtils.HTTP_DATA_URL;
|
||||
@@ -20,6 +21,7 @@ import static org.fest.assertions.api.Assertions.assertThat;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyInt;
|
||||
import static org.mockito.Matchers.anyLong;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
@@ -73,4 +75,24 @@ public class HttpProxyCacheTest {
|
||||
assertThat(response.data).isEqualTo(partialData);
|
||||
assertThat(response.code).isEqualTo(206);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoadEmptyFile() throws Exception {
|
||||
String zeroSizeUrl = "https://dl.dropboxusercontent.com/u/15506779/persistent/proxycache/empty.txt";
|
||||
HttpUrlSource source = new HttpUrlSource(zeroSizeUrl);
|
||||
HttpProxyCache proxyCache = new HttpProxyCache(source, new FileCache(ProxyCacheTestUtils.newCacheFile()));
|
||||
GetRequest request = new GetRequest("GET /" + HTTP_DATA_URL + " HTTP/1.1");
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
Socket socket = mock(Socket.class);
|
||||
when(socket.getOutputStream()).thenReturn(out);
|
||||
|
||||
CacheListener listener = Mockito.mock(CacheListener.class);
|
||||
proxyCache.registerCacheListener(listener);
|
||||
proxyCache.processRequest(request, socket);
|
||||
proxyCache.registerCacheListener(null);
|
||||
Response response = new Response(out.toByteArray());
|
||||
|
||||
Mockito.verify(listener).onCacheAvailable(Mockito.<File>any(), eq(zeroSizeUrl), eq(100));
|
||||
assertThat(response.data).isEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +40,11 @@ public class ProxyCacheTestUtils {
|
||||
}
|
||||
|
||||
public static Response readProxyResponse(HttpProxyCacheServer proxy, String url, int offset) throws IOException {
|
||||
URL proxiedUrl = new URL(proxy.getProxyUrl(url));
|
||||
String proxyUrl = proxy.getProxyUrl(url);
|
||||
if (!proxyUrl.startsWith("http://127.0.0.1")) {
|
||||
throw new IllegalStateException("Url " + url + " is not proxied!");
|
||||
}
|
||||
URL proxiedUrl = new URL(proxyUrl);
|
||||
HttpURLConnection connection = (HttpURLConnection) proxiedUrl.openConnection();
|
||||
try {
|
||||
if (offset >= 0) {
|
||||
|
||||
Reference in New Issue
Block a user