int index = source.lastIndexOf('/');
String srcName = index == -1?source:source.substring(index+1);
final File to = destDir == null ? new File(srcName):new File(destDir, srcName);
final IOException ex[] = new IOException[1];
Thread get = new IvyThread() {
public void run() {
initContext();
try {
sendCommand("get "+source, getExpectedDownloadMessage(source, to), 0);
} catch (IOException e) {
ex[0] = e;
}
}
};
get.start();
long prevLength = 0;
long lastUpdate = System.currentTimeMillis();
long timeout = _readTimeout;
while (get.isAlive()) {
checkInterrupted();
long length = to.exists()?to.length():0;
if (length > prevLength) {
fireTransferProgress(length - prevLength);
lastUpdate = System.currentTimeMillis();
prevLength = length;
} else {
if (System.currentTimeMillis() - lastUpdate > timeout) {
Message.verbose("download hang for more than "+timeout+"ms. Interrupting.");
get.interrupt();
if (to.exists()) to.delete();
throw new IOException(source+" download timeout from "+getHost());
}
}
try {
get.join(100);
} catch (InterruptedException e) {
if (to.exists()) to.delete();
return;
}
}