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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
package NET.worlds.network;
import NET.worlds.console.Console;
import NET.worlds.console.PolledDialog;
import NET.worlds.core.Std;
import java.awt.Event;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Label;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Vector;
public class ProgressDialog extends PolledDialog {
private static final long serialVersionUID = 7533646170494183110L;
private static final String bytesMsg = Console.message("Bytes-remaining");
private int bytesTotal;
private int bytesLoaded;
private ProgressBar progressBar;
private Label progressBytes;
private Label progressTime;
private int startTime = Std.getFastTime();
private CacheFile cf;
private boolean cancelled;
private int wholeFileBytes = 0;
public ProgressDialog(int totalSize) {
super(Console.getFrame(), null, Console.message("Download-Progress"), false);
this.bytesTotal = totalSize;
this.progressBar = new ProgressBar(240);
NumberFormat nF = NumberFormat.getNumberInstance(Locale.getDefault());
String strSize = nF.format((long)totalSize);
this.progressBytes = new Label(bytesMsg + strSize);
this.progressTime = new Label("");
this.setAlignment(1);
this.readySetGo();
}
public boolean loadFiles(Vector<String> names, Vector<URL> urls) throws IOException {
try {
int count = names.size();
int i = 0;
while (true) {
if (i >= count) {
return true;
}
synchronized (this) {
if (this.cancelled) {
NetUpdate.warnUser("Upgrade cancelled.");
break;
}
if (this.cf != null) {
this.wholeFileBytes = this.wholeFileBytes + this.cf.bytesLoaded();
}
this.cf = Cache.getFile(urls.elementAt(i));
}
this.cf.waitUntilLoaded();
if (!this.cf.isActive()) {
this.cf.markTemporary();
NetUpdate.warnUser("Upgrade cancelled.");
break;
}
if (this.cf.error()) {
this.cf.markTemporary();
NetUpdate.warnUser("Error getting upgrade info, try again later");
break;
}
copyFile(this.cf.getLocalName(), names.elementAt(i));
this.cf.markTemporary();
this.cf.close();
i++;
}
} finally {
this.done(true);
}
return false;
}
public static boolean copyFile(String from, String to) {
int lastSlash = to.lastIndexOf(47);
int lastBackslash = to.lastIndexOf(92);
if (lastBackslash > lastSlash) {
lastSlash = lastBackslash;
}
if (lastSlash >= 0) {
new File(to.substring(0, lastSlash)).mkdirs();
}
FileInputStream s = null;
FileOutputStream d = null;
try {
try {
s = new FileInputStream(from);
d = new FileOutputStream(to);
byte[] buffer = new byte[1024];
while (true) {
int got = s.read(buffer);
if (got == -1) {
return true;
}
d.write(buffer, 0, got);
}
} finally {
if (d != null) {
d.close();
}
if (s != null) {
s.close();
}
}
} catch (IOException var12) {
return false;
}
}
@Override
protected void build() {
GridBagLayout gbag = new GridBagLayout();
this.setLayout(gbag);
GridBagConstraints c = new GridBagConstraints();
c.anchor = 10;
c.gridwidth = 0;
c.weightx = 1.0;
c.weighty = 1.0;
this.add(gbag, new Label(Console.message("Downloading-update")), c);
c.insets = new Insets(2, 2, 2, 2);
this.add(gbag, this.progressBar, c);
c.insets = new Insets(0, 2, 0, 2);
c.anchor = 17;
this.add(gbag, this.progressBytes, c);
c.fill = 2;
this.add(gbag, this.progressTime, c);
}
@Override
protected synchronized void activeCallback() {
int n;
synchronized (this) {
n = this.wholeFileBytes;
if (this.cf != null) {
n += this.cf.bytesLoaded();
}
}
if (n != this.bytesLoaded) {
this.bytesLoaded = n;
this.progressBar.setProgress((double)this.bytesLoaded / this.bytesTotal);
int bytesRemaining = this.bytesTotal - this.bytesLoaded;
NumberFormat nF = NumberFormat.getNumberInstance(Locale.getDefault());
String strRemain = nF.format((long)bytesRemaining);
this.progressBytes.setText(bytesMsg + strRemain);
int elapsed = (Std.getFastTime() - this.startTime) / 1000;
if (elapsed > 0) {
double bytesPerSec = (double)this.bytesLoaded / elapsed;
if (bytesPerSec > 0.0) {
String bps = formatTime((long)(bytesRemaining / bytesPerSec));
Object[] arguments = new Object[]{new String(bps)};
this.progressTime.setText(MessageFormat.format(Console.message("Time-remaining"), arguments));
}
}
}
this.notify();
}
@Override
protected boolean done(boolean confirmed) {
synchronized (this) {
this.cancelled = true;
if (this.cf != null) {
this.cf.close();
}
}
return super.done(confirmed);
}
private static String fmtTwoDigit(long val) {
return val < 10L ? "0" + val : "" + val;
}
private static String formatTime(long secs) {
String result = "";
long hrs = secs / 3600L;
secs -= hrs * 3600L;
long mins = secs / 60L;
secs -= mins * 60L;
if (hrs > 0L) {
result = result + hrs + ":";
}
return result + fmtTwoDigit(mins) + ":" + fmtTwoDigit(secs);
}
@Override
public boolean handleEvent(Event event) {
if (event.id == 1004) {
Console.getFrame().requestFocus();
return true;
} else {
return super.handleEvent(event);
}
}
}
|