HttpRequest request;
try {
destination.getParentFile().mkdirs();
request = netHttpTransport.createRequestFactory().buildGetRequest(new GenericUrl(uri));
HttpResponse response = request.execute();
if (response.getStatusCode() != HttpStatusCodes.STATUS_CODE_OK) {
throw new RuntimeException("Invalid request: " + uri);
}
final HttpHeaders headers = response.getHeaders();
final String lastModified = headers.getLastModified();
final Long size = headers.getContentLength() == null ? 0 : headers.getContentLength();
final Date date = lastModified == null ? new Date(0) : dateFormat.get().parse(lastModified);
if (!overwrite && destination.exists() && destination.lastModified() >= date.getTime() && destination.length() == size) {
logger.info("File on disk is same or newer : " + destination);
return;
}
InputStream inputStream = response.getContent();
ReadableByteChannel rbc = Channels.newChannel(inputStream);
FileOutputStream fos = new FileOutputStream(destination);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();