1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
package org.openslx.dozmod.filetransfer;
import java.io.File;
import java.io.IOException;
import org.apache.log4j.Logger;
import org.openslx.filetransfer.UploadStatusCallback;
import org.openslx.filetransfer.Uploader;
/**
* Executes the file upload in a background thread and updates progress to
* listeners that implement the java.beans.PropertyChangeListener interface.
*
* @author www.codejava.net
*
*/
public class UploadTask implements Runnable {
/**
* Logger instance for this class.
*/
private final static Logger LOGGER = Logger.getLogger(UploadTask.class);
private static final double UPDATE_INTERVAL_SECONDS = 0.6;
private static final double UPDATE_INTERVAL_MS = UPDATE_INTERVAL_SECONDS * 1000;
private static final double BYTES_PER_MIB = 1024 * 1024;
private final String host;
private final int port;
private final String uploadToken;
private final File uploadFile;
public UploadTask(String host, int port, String uploadToken, File uploadFile) {
// TODO: SSL
this.host = host;
this.port = port;
this.uploadToken = uploadToken;
this.uploadFile = uploadFile;
}
/**
* Executed in background thread
*/
@Override
public void run() {
Uploader upload = null;
try {
upload = new Uploader(host, port, null, uploadToken); // TODO: SSL
final Uploader ul = upload;
/* BROKEN CODE - FIXME class imports
final long fileSize = Image.Filesize = uploadFile.length();
BROKEN CODE - FIXME class imports */
final long fileSize = 1000000;
LOGGER.debug("Filesize: " + fileSize);
final boolean ret = upload.upload(uploadFile.getAbsolutePath(), new UploadStatusCallback() {
// progress counter
private long totalBytesRead = 0;
// initialize the counters needed for speed calculations
private long lastUpdate = 0;
private long lastBytes = 0;
private long currentBytes = 0;
@Override
public void uploadProgress(long bytesSent) {
totalBytesRead += bytesSent;
currentBytes += bytesSent;
final long now = System.currentTimeMillis();
if (lastUpdate + UPDATE_INTERVAL_MS < now) {
final int percentCompleted = (int) ((totalBytesRead * 100) / fileSize);
lastBytes = (lastBytes * 2 + currentBytes) / 3;
final double speed = lastBytes / UPDATE_INTERVAL_SECONDS;
LOGGER.debug(percentCompleted + "% complete (speed: " + speed/BYTES_PER_MIB + ", total: " + totalBytesRead + ")");
lastUpdate = now;
currentBytes = 0;
}
}
@Override
public void uploadError(String message) {
LOGGER.error("Upload error: " + message);
//Gui.showMessageBox(null, message, MessageType.ERROR, LOGGER, null);
}
});
// if the upload succeeded, set the progress to 100% manually again here to make
// sure the GUI knows about it.
if (ret)
LOGGER.info("Upload completed.");
else
LOGGER.info("Upload failed.");
} catch (IOException e) {
LOGGER.error("Upload of " + uploadFile.getAbsolutePath() + " failed: ", e);
} finally {
if (upload != null)
upload.close(null);
}
return;
}
}
|