Skip to content

Commit

Permalink
fix: correct two methods
Browse files Browse the repository at this point in the history
correct the method of getting total length for trail connection and correct the method of getting content length for real download connection
  • Loading branch information
Tianhua Ran committed Mar 11, 2018
1 parent d8f6cf1 commit 1442f8c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ public void run() throws IOException, IllegalAccessException, IllegalArgumentExc

if (paused) return;

final long contentLength = FileDownloadUtils.findContentLength(connectionIndex, connection);
long contentLength = FileDownloadUtils.findContentLength(connectionIndex, connection);
if (contentLength == TOTAL_VALUE_IN_CHUNKED_RESOURCE) {
contentLength = FileDownloadUtils.findContentLengthFromContentRange(connection);
}
if (contentLength == 0) {
throw new FileDownloadGiveUpRetryException(FileDownloadUtils.
formatString(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -557,8 +557,11 @@ public static boolean isAcceptRange(int responseCode, FileDownloadConnection con
// content-length.
public static long findInstanceLengthForTrial(FileDownloadConnection connection) {
long length = findInstanceLengthFromContentRange(connection);
if (length < 0) length = findInstanceLengthFromContentLength(connection);
if (length < 0) length = TOTAL_VALUE_IN_CHUNKED_RESOURCE;
if (length < 0) {
length = TOTAL_VALUE_IN_CHUNKED_RESOURCE;
FileDownloadLog.w(FileDownloadUtils.class, "don't get instance length from"
+ "Content-Range header");
}
// the response of HEAD method is not very canonical sometimes(it depends on server
// implementation)
// so that it's uncertain the content-length is the same as the response of GET method if
Expand All @@ -575,20 +578,12 @@ public static long findInstanceLengthFromContentRange(FileDownloadConnection con
}

private static String getContentRangeHeader(FileDownloadConnection connection) {
return connection.getResponseHeaderField("Content-Range");
}

public static long findInstanceLengthFromContentLength(FileDownloadConnection connection) {
return convertContentLengthString(connection.getResponseHeaderField("Content-Length"));
return connection.getResponseHeaderField("Content-Range");
}

public static long findContentLength(final int id, FileDownloadConnection connection) {
long contentLength = findInstanceLengthFromContentLength(connection);
if (contentLength < 0) {
final String contentRange = getContentRangeHeader(connection);
contentLength = parseContentLengthFromContentRange(contentRange);
}

long contentLength = convertContentLengthString(
connection.getResponseHeaderField("Content-Length"));
final String transferEncoding = connection.getResponseHeaderField("Transfer-Encoding");

if (contentLength < 0) {
Expand Down Expand Up @@ -620,6 +615,13 @@ public static long findContentLength(final int id, FileDownloadConnection connec
return contentLength;
}

public static long findContentLengthFromContentRange(FileDownloadConnection connection) {
final String contentRange = getContentRangeHeader(connection);
long contentLength = parseContentLengthFromContentRange(contentRange);
if (contentLength < 0) contentLength = TOTAL_VALUE_IN_CHUNKED_RESOURCE;
return contentLength;
}

public static long parseContentLengthFromContentRange(String contentRange) {
if (contentRange == null || contentRange.length() == 0) return -1;
final String pattern = "bytes (\\d+)-(\\d+)/\\d+";
Expand All @@ -631,9 +633,9 @@ public static long parseContentLengthFromContentRange(String contentRange) {
final long rangeEnd = Long.parseLong(m.group(2));
return rangeEnd - rangeStart + 1;
}
}catch (Exception e) {
FileDownloadLog.e(FileDownloadUtils.class, e, "parse content length" +
" from content range error");
} catch (Exception e) {
FileDownloadLog.e(FileDownloadUtils.class, e, "parse content length"
+ " from content range error");
}
return -1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public void parseContentLengthFromContentRange_withEmptyContentRange() {

@Test
public void parseContentLengthFromContentRange_withStartToEndRange() {
long length = FileDownloadUtils.parseContentLengthFromContentRange("bytes 25086300-37629450/37629451");
long length = FileDownloadUtils
.parseContentLengthFromContentRange("bytes 25086300-37629450/37629451");
assertThat(length).isEqualTo(12543151);
}

Expand Down

0 comments on commit 1442f8c

Please sign in to comment.