|
@@ -3,6 +3,7 @@ package com.benyanyi.okhttp.download;
|
|
|
import android.content.Context;
|
|
|
|
|
|
import com.benyanyi.okhttp.listener.OnDownLoadObserver;
|
|
|
+import com.benyanyi.okhttp.util.OkHttpLog;
|
|
|
|
|
|
import java.io.File;
|
|
|
import java.io.FileOutputStream;
|
|
@@ -45,6 +46,7 @@ class DownloadManager {
|
|
|
private Context mContext;
|
|
|
|
|
|
private String suffix = "";//文件名后缀
|
|
|
+ private boolean isCover = false;//是否覆盖之前的文件
|
|
|
|
|
|
/**
|
|
|
* 获得一个单例类
|
|
@@ -95,10 +97,12 @@ class DownloadManager {
|
|
|
/**
|
|
|
* 开始下载
|
|
|
*
|
|
|
- * @param suffix 文件名后缀
|
|
|
- * @param url 下载请求的网址
|
|
|
+ * @param url 下载请求的网址
|
|
|
+ * @param isCover 是否覆盖之前的文件
|
|
|
+ * @param suffix 文件名后缀
|
|
|
*/
|
|
|
- void download(String url, String suffix) {
|
|
|
+ void download(String url, boolean isCover, String suffix) {
|
|
|
+ this.isCover = isCover;
|
|
|
this.suffix = suffix;
|
|
|
this.download(url);
|
|
|
}
|
|
@@ -106,11 +110,13 @@ class DownloadManager {
|
|
|
/**
|
|
|
* 开始下载
|
|
|
*
|
|
|
- * @param suffix 文件名后缀
|
|
|
* @param url 下载请求的网址
|
|
|
+ * @param isCover 是否覆盖之前的文件
|
|
|
+ * @param suffix 文件名后缀
|
|
|
* @param onDownLoadObserver 用来回调的接口
|
|
|
*/
|
|
|
- void download(String url, String suffix, OnDownLoadObserver onDownLoadObserver) {
|
|
|
+ void download(String url, boolean isCover, String suffix, OnDownLoadObserver onDownLoadObserver) {
|
|
|
+ this.isCover = isCover;
|
|
|
this.suffix = suffix;
|
|
|
this.download(url, onDownLoadObserver);
|
|
|
}
|
|
@@ -120,7 +126,7 @@ class DownloadManager {
|
|
|
*
|
|
|
* @param url 下载请求的网址
|
|
|
*/
|
|
|
- void download(String url) {
|
|
|
+ private void download(String url) {
|
|
|
DownloadObserver observer = new DownloadObserver();
|
|
|
observer.setContext(mContext);
|
|
|
Observable.just(url)
|
|
@@ -159,7 +165,7 @@ class DownloadManager {
|
|
|
.subscribe(observer);
|
|
|
}
|
|
|
|
|
|
- void download(String url, OnDownLoadObserver onDownLoadObserver) {
|
|
|
+ private void download(String url, OnDownLoadObserver onDownLoadObserver) {
|
|
|
Observable.just(url)
|
|
|
//call的map已经有了,就证明正在下载,则这次不下载
|
|
|
.filter(new Predicate<String>() {
|
|
@@ -261,24 +267,30 @@ class DownloadManager {
|
|
|
long downloadLength = 0, contentLength = downloadInfo.getTotal();
|
|
|
File file = new File(savePath, fileName);
|
|
|
if (file.exists()) {
|
|
|
- //找到了文件,代表已经下载过,则获取其长度
|
|
|
- downloadLength = file.length();
|
|
|
- }
|
|
|
- //之前下载过,需要重新来一个文件
|
|
|
- int i = 1;
|
|
|
- while (downloadLength >= contentLength) {
|
|
|
- int dotIndex = fileName.lastIndexOf(".");
|
|
|
- String fileNameOther;
|
|
|
- if (dotIndex == -1) {
|
|
|
- fileNameOther = fileName + "(" + i + ")";
|
|
|
+ if (isCover) {
|
|
|
+ file.delete();
|
|
|
} else {
|
|
|
- fileNameOther = fileName.substring(0, dotIndex)
|
|
|
- + "(" + i + ")" + fileName.substring(dotIndex);
|
|
|
+ //找到了文件,代表已经下载过,则获取其长度
|
|
|
+ downloadLength = file.length();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!isCover) {
|
|
|
+ //之前下载过,需要重新来一个文件
|
|
|
+ int i = 1;
|
|
|
+ while (downloadLength >= contentLength) {
|
|
|
+ int dotIndex = fileName.lastIndexOf(".");
|
|
|
+ String fileNameOther;
|
|
|
+ if (dotIndex == -1) {
|
|
|
+ fileNameOther = fileName + "(" + i + ")";
|
|
|
+ } else {
|
|
|
+ fileNameOther = fileName.substring(0, dotIndex)
|
|
|
+ + "(" + i + ")" + fileName.substring(dotIndex);
|
|
|
+ }
|
|
|
+ File newFile = new File(savePath, fileNameOther);
|
|
|
+ file = newFile;
|
|
|
+ downloadLength = newFile.length();
|
|
|
+ i++;
|
|
|
}
|
|
|
- File newFile = new File(savePath, fileNameOther);
|
|
|
- file = newFile;
|
|
|
- downloadLength = newFile.length();
|
|
|
- i++;
|
|
|
}
|
|
|
//设置改变过的文件名/大小
|
|
|
downloadInfo.setProgress(downloadLength);
|
|
@@ -295,7 +307,7 @@ class DownloadManager {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void subscribe(ObservableEmitter<DownloadInfo> e) throws Exception {
|
|
|
+ public void subscribe(ObservableEmitter<DownloadInfo> e) {
|
|
|
String url = downloadInfo.getUrl();
|
|
|
//已经下载好的长度
|
|
|
long downloadLength = downloadInfo.getProgress();
|
|
@@ -313,32 +325,54 @@ class DownloadManager {
|
|
|
//把这个添加到call里,方便取消
|
|
|
downCalls.put(url, call);
|
|
|
downInfos.put(url, downloadInfo);
|
|
|
- Response response = call.execute();
|
|
|
- String savePath = FileUtil.isExistDir(mContext.getPackageName());
|
|
|
- File file = new File(savePath, downloadInfo.getFileName());
|
|
|
- InputStream is = null;
|
|
|
- FileOutputStream fileOutputStream = null;
|
|
|
+ Response response;
|
|
|
try {
|
|
|
- is = response.body().byteStream();
|
|
|
- fileOutputStream = new FileOutputStream(file, true);
|
|
|
- //缓冲数组2kB
|
|
|
- byte[] buffer = new byte[2048];
|
|
|
- int len;
|
|
|
- while ((len = is.read(buffer)) != -1) {
|
|
|
- fileOutputStream.write(buffer, 0, len);
|
|
|
- downloadLength += len;
|
|
|
- downloadInfo.setProgress(downloadLength);
|
|
|
- downInfos.put(url, downloadInfo);
|
|
|
- e.onNext(downloadInfo);
|
|
|
+ response = call.execute();
|
|
|
+ String savePath = FileUtil.isExistDir(mContext.getPackageName());
|
|
|
+ File file = new File(savePath, downloadInfo.getFileName());
|
|
|
+ InputStream is = null;
|
|
|
+ FileOutputStream fileOutputStream = null;
|
|
|
+ try {
|
|
|
+ if (response.body() == null) {
|
|
|
+ e.onError(new Throwable("下载文件为空"));
|
|
|
+ } else {
|
|
|
+ is = response.body().byteStream();
|
|
|
+ fileOutputStream = new FileOutputStream(file, true);
|
|
|
+ //缓冲数组2kB
|
|
|
+ byte[] buffer = new byte[2048];
|
|
|
+ int len;
|
|
|
+ while ((len = is.read(buffer)) != -1) {
|
|
|
+ fileOutputStream.write(buffer, 0, len);
|
|
|
+ downloadLength += len;
|
|
|
+ downloadInfo.setDownloadStatus(DownloadInfo.DOWNLOAD_ING);
|
|
|
+ downloadInfo.setProgress(downloadLength);
|
|
|
+ downInfos.put(url, downloadInfo);
|
|
|
+ e.onNext(downloadInfo);
|
|
|
+ }
|
|
|
+ downloadInfo.setFile(file);
|
|
|
+ fileOutputStream.flush();
|
|
|
+ downCalls.remove(url);
|
|
|
+ downloadInfo.setDownloadStatus(DownloadInfo.DOWNLOAD_OVER);
|
|
|
+ //关闭IO流
|
|
|
+ IoUtil.closeAll(is, fileOutputStream);
|
|
|
+ e.onComplete();//完成
|
|
|
+ }
|
|
|
+ } catch (Exception ex) {
|
|
|
+ if (ex.getMessage() != null && ex.getMessage().equals("Software caused connection abort")) {
|
|
|
+ OkHttpLog.e("网络断开");
|
|
|
+ }
|
|
|
+ pause(url);
|
|
|
+ downloadInfo.setDownloadStatus(DownloadInfo.DOWNLOAD_ERROR);
|
|
|
+ e.onError(ex);
|
|
|
+// } finally {
|
|
|
+ //关闭IO流
|
|
|
+// IoUtil.closeAll(is, fileOutputStream);
|
|
|
}
|
|
|
- downloadInfo.setFile(file);
|
|
|
- fileOutputStream.flush();
|
|
|
- downCalls.remove(url);
|
|
|
- } finally {
|
|
|
- //关闭IO流
|
|
|
- IoUtil.closeAll(is, fileOutputStream);
|
|
|
+ } catch (IOException ex) {
|
|
|
+ pause(url);
|
|
|
+ downloadInfo.setDownloadStatus(DownloadInfo.DOWNLOAD_ERROR);
|
|
|
+ e.onError(ex);
|
|
|
}
|
|
|
- e.onComplete();//完成
|
|
|
}
|
|
|
}
|
|
|
|