@@ -555,10 +555,13 @@ public static boolean isAcceptRange(int responseCode, FileDownloadConnection con
555
555
// because of we using one of two HEAD method to request or using range:0-0 to trial connection
556
556
// only if connection api not support, so we test content-range first and then test
557
557
// content-length.
558
- public static long findInstanceLengthForTrial (int id , FileDownloadConnection connection ) {
558
+ public static long findInstanceLengthForTrial (FileDownloadConnection connection ) {
559
559
long length = findInstanceLengthFromContentRange (connection );
560
- if (length < 0 ) length = findContentLength (id , connection );
561
- if (length < 0 ) length = TOTAL_VALUE_IN_CHUNKED_RESOURCE ;
560
+ if (length < 0 ) {
561
+ length = TOTAL_VALUE_IN_CHUNKED_RESOURCE ;
562
+ FileDownloadLog .w (FileDownloadUtils .class , "don't get instance length from"
563
+ + "Content-Range header" );
564
+ }
562
565
// the response of HEAD method is not very canonical sometimes(it depends on server
563
566
// implementation)
564
567
// so that it's uncertain the content-length is the same as the response of GET method if
@@ -571,13 +574,16 @@ public static long findInstanceLengthForTrial(int id, FileDownloadConnection con
571
574
}
572
575
573
576
public static long findInstanceLengthFromContentRange (FileDownloadConnection connection ) {
574
- return parseContentRangeFoInstanceLength (
575
- connection .getResponseHeaderField ("Content-Range" ));
577
+ return parseContentRangeFoInstanceLength (getContentRangeHeader (connection ));
578
+ }
579
+
580
+ private static String getContentRangeHeader (FileDownloadConnection connection ) {
581
+ return connection .getResponseHeaderField ("Content-Range" );
576
582
}
577
583
578
584
public static long findContentLength (final int id , FileDownloadConnection connection ) {
579
- long contentLength = FileDownloadUtils
580
- . convertContentLengthString ( connection .getResponseHeaderField ("Content-Length" ));
585
+ long contentLength = convertContentLengthString (
586
+ connection .getResponseHeaderField ("Content-Length" ));
581
587
final String transferEncoding = connection .getResponseHeaderField ("Transfer-Encoding" );
582
588
583
589
if (contentLength < 0 ) {
@@ -609,6 +615,31 @@ public static long findContentLength(final int id, FileDownloadConnection connec
609
615
return contentLength ;
610
616
}
611
617
618
+ public static long findContentLengthFromContentRange (FileDownloadConnection connection ) {
619
+ final String contentRange = getContentRangeHeader (connection );
620
+ long contentLength = parseContentLengthFromContentRange (contentRange );
621
+ if (contentLength < 0 ) contentLength = TOTAL_VALUE_IN_CHUNKED_RESOURCE ;
622
+ return contentLength ;
623
+ }
624
+
625
+ public static long parseContentLengthFromContentRange (String contentRange ) {
626
+ if (contentRange == null || contentRange .length () == 0 ) return -1 ;
627
+ final String pattern = "bytes (\\ d+)-(\\ d+)/\\ d+" ;
628
+ try {
629
+ final Pattern r = Pattern .compile (pattern );
630
+ final Matcher m = r .matcher (contentRange );
631
+ if (m .find ()) {
632
+ final long rangeStart = Long .parseLong (m .group (1 ));
633
+ final long rangeEnd = Long .parseLong (m .group (2 ));
634
+ return rangeEnd - rangeStart + 1 ;
635
+ }
636
+ } catch (Exception e ) {
637
+ FileDownloadLog .e (FileDownloadUtils .class , e , "parse content length"
638
+ + " from content range error" );
639
+ }
640
+ return -1 ;
641
+ }
642
+
612
643
public static String findFilename (FileDownloadConnection connection , String url ) {
613
644
String filename = FileDownloadUtils .parseContentDisposition (connection .
614
645
getResponseHeaderField ("Content-Disposition" ));
0 commit comments