Skip to content

Instantly share code, notes, and snippets.

@allenatwork
Created March 15, 2017 08:51
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save allenatwork/152e6c96877be7234f9bf49693d07c24 to your computer and use it in GitHub Desktop.
Download Task
public class DownLoadTask extends Thread {
private String mFilePath;
private String mDownLoadUrl;
private ProgressListener mProgressListener;
private OkHttpClient mClient;
public DownLoadTask(String filePath, String downLoadUrl, ProgressListener progressListener) {
mDownLoadUrl = downLoadUrl;
mFilePath = filePath;
mProgressListener = progressListener;
}
@Override
public void run() {
Request request = new Request.Builder()
.url(mDownLoadUrl)
.tag("download")
.build();
mClient = new OkHttpClient.Builder()
.addNetworkInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Response originalResponse = chain.proceed(chain.request());
return originalResponse.newBuilder()
.body(new ProgressResponseBody(originalResponse.body(), mProgressListener))
.build();
}
})
.build();
mClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
int len;
byte[] buf = new byte[2048];
InputStream inputStream = response.body().byteStream();
File file = new File(mFilePath);
if (file.exists()) {
file.delete();
}
FileOutputStream fileOutputStream = new FileOutputStream(file);
while ((len = inputStream.read(buf)) != -1) {
fileOutputStream.write(buf, 0, len);
}
fileOutputStream.flush();
fileOutputStream.close();
inputStream.close();
}
});
}
public void cancel() {
for(Call call : mClient.dispatcher().queuedCalls()) {
if(call.request().tag().equals("sss"))
call.cancel();
}
for (Call call : mClient.dispatcher().runningCalls()) {
if (call.request().tag().equals("download")){
call.cancel();
}
}
}
private static class ProgressResponseBody extends ResponseBody {
private final ResponseBody responseBody;
private final ProgressListener progressListener;
private BufferedSource bufferedSource;
public ProgressResponseBody(ResponseBody responseBody, ProgressListener progressListener) {
this.responseBody = responseBody;
this.progressListener = progressListener;
}
@Override
public MediaType contentType() {
return responseBody.contentType();
}
@Override
public long contentLength() {
return responseBody.contentLength();
}
@Override
public BufferedSource source() {
if (bufferedSource == null) {
bufferedSource = Okio.buffer(source(responseBody.source()));
}
return bufferedSource;
}
private Source source(Source source) {
return new ForwardingSource(source) {
long totalBytesRead = 0L;
@Override
public long read(Buffer sink, long byteCount) throws IOException {
long bytesRead = super.read(sink, byteCount);
// read() returns the number of bytes read, or -1 if this source is exhausted.
totalBytesRead += bytesRead != -1 ? bytesRead : 0;
progressListener.update(totalBytesRead, responseBody.contentLength(), bytesRead == -1);
return bytesRead;
}
};
}
}
public interface ProgressListener {
void update(long bytesRead, long contentLength, boolean done);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment