in command.line/java/jetbrains/buildServer/core/runtime/RuntimeUtil.java [224:289]
private final static File loadUrl(final URL source, final File dest, final IProgressMonitor monitor) throws IOException {
monitor.status(new ProgressStatus(ProgressStatus.OK, new StringBuilder("Downloading object from ").append(source).append(" to ").append(dest.getAbsolutePath()).append("...").toString()));
HttpURLConnection connection = (HttpURLConnection) source.openConnection();
final int totalContentLength = connection.getContentLength();
final int uiStepTick = totalContentLength / UI_STEPS_COUNT;
monitor.beginTask(String.format("Download"));
monitor.status(new ProgressStatus(ProgressStatus.INFO, String.format("Loading artifact(s) from '%s'", source)));
monitor.status(new ProgressStatus(ProgressStatus.INFO, String.format("Size reported: %s bytes", totalContentLength)));
try {
long loadedBytes = 0;
long uiStepLoadedBytes = 0;
InputStream inputStream = null;
OutputStream outputStream = null;
int threshold = DOWNLOAD_TRY_NUMBER;
boolean cancelled = false;
while (threshold > 0 && !cancelled) {
IOException result = null;
try {
inputStream = connection.getInputStream();
outputStream = new FileOutputStream(dest);
final byte[] buffer = new byte[10 * 1024];
int count;
while ((count = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, count);
loadedBytes += count;
uiStepLoadedBytes += count;
if (uiStepLoadedBytes >= uiStepTick) {
uiStepLoadedBytes = 0;
monitor.status(new ProgressStatus(ProgressStatus.INFO, String.format("%d (Ok)", loadedBytes, totalContentLength)));
}
//check build is interrupted
if (monitor.isCancelled()) {
monitor.status(new ProgressStatus(ProgressStatus.OK, "Cancel signal recieved"));
cancelled = true;
break;
}
}
} catch (IOException e) {
result = e;
} finally {
try {
close(inputStream);
close(outputStream);
} catch (IOException e) {
result = e;
}
}
if (result == null && !cancelled) {
monitor.status(new ProgressStatus(ProgressStatus.OK, new StringBuilder("Successfully downloaded object from ").append(source).append(" to ").append(dest.getAbsolutePath()).toString()));
monitor.status(new ProgressStatus(ProgressStatus.INFO, String.format("%d downloaded", loadedBytes)));
return dest;
}
--threshold;
loadedBytes = 0;
}
if (cancelled) {
monitor.status(new ProgressStatus(ProgressStatus.INFO, "Download interrupted."));
return dest;
}
} finally {
monitor.done();
connection.disconnect();
}
throw new IOException(new StringBuilder("Unable to download object from ").append(source).append(" to ").append(dest.getAbsolutePath()).append(" from ").append(DOWNLOAD_TRY_NUMBER).append(" tries").toString());
}