Source code analysis, if need to be reproduced, please specify the author: Yuloran (t.cn/EGU6c76)

preface

Wheel Maker: Season_zlc

This article summarizes the main functions and corresponding technical points of RxDownload2

The main function

  1. Use Retrofit+OKHTTP for network requests
  2. Based on RxJava build, support RxJava various operator chain call
  3. Breakpoint continuation: Automatically determines whether breakpoint continuation is supported according to the response value of the server
  4. If breakpoint continuation is not supported, traditional download is performed
  5. Multithreaded download, you can set the maximum thread, default is 3
  6. Automatically attempts to reconnect when a network connection failure is detected, and the maximum number of retries can be configured. The default value is 3
  7. The server file can be determined based on the last-Modified field
  8. In the process of authentication with the server, the lighter HEAD request method is used to obtain only the response header, reducing the burden on the server

Technology is briefly

  1. Android & Java platform network request framework, thief easy to use
  2. Based on the event sequence programming framework, thief easy to use
  3. The Http HEAD method, which carries the “Range” request header with the value “bytes=0-“, and determines whether the response header contains “content-range”
    • rfc2616 – Range
    • rfc2616 – Content-Range
    • Note that the offset of the Range value starts at 0, such as 0-499, and returns 500 bytes
  4. If the download is interrupted, you can only download again
  5. When the server supports breakpoint continuation, several threads can be opened simultaneously to download a file. The maximum number of tasks that can be downloaded simultaneously is controlled by the semaphore
  6. The retry operator of RxJava
  7. Http protocol “last-Modified” response headers and “if-Modified-since” request headers
    • rf2616 – Last-Modified
    • rf2616 – If-Modified-Since
  8. The HEAD method of the Http protocol checks whether the server file is modified, whether the server supports breakpoint download, and save file length are all HEAD requests (very fast responses). In practice, it depends on whether your server supports the HEAD request. Our server does not support the HEAD request. (Locating and solving…)
    • rfc2616 – HEAD

advantages

  • Source variable naming, indent style unified
  • English annotations and annotation habits are native to Android
  • Class has a single responsibility, great!
  • RxJava uses very 6!
  • Source code is very concise, easy to read
  • The author is obviously a big shot, but very modest! It was all Kotlin last year!

disadvantages

  • Download status encapsulation is not good, with the upstream binding, can not directly determine the received status of which object

The attached

RxDownload2 series:

  • RxDownload2 source code analysis (a)
  • RxDownload2 source code analysis (2)
  • RxDownload2 source code analysis (3)
  • RxDownload2 File Download Slow Analysis