|
|
@ -28,9 +28,7 @@ import java.math.BigDecimal; |
|
|
|
import java.net.HttpURLConnection; |
|
|
|
import java.net.HttpURLConnection; |
|
|
|
import java.net.MalformedURLException; |
|
|
|
import java.net.MalformedURLException; |
|
|
|
import java.net.URL; |
|
|
|
import java.net.URL; |
|
|
|
import java.util.HashSet; |
|
|
|
|
|
|
|
import java.util.Properties; |
|
|
|
import java.util.Properties; |
|
|
|
import java.util.Set; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Created by lyy on 2017/1/18. |
|
|
|
* Created by lyy on 2017/1/18. |
|
|
@ -38,7 +36,7 @@ import java.util.Set; |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
final class SingleThreadTask implements Runnable { |
|
|
|
final class SingleThreadTask implements Runnable { |
|
|
|
private static final String TAG = "SingleThreadTask"; |
|
|
|
private static final String TAG = "SingleThreadTask"; |
|
|
|
private DownloadUtil.ConfigEntity mConfigEntity; |
|
|
|
private DownloadUtil.ChildThreadConfigEntity mConfigEntity; |
|
|
|
private String mConfigFPath; |
|
|
|
private String mConfigFPath; |
|
|
|
private long mChildCurrentLocation = 0; |
|
|
|
private long mChildCurrentLocation = 0; |
|
|
|
private int mBufSize; |
|
|
|
private int mBufSize; |
|
|
@ -47,14 +45,14 @@ final class SingleThreadTask implements Runnable { |
|
|
|
private long mSleepTime = 0; |
|
|
|
private long mSleepTime = 0; |
|
|
|
|
|
|
|
|
|
|
|
SingleThreadTask(DownloadStateConstance constance, IDownloadListener listener, |
|
|
|
SingleThreadTask(DownloadStateConstance constance, IDownloadListener listener, |
|
|
|
DownloadUtil.ConfigEntity downloadInfo) { |
|
|
|
DownloadUtil.ChildThreadConfigEntity downloadInfo) { |
|
|
|
AriaManager manager = AriaManager.getInstance(AriaManager.APP); |
|
|
|
AriaManager manager = AriaManager.getInstance(AriaManager.APP); |
|
|
|
CONSTANCE = constance; |
|
|
|
CONSTANCE = constance; |
|
|
|
CONSTANCE.CONNECT_TIME_OUT = manager.getDownloadConfig().getConnectTimeOut(); |
|
|
|
CONSTANCE.CONNECT_TIME_OUT = manager.getDownloadConfig().getConnectTimeOut(); |
|
|
|
CONSTANCE.READ_TIME_OUT = manager.getDownloadConfig().getIOTimeOut(); |
|
|
|
CONSTANCE.READ_TIME_OUT = manager.getDownloadConfig().getIOTimeOut(); |
|
|
|
mListener = listener; |
|
|
|
mListener = listener; |
|
|
|
this.mConfigEntity = downloadInfo; |
|
|
|
this.mConfigEntity = downloadInfo; |
|
|
|
if (mConfigEntity.isSupportBreakpoint) { |
|
|
|
if (mConfigEntity.IS_SUPPORT_BREAK_POINT) { |
|
|
|
mConfigFPath = downloadInfo.CONFIG_FILE_PATH; |
|
|
|
mConfigFPath = downloadInfo.CONFIG_FILE_PATH; |
|
|
|
} |
|
|
|
} |
|
|
|
mBufSize = manager.getDownloadConfig().getBuffSize(); |
|
|
|
mBufSize = manager.getDownloadConfig().getBuffSize(); |
|
|
@ -76,12 +74,13 @@ 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); |
|
|
|
if (mConfigEntity.isSupportBreakpoint) { |
|
|
|
if (mConfigEntity.IS_SUPPORT_BREAK_POINT) { |
|
|
|
Log.d(TAG, "任务【" |
|
|
|
Log.d(TAG, "任务【" |
|
|
|
+ mConfigEntity.TEMP_FILE.getName() |
|
|
|
+ mConfigEntity.TEMP_FILE.getName() |
|
|
|
+ "】线程__" |
|
|
|
+ "】线程__" |
|
|
@ -102,8 +101,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 +119,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; |
|
|
|
} |
|
|
|
} |
|
|
@ -133,7 +127,7 @@ final class SingleThreadTask implements Runnable { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
//支持断点的处理
|
|
|
|
//支持断点的处理
|
|
|
|
if (mConfigEntity.isSupportBreakpoint) { |
|
|
|
if (mConfigEntity.IS_SUPPORT_BREAK_POINT) { |
|
|
|
Log.i(TAG, "任务【" |
|
|
|
Log.i(TAG, "任务【" |
|
|
|
+ mConfigEntity.TEMP_FILE.getName() |
|
|
|
+ mConfigEntity.TEMP_FILE.getName() |
|
|
|
+ "】线程__" |
|
|
|
+ "】线程__" |
|
|
@ -161,6 +155,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(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -170,7 +178,7 @@ final class SingleThreadTask implements Runnable { |
|
|
|
protected void stop() { |
|
|
|
protected void stop() { |
|
|
|
synchronized (AriaManager.LOCK) { |
|
|
|
synchronized (AriaManager.LOCK) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
if (mConfigEntity.isSupportBreakpoint) { |
|
|
|
if (mConfigEntity.IS_SUPPORT_BREAK_POINT) { |
|
|
|
CONSTANCE.STOP_NUM++; |
|
|
|
CONSTANCE.STOP_NUM++; |
|
|
|
Log.d(TAG, "任务【" |
|
|
|
Log.d(TAG, "任务【" |
|
|
|
+ mConfigEntity.TEMP_FILE.getName() |
|
|
|
+ mConfigEntity.TEMP_FILE.getName() |
|
|
@ -211,7 +219,7 @@ final class SingleThreadTask implements Runnable { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected void cancel() { |
|
|
|
protected void cancel() { |
|
|
|
synchronized (AriaManager.LOCK) { |
|
|
|
synchronized (AriaManager.LOCK) { |
|
|
|
if (mConfigEntity.isSupportBreakpoint) { |
|
|
|
if (mConfigEntity.IS_SUPPORT_BREAK_POINT) { |
|
|
|
CONSTANCE.CANCEL_NUM++; |
|
|
|
CONSTANCE.CANCEL_NUM++; |
|
|
|
Log.d(TAG, "任务【" |
|
|
|
Log.d(TAG, "任务【" |
|
|
|
+ mConfigEntity.TEMP_FILE.getName() |
|
|
|
+ mConfigEntity.TEMP_FILE.getName() |
|
|
@ -250,7 +258,7 @@ final class SingleThreadTask implements Runnable { |
|
|
|
if (ex != null) { |
|
|
|
if (ex != null) { |
|
|
|
Log.e(TAG, msg + "\n" + CommonUtil.getPrintException(ex)); |
|
|
|
Log.e(TAG, msg + "\n" + CommonUtil.getPrintException(ex)); |
|
|
|
} |
|
|
|
} |
|
|
|
if (mConfigEntity.isSupportBreakpoint) { |
|
|
|
if (mConfigEntity.IS_SUPPORT_BREAK_POINT) { |
|
|
|
writeConfig(false, currentLocation); |
|
|
|
writeConfig(false, currentLocation); |
|
|
|
if (CONSTANCE.isFail()) { |
|
|
|
if (CONSTANCE.isFail()) { |
|
|
|
Log.e(TAG, "任务【" + mConfigEntity.TEMP_FILE.getName() + "】下载失败"); |
|
|
|
Log.e(TAG, "任务【" + mConfigEntity.TEMP_FILE.getName() + "】下载失败"); |
|
|
|