Merge pull request #188 from GravitLauncher/feature/downloader-auto

[FEATURE] Готов Downloader.
This commit is contained in:
Gravit 2019-03-10 19:05:22 +07:00 committed by GitHub
commit 962137cc48
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 50 deletions

View file

@ -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));

View file

@ -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) { }
}
}

View file

@ -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();
}
}