📄 taskinfo.java
字号:
import java.net.*;
import java.util.*;
import java.io.*;
/*
* this class save the task info
* only TaskThread can operate the instance content of this class's
* others can only read it
*/
public class TaskInfo implements Serializable {
public static final int TASK_STATE_FINISHED = 3;
public static final int TASK_STATE_PAUSE = 2;
public static final int TASK_STATE_RUN = 1;
public static final int TASK_STATE_STOP = 0;
transient private boolean debug = true;
private File dstFile = null;
// these parameters are deciding in runtime
private HashSet<TaskPortionInfo> portionList = null;
private int portionNumber = 0;
// used for calculate speed...
transient private long startTime = 0;
transient private int state = TaskInfo.TASK_STATE_STOP;
// these parameters are used for assistant
private long totalLength = 0; // bytes
// these parameters should be initiated when object first construct
private URL url = null;
// the only way to get instance of this class
// if url is not correct or connect failed, return a null reference
public static TaskInfo getInstance(String url, String dstFile,
int portionNumber) {
try {
TaskInfo ti = new TaskInfo();
ti.url = new URL(url);
ti.dstFile = new File(dstFile);
URLConnection conn = ti.url.openConnection();
ti.totalLength = conn.getContentLength();
ti.portionNumber = portionNumber;
ti.allocatePortion();
return ti;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private TaskInfo() {
URL url = null;
File dstFile = null;
portionNumber = 0;
portionList = new HashSet<TaskPortionInfo>();
totalLength = 0;
}
private void allocatePortion() {
long portionSize = totalLength / portionNumber;
long startPos = 0;
// here use -1 to ensure first allocation set startPos to 0
long endPos = -1;
for (int i = 0; i < portionNumber - 1; i++) {
TaskPortionInfo tpi = new TaskPortionInfo();
startPos = endPos + 1;
endPos = startPos + portionSize;
tpi.setStartPos(startPos);
tpi.setNowPos(startPos);
tpi.setLastPos(startPos);
tpi.setEndPos(endPos + 1);
portionList.add(tpi);
}
TaskPortionInfo tpi = new TaskPortionInfo();
tpi.setStartPos(endPos + 1);
tpi.setNowPos(endPos + 1);
tpi.setLastPos(endPos + 1);
tpi.setEndPos(totalLength);
portionList.add(tpi);
}
public boolean finished() {
return (getRemaindSize() <= 0) ? true : false;
}
public long getAcquiredSize() {
if (portionList.isEmpty())
return 0;
Iterator<TaskPortionInfo> it = portionList.iterator();
long acquiredSize = 0;
while (it.hasNext()) {
long m = it.next().getAcquiredPortionSize();
acquiredSize += m;
}
return acquiredSize;
}
public File getDstFile() {
return dstFile;
}
public String getDstFileName() {
return dstFile.getName();
}
public HashSet<TaskPortionInfo> getPortionList() {
return portionList;
}
public int getPortionNumber() {
return portionNumber;
}
public void setLastPos() {
if (portionList.isEmpty())
return;
Iterator<TaskPortionInfo> it = portionList.iterator();
TaskPortionInfo tpi = null;
while (it.hasNext() && !portionList.isEmpty()) {
tpi = it.next();
tpi.setLastPos(tpi.getNowPos());
}
}
public long getRemaindSize() {
if (portionList.isEmpty())
return totalLength;
Iterator<TaskPortionInfo> it = portionList.iterator();
long remaindSize = 0;
while (it.hasNext()) {
remaindSize += it.next().getRemaindPortionSize();
}
return remaindSize;
}
public long getStartTime() {
return startTime;
}
public int getState() {
return state;
}
public long getTotalLength() {
return totalLength;
}
public URL getUrl() {
return url;
}
public String getURLFileName() {
String result = url.toString();
result.substring(result.lastIndexOf("/"));
return result;
}
public void setStartTime(long startTime) {
this.startTime = startTime;
}
public void setState(int state) {
this.state = state;
}
public long getLatestAcquiredSize() {
if (portionList.isEmpty())
return 0;
Iterator<TaskPortionInfo> it = portionList.iterator();
long latestAcquiredSize = 0;
while (it.hasNext()) {
latestAcquiredSize += it.next().getLatestAcquiredSize();
}
return latestAcquiredSize;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -