mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-12-23 09:01:08 +03:00
[FEATURE] Готов Downloader.
This commit is contained in:
parent
1819e1c95c
commit
d06fcafdc7
3 changed files with 8 additions and 50 deletions
|
@ -130,22 +130,6 @@ public void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) thr
|
|||
lastContentFuture = sendFileFuture;
|
||||
}
|
||||
|
||||
sendFileFuture.addListener(new ChannelProgressiveFutureListener() {
|
||||
@Override
|
||||
public void operationProgressed(ChannelProgressiveFuture future, long progress, long total) {
|
||||
if (total < 0) { // total unknown
|
||||
System.err.println(future.channel() + " Transfer progress: " + progress);
|
||||
} else {
|
||||
System.err.println(future.channel() + " Transfer progress: " + progress + " / " + total);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void operationComplete(ChannelProgressiveFuture future) {
|
||||
System.err.println(future.channel() + " Transfer complete.");
|
||||
}
|
||||
});
|
||||
|
||||
// Decide whether to close the connection or not.
|
||||
if (!HttpUtil.isKeepAlive(request)) {
|
||||
lastContentFuture.addListener(new ClosingChannelFutureListener(raf));
|
||||
|
|
|
@ -39,6 +39,8 @@ public interface Handler {
|
|||
private final int skip;
|
||||
private final Handler handler;
|
||||
|
||||
private HttpURLConnection connect = null;
|
||||
|
||||
public Downloader(URL url, File file) {
|
||||
this.requestProps = new HashMap<>(requestClient);
|
||||
this.file = file;
|
||||
|
@ -128,6 +130,7 @@ public void downloadFile() throws IOException {
|
|||
interrupted.set(false);
|
||||
if (url.getProtocol().equalsIgnoreCase("http")) {
|
||||
HttpURLConnection connect = (HttpURLConnection) (url).openConnection();
|
||||
this.connect = connect;
|
||||
if (method != null) connect.setRequestMethod(method);
|
||||
for (Map.Entry<String, String> ent : requestProps.entrySet()) {
|
||||
connect.setRequestProperty(ent.getKey(), ent.getValue());
|
||||
|
@ -158,6 +161,7 @@ public void downloadFile() throws IOException {
|
|||
}
|
||||
} else {
|
||||
HttpsURLConnection connect = (HttpsURLConnection) (url).openConnection();
|
||||
this.connect = connect;
|
||||
if (method != null) connect.setRequestMethod(method);
|
||||
for (Map.Entry<String, String> ent : requestProps.entrySet()) {
|
||||
connect.setRequestProperty(ent.getKey(), ent.getValue());
|
||||
|
@ -200,5 +204,9 @@ public void run() {
|
|||
this.ex.set(ex);
|
||||
LogHelper.error(ex);
|
||||
}
|
||||
if (connect != null)
|
||||
try {
|
||||
connect.disconnect();
|
||||
} catch (Throwable ignored) { }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,34 +0,0 @@
|
|||
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