From 8a762f1d84beeebb8f5c5b6436857e0cd0c8f49f Mon Sep 17 00:00:00 2001 From: zaxar163 Date: Wed, 26 Sep 2018 16:12:59 +0300 Subject: [PATCH] Added thread downloader. --- .../gravit/utils/downloader/Downloader.java | 20 +++++++---- .../utils/downloader/DownloadingThread.java | 35 +++++++++++++++++++ 2 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 libLauncher/src/main/java/ru/gravit/utils/downloader/DownloadingThread.java diff --git a/libLauncher/src/main/java/ru/gravit/utils/downloader/Downloader.java b/libLauncher/src/main/java/ru/gravit/utils/downloader/Downloader.java index 2c7129c4..ace2e765 100644 --- a/libLauncher/src/main/java/ru/gravit/utils/downloader/Downloader.java +++ b/libLauncher/src/main/java/ru/gravit/utils/downloader/Downloader.java @@ -9,17 +9,20 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; +import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.LogHelper; public class Downloader implements Runnable { - public static final int BUFER_SIZE = 8192; public static final int INTERVAL = 300; + public AtomicInteger writed = new AtomicInteger(0); private final File file; private final URL url; public final AtomicBoolean interrupt = new AtomicBoolean(false); + public final AtomicBoolean interrupted = new AtomicBoolean(false); private final int skip; - public AtomicReference ex = new AtomicReference<>(null); + + public AtomicReference ex = new AtomicReference<>(null); public Downloader(URL url, File file) { this.file = file; @@ -39,21 +42,26 @@ public File getFile() { public void downloadFile() throws IOException { try (BufferedInputStream in = new BufferedInputStream(url.openStream()); FileOutputStream fout = new FileOutputStream(file, skip != 0)) { - final byte data[] = new byte[BUFER_SIZE]; + final byte data[] = new byte[IOHelper.BUFFER_SIZE]; int count; long timestamp = System.currentTimeMillis(); int writed_local = 0; in.skip(skip); - while ((count = in.read(data, 0, BUFER_SIZE)) != -1) { + while ((count = in.read(data)) != -1) { fout.write(data, 0, count); writed_local += count; if (System.currentTimeMillis() - timestamp > INTERVAL) { writed.set(writed_local); LogHelper.debug("Downloaded %d", writed_local); - if (interrupt.get()) break; + if (interrupt.get()) { + interrupted.set(true); + break; + } } } + LogHelper.debug("Downloaded %d", writed_local); writed.set(writed_local); + interrupted.set(true); } } @@ -61,7 +69,7 @@ public void downloadFile() throws IOException { public void run() { try { downloadFile(); - } catch (IOException ex) { + } catch (Throwable ex) { this.ex.set(ex); LogHelper.error(ex); } diff --git a/libLauncher/src/main/java/ru/gravit/utils/downloader/DownloadingThread.java b/libLauncher/src/main/java/ru/gravit/utils/downloader/DownloadingThread.java new file mode 100644 index 00000000..dcf1c70a --- /dev/null +++ b/libLauncher/src/main/java/ru/gravit/utils/downloader/DownloadingThread.java @@ -0,0 +1,35 @@ +package ru.gravit.utils.downloader; + +import java.io.File; +import java.net.URL; + +public class DownloadingThread extends Thread { + private final Downloader runnable; + + public DownloadingThread(File file, URL url, String name) { + super(name); + runnable = new Downloader(url, file); + } + + public Downloader getDownloader() { + return runnable; + } + + @Override + public void interrupt() { + runnable.interrupt.set(true); + while (!runnable.interrupted.get()) { + ; + } + super.interrupt(); + } + + public void hardInterrupt() { + super.interrupt(); + } + + @Override + public void run() { + runnable.run(); + } +}