|
|
@ -76,8 +76,9 @@ final class SingleThreadTask implements Runnable { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override public void run() { |
|
|
|
@Override public void run() { |
|
|
|
HttpURLConnection conn; |
|
|
|
HttpURLConnection conn = null; |
|
|
|
InputStream is; |
|
|
|
InputStream is = null; |
|
|
|
|
|
|
|
BufferedRandomAccessFile file = null; |
|
|
|
try { |
|
|
|
try { |
|
|
|
URL url = new URL(mConfigEntity.DOWNLOAD_URL); |
|
|
|
URL url = new URL(mConfigEntity.DOWNLOAD_URL); |
|
|
|
conn = ConnectionHelp.handleConnection(url); |
|
|
|
conn = ConnectionHelp.handleConnection(url); |
|
|
@ -102,8 +103,7 @@ final class SingleThreadTask implements Runnable { |
|
|
|
conn.setReadTimeout(CONSTANCE.READ_TIME_OUT); //设置读取流的等待时间,必须设置该参数
|
|
|
|
conn.setReadTimeout(CONSTANCE.READ_TIME_OUT); //设置读取流的等待时间,必须设置该参数
|
|
|
|
is = conn.getInputStream(); |
|
|
|
is = conn.getInputStream(); |
|
|
|
//创建可设置位置的文件
|
|
|
|
//创建可设置位置的文件
|
|
|
|
BufferedRandomAccessFile file = |
|
|
|
file = new BufferedRandomAccessFile(mConfigEntity.TEMP_FILE, "rwd", mBufSize); |
|
|
|
new BufferedRandomAccessFile(mConfigEntity.TEMP_FILE, "rwd", mBufSize); |
|
|
|
|
|
|
|
//设置每条线程写入文件的位置
|
|
|
|
//设置每条线程写入文件的位置
|
|
|
|
file.seek(mConfigEntity.START_LOCATION); |
|
|
|
file.seek(mConfigEntity.START_LOCATION); |
|
|
|
byte[] buffer = new byte[mBufSize]; |
|
|
|
byte[] buffer = new byte[mBufSize]; |
|
|
@ -121,10 +121,6 @@ final class SingleThreadTask implements Runnable { |
|
|
|
file.write(buffer, 0, len); |
|
|
|
file.write(buffer, 0, len); |
|
|
|
progress(len); |
|
|
|
progress(len); |
|
|
|
} |
|
|
|
} |
|
|
|
file.close(); |
|
|
|
|
|
|
|
//close 为阻塞的,需要使用线程池来处理
|
|
|
|
|
|
|
|
is.close(); |
|
|
|
|
|
|
|
conn.disconnect(); |
|
|
|
|
|
|
|
if (CONSTANCE.isCancel) { |
|
|
|
if (CONSTANCE.isCancel) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
@ -161,6 +157,20 @@ final class SingleThreadTask implements Runnable { |
|
|
|
failDownload(mChildCurrentLocation, "下载失败【" + mConfigEntity.DOWNLOAD_URL + "】", e); |
|
|
|
failDownload(mChildCurrentLocation, "下载失败【" + mConfigEntity.DOWNLOAD_URL + "】", e); |
|
|
|
} catch (Exception e) { |
|
|
|
} catch (Exception e) { |
|
|
|
failDownload(mChildCurrentLocation, "获取流失败", e); |
|
|
|
failDownload(mChildCurrentLocation, "获取流失败", e); |
|
|
|
|
|
|
|
} finally { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
if (file != null) { |
|
|
|
|
|
|
|
file.close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (is != null) { |
|
|
|
|
|
|
|
is.close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (conn != null) { |
|
|
|
|
|
|
|
conn.disconnect(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|