protected void performSafeDownload()

in flex-maven-tools/flex-sdk-converter/retrievers/download/src/main/java/org/apache/flex/utilities/converter/retrievers/download/DownloadRetriever.java [244:337]


    protected void performSafeDownload(URI sourceUri, File targetFile) throws IOException {
        RequestConfig config;
        ProxySettings proxySettings = ProxySettings.getProxySettings();
        if(proxySettings != null) {
            HttpHost proxy = new HttpHost(proxySettings.getHost(), proxySettings.getPort());
            config = RequestConfig.custom().setProxy(proxy).build();
        } else {
            config = RequestConfig.DEFAULT;
        }

        CloseableHttpClient httpclient = null;
        try {
            HttpGet httpget = new HttpGet(sourceUri);
            httpget.setConfig(config);
            httpclient = HttpClients.createDefault();
            HttpResponse response = httpclient.execute(httpget);

            String reasonPhrase = response.getStatusLine().getReasonPhrase();
            int statusCode = response.getStatusLine().getStatusCode();
            LOG.info(String.format("statusCode: %d", statusCode));
            LOG.info(String.format("reasonPhrase: %s", reasonPhrase));

            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();

            ReadableByteChannel rbc = null;
            FileOutputStream fos = null;
            try {
                rbc = Channels.newChannel(content);
                fos = new FileOutputStream(targetFile);

            ////////////////////////////////////////////////////////////////////////////////
            // Do the downloading.
            ////////////////////////////////////////////////////////////////////////////////

                final long expectedSize = entity.getContentLength();
                LOG.info("===========================================================");
                LOG.info("Downloading " + sourceUri.toString());
                if (expectedSize <= 0) {
                    try {
                        LOG.info("Unknown size.");
                        IOUtils.copy(content, fos);
                    } finally {
                        // close http network connection
                        content.close();
                    }
                } else {
                    if (expectedSize > 1014 * 1024) {
                        LOG.info("Expected size: " + (expectedSize / 1024 / 1024) + "MB");
                    } else {
                        LOG.info("Expected size: " + (expectedSize / 1024) + "KB");
                    }
                    final ProgressBar progressBar = new ProgressBar(expectedSize);
                    long transferredSize = 0L;
                    while (transferredSize < expectedSize) {
                        // Transfer about 1MB in each iteration.
                        long currentSize = fos.getChannel().transferFrom(rbc, transferredSize, MEGABYTE);
                        if (currentSize < MEGABYTE) {
                            break;
                        }
                        transferredSize += currentSize;
                        progressBar.updateProgress(transferredSize);
                    }
                    fos.close();
                    LOG.info("");
                }
                LOG.info("Finished downloading.");
                LOG.info("===========================================================");
            } finally {
                if(rbc != null) {
                    try {
                        rbc.close();
                    } catch (IOException e) {
                        // Ignore ...
                    }
                }
                if(fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        // Ignore ...
                    }
                }
            }
        } finally {
            if(httpclient != null) {
                try {
                    httpclient.close();
                } catch(IOException e) {
                    // Ignore ...
                }
            }
        }
    }