Search in sources :

Example 1 with HttpRangeException

use of com.frostwire.util.http.HttpClient.HttpRangeException in project frostwire by frostwire.

the class InstallerUpdater method handleHttpDownload.

private void handleHttpDownload() {
    File updateFolder = UpdateSettings.UPDATES_DIR;
    String fileName = _updateMessage.getSaveAs() != null ? _updateMessage.getSaveAs() : getFileNameFromHttpUrl();
    File installerFileLocation = new File(updateFolder, fileName);
    if (!updateFolder.exists()) {
        updateFolder.mkdir();
        updateFolder.setWritable(true);
    }
    try {
        HttpClient httpClient = HttpClientFactory.getInstance(HttpClientFactory.HttpContext.MISC);
        httpClient.setListener(new HttpClient.HttpClientListener() {

            long contentLength;

            long downloaded = 0;

            @Override
            public void onError(HttpClient client, Throwable e) {
            }

            @Override
            public void onData(HttpClient client, byte[] buffer, int offset, int length) {
                downloaded += length;
                downloadProgress = (int) ((float) downloaded / contentLength * 100.0);
            }

            @Override
            public void onComplete(HttpClient client) {
            }

            @Override
            public void onCancel(HttpClient client) {
            }

            @Override
            public void onHeaders(HttpClient httpClient, Map<String, List<String>> headerFields) {
                if (headerFields.containsKey("Content-Length")) {
                    contentLength = Long.valueOf(headerFields.get("Content-Length").get(0));
                }
            }
        });
        try {
            httpClient.save(_updateMessage.getInstallerUrl(), installerFileLocation, true);
        } catch (HttpRangeException e) {
            // recovery in case the server does not support resume
            httpClient.save(_updateMessage.getInstallerUrl(), installerFileLocation, false);
        } catch (IOException e2) {
            if (e2.getMessage().contains("416")) {
                // HTTP Request Range error came through IOException.
                httpClient.save(_updateMessage.getInstallerUrl(), installerFileLocation, false);
            }
        }
        isDownloadingUpdate = false;
        downloadComplete();
    } catch (Throwable e) {
        isDownloadingUpdate = false;
        LOG.error("Failed to download installer: " + _updateMessage.getInstallerUrl(), e);
    }
}
Also used : HttpRangeException(com.frostwire.util.http.HttpClient.HttpRangeException) IOException(java.io.IOException) HttpClient(com.frostwire.util.http.HttpClient) List(java.util.List) File(java.io.File)

Aggregations

HttpClient (com.frostwire.util.http.HttpClient)1 HttpRangeException (com.frostwire.util.http.HttpClient.HttpRangeException)1 File (java.io.File)1 IOException (java.io.IOException)1 List (java.util.List)1