使用okhttp

时间:2016-11-10 06:39:25

标签: android okhttp

为什么当存在异常时,RealBufferedSource(FixedLengthSource)的BufferSize从65536变为2048字节?

我在下载管理员代码上遇到的2个问题如下:(我是这个论坛的新手。如果需要更多输入,请告诉我)

问题1:

android的DownloadManager(a.k.a DownloadThread)更新一次,读取65536字节4次。这个实现在哪里完成?对不起,我找不到它完成的地方。这次更新的任何特定原因是DownloadThread一次4次读取?

在错误情况下,如果我从DownloadManager执行部分HTTP提取请求,则Range:请求将使用上次更新的currentBytes值。因此,如果中间发生错误(例如,第4次读取中的第2次),则Range:请求将使用先前更新的currentBytes,这可能会再次重新读取相同的字节。

// HC:是HttpConnection.java,FixedLengthSource#读取添加打印的位置。

19:09:10.374  System.out: (HTTPLog)-Thread-270-223795676: Content-Length : 70610220
19:09:10.684  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 70610220,65536
19:09:10.694  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 70593836,65536
19:09:10.704  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 70577452,65536
19:09:10.704  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 70561068,65536
19:09:10.714  DownloadManager: currentBytes : 65536,mLastUpdateBytes : 0

19:09:10.714  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 70544684,65536
19:09:10.714  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 70528300,65536
19:09:10.724  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 70511916,65536
19:09:10.754  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 70495532,65536
19:09:10.774  DownloadManager: currentBytes : 131072,mLastUpdateBytes : 131072

19:09:10.774  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 70479148,65536
19:09:10.784  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 70462764,65536
19:09:10.784  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 70446380,65536
19:09:10.784  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 70429996,65536
19:09:10.794  DownloadManager: currentBytes : 196608,mLastUpdateBytes : 131072

19:09:10.794  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 70413612,65536
19:09:10.814  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 70397228,65536
19:09:10.814  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 70380844,65536
19:09:10.824  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 70364460,65536
19:09:10.834  DownloadManager: currentBytes : 262144,mLastUpdateBytes : 131072

示例:

19:09:10.794  DownloadManager: currentBytes : 196608,mLastUpdateBytes : 131072
// 1st read
19:09:10.794  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 70413612,65536 
// 2nd read
19:09:10.814  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 70397228,65536 
// If error happens here, the range: request for partial fetch happens using the 
// currentBytes variable as follows
System.out: (HTTPLog)-Thread-270-51176450: Range : bytes=196608-

// Ideally, it should be { [currentBytes : 196608] + [1st Read : 65536] + [2nd Read : 65536]}
System.out: (HTTPLog)-Thread-270-51176450: Range : bytes=327680- 

问题2:

当发生类似SSLException的IOException时,自动将获取大小从65536减少到2048.这个实现完成了什么?对不起,我找不到它完成的地方。 降级的任何具体原因 - 避免拥堵都是这样的?

19:10:11.244  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 13905628,65536 19:10:11.244  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 13889244,65536 19:10:11.254  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 13872860,65536 19:10:11.254  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 13856476,65536 19:10:11.254  DownloadManager: currentBytes : 56754176, mLastUpdateBytes : 56360960 19:10:11.254  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 13840092,65536 19:10:11.264  System.out: (HTTPLog)-Static: FixedLengthSource#read:: SSLException caught... 19:10:11.264  System.out: (HTTPLog)-Static: FixedLengthSource#read, Bytes remaining to be  downloaded : 13840092 19:10:11.264  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 13840092,2048 19:10:11.284  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 13838044,2048 19:10:11.284  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 13835996,2048 19:10:11.284  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 13833948,2048 19:10:11.294  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 13831900,2048 19:10:11.294  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 13829852,2048 19:10:11.294  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 13827804,2048 19:10:11.294  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 13825756,2048 19:10:11.304  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 13823708,2048 19:10:11.304  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 13821660,2048 19:10:11.304  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 13819612,2048 19:10:11.314  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 13817564,2048 19:10:11.314  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 13815516,2048 19:10:11.314  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 13813468,2048 19:10:11.314  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 13811420,2048 19:10:11.314  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 13809372,2048 19:10:11.314  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 13807324,2048 19:10:11.324  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 13805276,2048 19:10:11.324  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 13803228,2048 19:10:11.324  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 13801180,2048 19:10:11.324  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 13799132,2048 19:10:11.324  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 13797084,2048 19:10:11.324  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 13795036,2048 19:10:11.334  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 13792988,2048 19:10:11.334  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 13790940,2048 19:10:11.334  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 13788892,2048 19:10:11.334  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 13786844,2048 19:10:11.334  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 13784796,2048 19:10:11.334  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 13782748,2048 19:10:11.344  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 13780700,2048 19:10:11.344  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 13778652,2048 19:10:11.354  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 13776604,2048 19:10:11.364  System.out: (HTTPLog)-Static:  HC:: bytesRemaining, byteCount ::: 13774556,2048

在这种情况下,也不会更新有关此2048XM字节下载的currentThread的currentBytes。

因此,如果DownloadThread使用可用的currentBytes进行部分提取,它最终将从服务器重新读取相同的字节,从而导致更多的数据使用。

请帮助我理解我是否遗漏任何基本内容。

谢谢 拉姆

0 个答案:

没有答案