package com.downloader.model;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.io.Serializable;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.UUID;
import com.downloader.util.URLTools;
public class Task implements Serializable {
private static final long serialVersionUID = -3619398241803514013L;
public static final int UNSTARTED = -2;
public static final int PAUSED = -1;
public static final int DOWNLOADING = 0;
public static final int FINISHED = 1;
private String id = null;
private URL url = null;
private File path = null;
private DownloadThread downloadthread = null;
private UpdateThread updatethread = null;
private int size = 0;
private int downloaded = 0;
private int state = UNSTARTED;
private double speed = 0;
public Task(final URL url) throws IOException {
// TODO Auto-generated constructor stub
id = UUID.randomUUID().toString();
this.url = url;
this.path = new File(CurrentSettings.getPath(), URLTools.getFileName(url));
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
size = conn.getContentLength();
conn.disconnect();
downloadthread = new DownloadThread();
updatethread = new UpdateThread();
}
public Task(final String spec) throws IOException {
// TODO Auto-generated constructor stub
this(new URL(spec));
}
public void startDownload() {
if (state == UNSTARTED) {
state = DOWNLOADING;
updatethread.start();
downloadthread.start();
}
}
public void pauseDownload() {
if (state == DOWNLOADING) {
state = PAUSED;
}
}
public void resumeDownload() {
if (state == PAUSED) {
state = DOWNLOADING;
downloadthread = new DownloadThread();
updatethread = new UpdateThread();
updatethread.start();
downloadthread.start();
}
}
public String getId() {
return id;
}
public URL getUrl() {
return url;
}
public File getPath() {
return path;
}
public int getSize() {
return size;
}
public synchronized int getDownloaded() {
return downloaded;
}
private synchronized void addDownloaded(int size) {
downloaded = downloaded + size;
}
public int getState() {
return state;
}
public double getSpeed() {
if (state == DOWNLOADING) {
return speed;
} else {
return 0;
}
}
public double getProgress() {
return ((double) downloaded / size);
}
private class UpdateThread extends Thread implements Serializable {
private static final long serialVersionUID = -2462261743477870556L;
@Override
public void run() {
// TODO Auto-generated method stub
while (state == DOWNLOADING) {
long d1 = getDownloaded();
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long d2 = getDownloaded();
speed = ((d2 - d1) / 0.2) / 1024;
}
}
}
private class DownloadThread extends Thread implements Serializable {
private static final long serialVersionUID = 1106138922362623787L;
@Override
public void run() {
// TODO Auto-generated method stub
HttpURLConnection connection = null;
InputStream in = null;
RandomAccessFile raf = null;
try {
connection = (HttpURLConnection) url.openConnection();
String range = "bytes=" + getDownloaded() + "-";
connection.setRequestProperty("Range", range);
connection.connect();
in = connection.getInputStream();
raf = new RandomAccessFile(path, "rw");
raf.seek(getDownloaded());
byte[] buffer = new byte[CurrentSettings.getBufferSize()];
int readed = 0;
while ((state == DOWNLOADING) && (readed = in.read(buffer)) > 0) {
raf.write(buffer, 0, readed);
addDownloaded(readed);
}
if (state == DOWNLOADING) {
state = FINISHED;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (raf != null) {
raf.close();
}
if (in != null) {
in.close();
}
if (connection != null) {
connection.disconnect();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
Java断点下载
最新推荐文章于 2024-04-29 17:36:54 发布