Skip to content

Commit 910bf1d

Browse files
author
ghc
committed
branch:master_dev
下载服务改造
1 parent aa2026e commit 910bf1d

7 files changed

Lines changed: 326 additions & 261 deletions

File tree

EvolvingNet/src/main/java/com/codingcoderscode/evolving/net/CCRxNetManager.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import com.codingcoderscode.evolving.net.request.CCDownloadRequest;
66
import com.codingcoderscode.evolving.net.request.CCGetRequest;
77
import com.codingcoderscode.evolving.net.request.CCHeadRequest;
8-
import com.codingcoderscode.evolving.net.request.CCMultiDownladRequest;
8+
import com.codingcoderscode.evolving.net.request.CCMultiDownloadRequest;
99
import com.codingcoderscode.evolving.net.request.CCOptionsRequest;
1010
import com.codingcoderscode.evolving.net.request.CCPostRequest;
1111
import com.codingcoderscode.evolving.net.request.CCPutRequest;
@@ -367,8 +367,8 @@ public <T> CCDownloadRequest<T> download(String url) {
367367
* @param <T> 传值Void
368368
* @return
369369
*/
370-
public <T> CCMultiDownladRequest<T> multiDownload(String url) {
371-
return new CCMultiDownladRequest<T>(url, this.ccNetApiService);
370+
public <T> CCMultiDownloadRequest<T> multiDownload(String url) {
371+
return new CCMultiDownloadRequest<T>(url, this.ccNetApiService);
372372
}
373373

374374
/**

EvolvingNet/src/main/java/com/codingcoderscode/evolving/net/request/CCDownloadRequest.java

Lines changed: 60 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ public class CCDownloadRequest<T> extends CCRequest<T, CCDownloadRequest<T>> {
6767
//是否支持断点下载
6868
private boolean supportRage;
6969

70-
7170
private InputStream inputStream = null;
7271
private OutputStream outputStream = null;
7372

@@ -93,6 +92,21 @@ public CCDownloadRequest(String url, CCNetApiService apiService) {
9392
this.supportRage = false;
9493
}
9594

95+
@Override
96+
protected int getHttpMethod() {
97+
return CCHttpMethod.GET;
98+
}
99+
100+
@Override
101+
public int getCacheQueryMode() {
102+
return CCCMode.QueryMode.MODE_NET;
103+
}
104+
105+
@Override
106+
public int getCacheSaveMode() {
107+
return CCCMode.SaveMode.MODE_NONE;
108+
}
109+
96110
@Override
97111
protected Flowable<CCBaseResponse<T>> getRequestFlowable() {
98112

@@ -109,61 +123,30 @@ public void subscribe(FlowableEmitter<Call<ResponseBody>> e) throws Exception {
109123
e.onComplete();
110124
}
111125
}, BackpressureStrategy.LATEST)
112-
.subscribeOn(Schedulers.io())
113-
.unsubscribeOn(Schedulers.io())
114-
.observeOn(Schedulers.io())
126+
//.subscribeOn(Schedulers.io())
127+
//.unsubscribeOn(Schedulers.io())
128+
//.observeOn(Schedulers.io())
115129
.flatMap(new Function<Call<ResponseBody>, Publisher<CCBaseResponse<T>>>() {
116130
@Override
117131
public Publisher<CCBaseResponse<T>> apply(Call<ResponseBody> responseBodyCall) throws Exception {
118132

119133
Response<ResponseBody> retrofitResponse;
120134
Headers headers = null;
121135
try {
122-
123136
retrofitResponse = responseBodyCall.clone().execute();
124137

125138
headers = retrofitResponse.headers();
126-
127-
onFileSaveCheck(CCNetUtil.isHttpSupportRange(headers) && isSupportRage());
128-
129-
String contentLengthStr = CCNetUtil.getHeader("Content-Length", headers);
130-
131-
if (TextUtils.isEmpty(contentLengthStr)) {
132-
throw new NoResponseBodyDataException("no file data");
133-
} else {
134-
long contentLengthLong = convertStringToLong(contentLengthStr);
135-
136-
if (contentLengthLong > SDCardUtil.getSDCardAvailableSize() * 1024 * 1024) {
137-
throw new NoEnoughSpaceException("write failed: ENOSPC (No space left on device)");
138-
}
139-
140-
if (contentLengthLong == 0) {
141-
throw new NoResponseBodyDataException("no file data");
142-
}
143-
}
144-
145-
if (getCCDownloadFileWriteListener() != null) {
146-
getCCDownloadFileWriteListener().onWriteToDisk(retrofitResponse.body(), headers, getCCNetResultListener());
147-
} else {
148-
onWriteToDisk(retrofitResponse.body());
149-
}
150-
151-
} catch (NoResponseBodyDataException nrbde) {
152-
throw nrbde;
153139
} catch (Exception exception) {
154140
throw new CCUnExpectedException(exception);
155141
}
156142

143+
onTryToWriteFileToSDCard(retrofitResponse, headers);
144+
157145
return Flowable.just(new CCBaseResponse<T>(null, headers, false, false, true, null));
158146
}
159147
}).retryWhen(new FlowableRetryWithDelay(getRetryCount(), getRetryDelayTimeMillis())).onBackpressureLatest();
160148
}
161149

162-
@Override
163-
protected int getHttpMethod() {
164-
return CCHttpMethod.GET;
165-
}
166-
167150
@Override
168151
protected Call<ResponseBody> getRequestCall() {
169152
if (isSupportRage()) {
@@ -190,16 +173,6 @@ protected Call<ResponseBody> getRequestCall() {
190173
return call;
191174
}
192175

193-
@Override
194-
public int getCacheQueryMode() {
195-
return CCCMode.QueryMode.MODE_NET;
196-
}
197-
198-
@Override
199-
public int getCacheSaveMode() {
200-
return CCCMode.SaveMode.MODE_NONE;
201-
}
202-
203176
private long convertStringToLong(String source) {
204177
long result = 0;
205178
try {
@@ -210,6 +183,46 @@ private long convertStringToLong(String source) {
210183
return result;
211184
}
212185

186+
/**
187+
* 将文件内容写入磁盘
188+
*
189+
* @param retrofitResponse
190+
* @param headers
191+
* @throws Exception
192+
*/
193+
protected void onTryToWriteFileToSDCard(Response<ResponseBody> retrofitResponse, Headers headers) throws Exception {
194+
try {
195+
onFileSaveCheck(CCNetUtil.isHttpSupportRange(headers) && isSupportRage());
196+
197+
String contentLengthStr = CCNetUtil.getHeader("Content-Length", headers);
198+
199+
if (TextUtils.isEmpty(contentLengthStr)) {
200+
throw new NoResponseBodyDataException("no file data");
201+
} else {
202+
long contentLengthLong = convertStringToLong(contentLengthStr);
203+
204+
if (contentLengthLong > SDCardUtil.getSDCardAvailableSize() * 1024 * 1024) {
205+
throw new NoEnoughSpaceException("write failed: ENOSPC (No space left on device)");
206+
}
207+
208+
if (contentLengthLong == 0) {
209+
throw new NoResponseBodyDataException("no file data");
210+
}
211+
}
212+
213+
if (getCCDownloadFileWriteListener() != null) {
214+
getCCDownloadFileWriteListener().onWriteToDisk(retrofitResponse.body(), headers, getCCNetResultListener());
215+
} else {
216+
onWriteToDisk(retrofitResponse.body());
217+
}
218+
219+
} catch (NoResponseBodyDataException nrbde) {
220+
throw nrbde;
221+
} catch (Exception exception) {
222+
throw new CCUnExpectedException(exception);
223+
}
224+
}
225+
213226
/**
214227
* 将下载内容写到磁盘指定位置
215228
*

0 commit comments

Comments
 (0)