📄 taskinfo.java
字号:
package com.leapget.util;
import java.util.Calendar;
import javax.swing.JFrame;
public class TaskInfo {
private String fileUrl; // 文件的网络地址
private String savePath; // 保存路径
private int threadCount; // 线程数目
private int totalBytes; // 文件的总大小
private int downloadedBytes; // 已经下载的字节数
private long costMillis; // 下载所用的全部时间(单位毫秒)
private long startMillis; // 任务开始时间(单位毫秒)
private int order; // 任务的序号
private int state; // 线程的状态
private int lastDownloaded; // 上次已经下载的字节数,用于计算即时速度
private String currentSpeed;
private long lastMillis = Calendar.getInstance().getTimeInMillis();
private JFrame mainFrame;
// 定义任务状态
public static final int TASK_READY = 0; // 就绪
public static final int TASK_NORMAL = 1; // 下载
public static final int TASK_PAUSED = 2; // 暂停
public static final int TASK_COMPLETE = 3; // 完成
public static final int TASK_DELETEED = 4; // 删除
public static final int TASK_ERROR = 5; // 错误
public static final String[] STATE = { "就绪", "下载", "暂停", "完成", "删除", "错误" };
public TaskInfo(String fileUrl, String savePath, int threadCount) {
this.fileUrl = fileUrl;
this.savePath = savePath;
this.threadCount = threadCount;
this.downloadedBytes = 0;
this.costMillis = 0;
this.state = TASK_READY;
}
public void setMainFrame(JFrame mainFrame){
this.mainFrame = mainFrame;
}
public JFrame getMainFrame(){
return mainFrame;
}
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
public long getCostMillis() {
return costMillis;
}
public String getStrCostTime(){
int seconds = (int) ((costMillis/1000)<=0?1:(costMillis/1000));
int second = seconds%60;
int minute = seconds/60%60;
int hour = seconds/3600%60;
return (hour<=9?("0"+hour):hour) + ":" + (minute<=9?("0"+minute):minute) + ":" + (second<=9?("0"+second):second);
}
public void setCostMillis(long costMillis) {
this.costMillis = costMillis;
}
public long getStartMillis() {
return startMillis;
}
public void setStartMillis(long startMillis) {
this.startMillis = startMillis;
}
public void setLastMillis(long millis){
this.lastMillis = millis;
}
public int getTotalBytes() {
return totalBytes;
}
public String getSize(){
return getStrSize(totalBytes);
}
public void setTotalBytes(int totalBytes) {
this.totalBytes = totalBytes;
}
public int getDownloadedBytes() {
return downloadedBytes;
}
public void setDownloadedBytes(int downloadedBytes) {
this.downloadedBytes = downloadedBytes;
}
public void addDownloadedBytes(int len) {
this.downloadedBytes += len;
}
public String getFileUrl() {
return fileUrl;
}
public void setFileUrl(String fileUrl) {
this.fileUrl = fileUrl;
}
public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public int getThreadCount() {
return threadCount;
}
public void setThreadCount(int threadCount) {
this.threadCount = threadCount<0?0:threadCount;
}
public String getStrState() {
return STATE[state];
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
if(Debug.isDebug){
System.out.println("任务 " + savePath + " 被 " + getStrState() + " .");
}
}
public String getAvgSpeed(){
return getStrSpeed(totalBytes, costMillis);
}
public String getCurrentSpeed(){
if(state == TASK_NORMAL){
int bytes = downloadedBytes - lastDownloaded;
long millis = Calendar.getInstance().getTimeInMillis() - lastMillis;
if(millis > 999){
currentSpeed = getStrSpeed(bytes, millis);
lastDownloaded = downloadedBytes;
lastMillis = Calendar.getInstance().getTimeInMillis();
costMillis += millis;
}
}else if(state == TASK_COMPLETE){
currentSpeed = getAvgSpeed();
}else{
currentSpeed = "";
}
return currentSpeed;
}
private String getStrSpeed(int bytes, long millis){
double speed = bytes / (millis<1?1:millis) * 1000.0;
return speed<=0?"":(getStrSize(speed) + "/S");
}
private String getStrSize(double bytes){
String strSize;
if(bytes > 1024){
if(bytes > 1048576 /*1024*1024*/) strSize = ((int)(bytes/1048576*100)+50)/100.0 + " MB";
else strSize = ((int)(bytes/1024*100)+50)/100.0 + " KB";
}else{
strSize = bytes + " B";
}
return strSize;
}
// 获得下载进度
public int getProgress() {
return (int)((downloadedBytes / (totalBytes<=0?1.0:totalBytes*1.0)) * 100);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -