Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ public final class ANConstants {
public static final String USER_AGENT = "User-Agent";
public static final String SUCCESS = "success";
public static final String OPTIONS = "OPTIONS";
public static final String RANGE = "Range";
public static final String ACCEPT_ENCODING = "Accept-Encoding";
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ public class ANRequest<T extends ANRequest> {
private Executor mExecutor = null;
private OkHttpClient mOkHttpClient = null;
private String mUserAgent = null;
private boolean mResumeAllowed = false;
private Type mType = null;

public ANRequest(GetRequestBuilder builder) {
Expand Down Expand Up @@ -200,6 +201,7 @@ public ANRequest(DownloadBuilder builder) {
this.mExecutor = builder.mExecutor;
this.mOkHttpClient = builder.mOkHttpClient;
this.mUserAgent = builder.mUserAgent;
this.mResumeAllowed = builder.mResumeAllowed;
}

public ANRequest(MultiPartBuilder builder) {
Expand Down Expand Up @@ -465,6 +467,10 @@ public String getUserAgent() {
return mUserAgent;
}

public boolean getResumeAllowed() {
return mResumeAllowed;
}

public Type getType() {
return mType;
}
Expand Down Expand Up @@ -1443,6 +1449,7 @@ public static class DownloadBuilder<T extends DownloadBuilder> implements Reques
private Executor mExecutor;
private OkHttpClient mOkHttpClient;
private String mUserAgent;
private boolean mResumeAllowed;

public DownloadBuilder(String url, String dirPath, String fileName) {
this.mUrl = url;
Expand Down Expand Up @@ -1600,6 +1607,11 @@ public T setUserAgent(String userAgent) {
return (T) this;
}

public T allowResume(boolean resumeAllowed) {
mResumeAllowed = resumeAllowed;
return (T) this;
}

public T setPercentageThresholdForCancelling(int percentageThresholdForCancelling) {
mPercentageThresholdForCancelling = percentageThresholdForCancelling;
return (T) this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,11 @@ public Response intercept(Chain chain) throws IOException {
}
} catch (IOException ioe) {
try {
File destinationFile = new File(request.getDirPath() + File.separator + request.getFileName());
if (destinationFile.exists()) {
destinationFile.delete();
if (!request.getResumeAllowed()) {
File destinationFile = new File(request.getDirPath() + File.separator + request.getFileName());
if (destinationFile.exists()) {
destinationFile.delete();
}
}
} catch (Exception e) {
e.printStackTrace();
Expand Down Expand Up @@ -276,12 +278,28 @@ public static void addHeadersToRequestBuilder(Request.Builder builder, ANRequest
request.setUserAgent(sUserAgent);
builder.addHeader(ANConstants.USER_AGENT, sUserAgent);
}
if (request.getResumeAllowed()) {
File destinationFile = new File(request.getDirPath() + File.separator + request.getFileName());
if (destinationFile.exists()) {
long fileLength = destinationFile.length();
builder.addHeader(ANConstants.ACCEPT_ENCODING, "identity");
builder.addHeader(ANConstants.RANGE, "bytes=" + fileLength + "-");
}
}
Headers requestHeaders = request.getHeaders();
if (requestHeaders != null) {
builder.headers(requestHeaders);
if (request.getUserAgent() != null && !requestHeaders.names().contains(ANConstants.USER_AGENT)) {
builder.addHeader(ANConstants.USER_AGENT, request.getUserAgent());
}
if (request.getResumeAllowed() && !requestHeaders.names().contains(ANConstants.RANGE)) {
File destinationFile = new File(request.getDirPath() + File.separator + request.getFileName());
if (destinationFile.exists()) {
long fileLength = destinationFile.length();
builder.addHeader(ANConstants.ACCEPT_ENCODING, "identity");
builder.addHeader(ANConstants.RANGE, "bytes=" + fileLength + "-");
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public static int findBestSampleSize(int actualWidth, int actualHeight,
public static void saveFile(Response response, String dirPath,
String fileName) throws IOException {
InputStream is = null;
byte[] buf = new byte[2048];
byte[] buf = new byte[8192];
int len;
FileOutputStream fos = null;
try {
Expand All @@ -182,11 +182,15 @@ public static void saveFile(Response response, String dirPath,
dir.mkdirs();
}
File file = new File(dir, fileName);
fos = new FileOutputStream(file);
int code = response.code();
if ((code == 206 || code == 416) && file.exists()) {
fos = new FileOutputStream(file, true);
} else {
fos = new FileOutputStream(file);
}
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
}
fos.flush();
} finally {
try {
if (is != null) is.close();
Expand Down