Commit 940d629b authored by qq247321453's avatar qq247321453

预留下载缓存

parent 875d6e44
package cn.garymb.ygomobile.utils; package cn.garymb.ygomobile.utils;
import android.util.Log;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import cn.garymb.ygomobile.core.IrrlichtBridge;
import okhttp3.Call; import okhttp3.Call;
import okhttp3.Callback; import okhttp3.Callback;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
...@@ -15,6 +20,9 @@ public class DownloadUtil { ...@@ -15,6 +20,9 @@ public class DownloadUtil {
private static DownloadUtil downloadUtil; private static DownloadUtil downloadUtil;
private final OkHttpClient okHttpClient; private final OkHttpClient okHttpClient;
//暂时关闭
private static final boolean ENABLE_CACHE = false;
private static final Map<String, Call> cache = new HashMap<>();
public static DownloadUtil get() { public static DownloadUtil get() {
if (downloadUtil == null) { if (downloadUtil == null) {
...@@ -36,7 +44,15 @@ public class DownloadUtil { ...@@ -36,7 +44,15 @@ public class DownloadUtil {
*/ */
public void download(final String url, final String destFileDir, final String destFileName, final OnDownloadListener listener) { public void download(final String url, final String destFileDir, final String destFileName, final OnDownloadListener listener) {
if(ENABLE_CACHE){
synchronized (cache){
Call old = cache.get(url);
if(old != null){
Log.w(IrrlichtBridge.TAG, "exist download task by:" +url);
return;
}
}
}
Request request = new Request.Builder() Request request = new Request.Builder()
.url(url) .url(url)
.build(); .build();
...@@ -50,16 +66,21 @@ public class DownloadUtil { ...@@ -50,16 +66,21 @@ public class DownloadUtil {
}*/ }*/
//异步请求 //异步请求
okHttpClient.newCall(request).enqueue(new Callback() { Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override @Override
public void onFailure(Call call, IOException e) { public void onFailure(Call call, IOException e) {
// 下载失败监听回调 // 下载失败监听回调
listener.onDownloadFailed(e); listener.onDownloadFailed(e);
if(ENABLE_CACHE){
synchronized (cache){
cache.remove(url);
}
}
} }
@Override @Override
public void onResponse(Call call, Response response) throws IOException { public void onResponse(Call call, Response response) throws IOException {
InputStream is = null; InputStream is = null;
byte[] buf = new byte[2048]; byte[] buf = new byte[2048];
int len = 0; int len = 0;
...@@ -88,24 +109,17 @@ public class DownloadUtil { ...@@ -88,24 +109,17 @@ public class DownloadUtil {
fos.flush(); fos.flush();
//下载完成 //下载完成
listener.onDownloadSuccess(file); listener.onDownloadSuccess(file);
} catch (Exception e) { } catch (Exception ex) {
listener.onDownloadFailed(e); listener.onDownloadFailed(ex);
}finally { }finally {
IOUtils.close(is);
try { IOUtils.close(fos);
if (is != null) { }
is.close(); if(ENABLE_CACHE){
} synchronized (cache){
if (fos != null) { cache.remove(url);
fos.close();
}
} catch (IOException e) {
} }
} }
} }
}); });
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment