Leopard icon indicating copy to clipboard operation
Leopard copied to clipboard

大文件下载

Open Synaric opened this issue 8 years ago • 0 comments

小文件下载时没有问题,但是大文件下载时,进度会卡主不动,过一会报错:

06-29 14:22:41.983 11015-11937/cn.yuan.leopard W/System.err: java.net.ProtocolException: unexpected end of stream 06-29 14:22:41.983 11015-11937/cn.yuan.leopard W/System.err: at okhttp3.internal.http.Http1xStream$FixedLengthSource.read(Http1xStream.java:384) 06-29 14:22:41.983 11015-11937/cn.yuan.leopard W/System.err: at okio.RealBufferedSource.read(RealBufferedSource.java:50) 06-29 14:22:41.983 11015-11937/cn.yuan.leopard W/System.err: at okio.ForwardingSource.read(ForwardingSource.java:35) 06-29 14:22:41.983 11015-11937/cn.yuan.leopard W/System.err: at com.yuan.leopardkit.download.model.DownLoadResponseBody$1.read(DownLoadResponseBody.java:74) 06-29 14:22:41.983 11015-11937/cn.yuan.leopard W/System.err: at okio.RealBufferedSource$1.read(RealBufferedSource.java:371) 06-29 14:22:41.983 11015-11937/cn.yuan.leopard W/System.err: at java.io.InputStream.read(InputStream.java:162) 06-29 14:22:41.983 11015-11937/cn.yuan.leopard W/System.err: at com.yuan.leopardkit.download.task.DownLoadTask.writeCache(DownLoadTask.java:137) 06-29 14:22:41.983 11015-11937/cn.yuan.leopard W/System.err: at com.yuan.leopardkit.download.DownLoadManager.writeCache(DownLoadManager.java:74) 06-29 14:22:41.983 11015-11937/cn.yuan.leopard W/System.err: at com.yuan.leopardkit.download.task.DownLoadHelper$4$1.onResponse(DownLoadHelper.java:164) 06-29 14:22:41.983 11015-11937/cn.yuan.leopard W/System.err: at okhttp3.RealCall$AsyncCall.execute(RealCall.java:133) 06-29 14:22:41.983 11015-11937/cn.yuan.leopard W/System.err: at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) 06-29 14:22:41.983 11015-11937/cn.yuan.leopard W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 06-29 14:22:41.983 11015-11937/cn.yuan.leopard W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 06-29 14:22:41.983 11015-11937/cn.yuan.leopard W/System.err: at java.lang.Thread.run(Thread.java:841)

推测是因为这段代码长时间阻塞引起的:

randomAccessFile = new RandomAccessFile(file, "rwd");
channelOut = randomAccessFile.getChannel();
MappedByteBuffer mappedBuffer = channelOut.map(FileChannel.MapMode.READ_WRITE, downloadInfo.getBreakProgress(), downloadInfo.getFileLength()-downloadInfo.getBreakProgress());`

找一个200MB大小的文件试一试就知道了,MappedByteBuffer会首先创建一个200MB的文件,这个过程会引起阻塞

Synaric avatar Jun 29 '17 06:06 Synaric