001: package net.sf.jftp.net;
002:
003: import java.io.BufferedInputStream;
004: import java.io.BufferedOutputStream;
005: import java.io.File;
006: import java.io.FileOutputStream;
007: import java.net.URL;
008: import java.util.Vector;
009:
010: import net.sf.jftp.system.StringUtils;
011: import net.sf.jftp.system.logging.Log;
012:
013: public class HttpTransfer extends Transfer implements Runnable {
014: private String url;
015: private String localPath;
016: private String file;
017: public boolean work = true;
018: public boolean pause = false;
019: public Thread runner;
020: private Vector listeners;
021: private int stat = 1;
022: private ConnectionHandler handler = new ConnectionHandler();
023:
024: public HttpTransfer(String url, String localPath, Vector listeners,
025: ConnectionHandler handler) {
026: this .url = url;
027: this .localPath = localPath;
028: this .listeners = listeners;
029: this .handler = handler;
030:
031: file = StringUtils.getFile(url);
032:
033: prepare();
034: }
035:
036: public void prepare() {
037: runner = new Thread(this );
038: runner.setPriority(Thread.MIN_PRIORITY);
039: runner.start();
040: }
041:
042: public void run() {
043: try {
044: if (handler.getConnections().get(file) == null) {
045: Log.out("download started: " + url);
046: Log.out("connection handler present: " + handler
047: + ", poll size: "
048: + handler.getConnections().size());
049: Log.out("local file: " + localPath + file);
050: handler.addConnection(file, this );
051: } else {
052: Log.debug("Transfer already in progress: " + file);
053: work = false;
054: stat = 2;
055:
056: return;
057: }
058:
059: URL u = new URL(url);
060:
061: BufferedOutputStream f = new BufferedOutputStream(
062: new FileOutputStream(localPath + file));
063: BufferedInputStream in = new BufferedInputStream(u
064: .openStream());
065: byte[] buf = new byte[4096];
066: int len = 0;
067:
068: while ((stat > 0) && work) {
069: stat = in.read(buf);
070:
071: if (stat == -1) {
072: break;
073: }
074:
075: f.write(buf, 0, stat);
076:
077: len += stat;
078: fireProgressUpdate(file, DataConnection.GET, len);
079: }
080:
081: f.flush();
082: f.close();
083: in.close();
084:
085: fireProgressUpdate(file, DataConnection.FINISHED, len);
086: } catch (Exception ex) {
087: work = false;
088: Log.debug("Download failed: " + ex.toString());
089:
090: File f = new File(localPath + file);
091: f.delete();
092: fireProgressUpdate(file, DataConnection.FAILED, -1);
093:
094: ex.printStackTrace();
095:
096: return;
097: }
098:
099: if (!work) {
100: File f = new File(localPath + file);
101: f.delete();
102: Log.out("download aborted: " + file);
103: }
104: }
105:
106: public void fireProgressUpdate(String file, String type, int bytes) {
107: if (listeners == null) {
108: return;
109: }
110:
111: for (int i = 0; i < listeners.size(); i++) {
112: ((ConnectionListener) listeners.elementAt(i))
113: .updateProgress(file, type, bytes);
114: }
115: }
116:
117: public int getStatus() {
118: return stat;
119: }
120:
121: public boolean hasStarted() {
122: return true;
123: }
124:
125: public FtpConnection getFtpConnection() {
126: return null;
127: }
128:
129: public DataConnection getDataConnection() {
130: return null;
131: }
132: }
|