Skip to content

Progression #1

@Blackbalrog

Description

@Blackbalrog

ça m'a permis de faire ma bar de progression au téléchargement de Java

package fr.flowarg.azuljavadownloader;

public interface ProgressCallback
{
	/**
	 * @param downloadedBytes octets déjà téléchargés
	 * @param totalBytes taille totale (peut être -1 si inconnue)
	 */
	void onProgress(long downloadedBytes, long totalBytes);
}

AzulJavaDownloader

public void setProgressCallback(ProgressCallback callback)
{
        this.progressCallback = callback;
}
public Path downloadAndInstall(AzulJavaBuildInfo buildInfo, Path dirPath) throws IOException
{
   ...
   if(Files.notExists(archivePath) || !FileUtils.getMD5(archivePath).equalsIgnoreCase(buildInfo.getMd5Hash()))
   {
            //Files.deleteIfExists(archivePath);
            //Files.copy(new URL(buildInfo.getDownloadUrl()).openStream(), archivePath);

            Files.deleteIfExists(archivePath);
            this.downloadWithProgress(buildInfo.getDownloadUrl(), archivePath);
    }
    ...
}
private void downloadWithProgress(String url, Path target) throws IOException
{
        HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection();
        connection.setInstanceFollowRedirects(true);
        connection.addRequestProperty("User-Agent", "Mozilla/5.0");

        long total = connection.getContentLengthLong();
        long downloaded = 0L;

        try (InputStream inputStream = new BufferedInputStream(connection.getInputStream()); OutputStream outputStream = new BufferedOutputStream(Files.newOutputStream(target, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)))
        {
            byte[] buffer = new byte[8192];
            int read;

            while ((read = inputStream.read(buffer)) != -1)
            {
                outputStream.write(buffer, 0, read);
                downloaded += read;

                if (this.progressCallback != null) this.progressCallback.onProgress(downloaded, total);
            }
            if (this.progressCallback != null) this.progressCallback.onProgress(downloaded, total);
        }
        finally
        {
            connection.disconnect();
        }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions