mirror of
https://github.com/GravitLauncher/Launcher
synced 2025-04-01 22:14:01 +03:00
Added thread downloader.
This commit is contained in:
parent
60914c929a
commit
8a762f1d84
2 changed files with 49 additions and 6 deletions
|
@ -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<IOException> ex = new AtomicReference<>(null);
|
||||
|
||||
public AtomicReference<Throwable> 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);
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue