Fix filedownloader. (#28)

This commit is contained in:
Zaxar163 2018-10-01 14:59:34 +03:00 committed by zaxar163
parent e133fc06cb
commit fccd793168

View file

@ -6,69 +6,138 @@
import java.io.IOException; import java.io.IOException;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.security.cert.Certificate;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import javax.net.ssl.HttpsURLConnection;
import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.IOHelper;
import ru.gravit.utils.helper.LogHelper; import ru.gravit.utils.helper.LogHelper;
public class Downloader implements Runnable { public class Downloader implements Runnable {
public static final int INTERVAL = 300; @FunctionalInterface
public static interface Handler {
public AtomicInteger writed = new AtomicInteger(0); public void check(Certificate[] certs) throws IOException;
private final File file; }
private final URL url;
public final AtomicBoolean interrupt = new AtomicBoolean(false); public static final int INTERVAL = 300;
public final AtomicBoolean interrupted = new AtomicBoolean(false);
private final int skip; private final File file;
private final URL url;
public AtomicInteger writed = new AtomicInteger(0);
public final AtomicBoolean interrupt = new AtomicBoolean(false);
public final AtomicBoolean interrupted = new AtomicBoolean(false);
public AtomicReference<Throwable> ex = new AtomicReference<>(null); public AtomicReference<Throwable> ex = new AtomicReference<>(null);
private final int skip;
private final Handler handler;
public Downloader(URL url, File file) { public Downloader(URL url, File file) {
this.file = file; this.file = file;
this.url = url; this.url = url;
this.skip = 0; this.skip = 0;
} this.handler = null;
}
public Downloader(URL url, File file, int skip) { public Downloader(URL url, File file, int skip) {
this.file = file; this.file = file;
this.url = url; this.url = url;
this.skip = skip; this.skip = skip;
} this.handler = null;
}
public File getFile() {
return file;
}
public void downloadFile() throws IOException { public Downloader(URL url, File file, Handler handler) {
if (!(url.getProtocol().equalsIgnoreCase("http") || url.getProtocol().equalsIgnoreCase("https"))) throw new IOException("Invalid protocol."); this.file = file;
HttpURLConnection connect = (HttpURLConnection) (url).openConnection(); this.url = url;
connect.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11"); // for stupid servers this.skip = 0;
connect.setInstanceFollowRedirects(true); this.handler = handler;
if (!(connect.getResponseCode() >= 200 && connect.getResponseCode() < 300)) throw new IOException(String.format("Invalid response of http server %d.", connect.getResponseCode())); }
try (BufferedInputStream in = new BufferedInputStream(connect.getInputStream(), IOHelper.BUFFER_SIZE); FileOutputStream fout = new FileOutputStream(file, skip != 0)) {
final byte data[] = new byte[IOHelper.BUFFER_SIZE]; public Downloader(URL url, File file, int skip, Handler handler) {
int count = -1; this.file = file;
long timestamp = System.currentTimeMillis(); this.url = url;
int writed_local = 0; this.skip = skip;
in.skip(skip); this.handler = handler;
while ((count = in.read(data)) != -1) { }
fout.write(data, 0, count);
writed_local += count; public File getFile() {
if (System.currentTimeMillis() - timestamp > INTERVAL) { return file;
writed.set(writed_local); }
LogHelper.debug("Downloaded %d", writed_local);
if (interrupt.get()) { public Handler getHandler() {
break; return handler;
} }
}
} public void downloadFile() throws IOException {
LogHelper.debug("Downloaded %d", writed_local); if (!(url.getProtocol().equalsIgnoreCase("http") || url.getProtocol().equalsIgnoreCase("https")))
writed.set(writed_local); throw new IOException("Invalid protocol.");
} interrupted.set(false);
interrupted.set(true); if (url.getProtocol().equalsIgnoreCase("http")) {
} HttpURLConnection connect = (HttpURLConnection) (url).openConnection();
connect.setRequestProperty("User-Agent",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11"); // for
// stupid
// servers
connect.setInstanceFollowRedirects(true);
if (!(connect.getResponseCode() >= 200 && connect.getResponseCode() < 300))
throw new IOException(String.format("Invalid response of http server %d.", connect.getResponseCode()));
try (BufferedInputStream in = new BufferedInputStream(connect.getInputStream(), IOHelper.BUFFER_SIZE);
FileOutputStream fout = new FileOutputStream(file, skip != 0)) {
final byte data[] = new byte[IOHelper.BUFFER_SIZE];
int count = -1;
long timestamp = System.currentTimeMillis();
int writed_local = 0;
in.skip(skip);
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;
}
}
}
LogHelper.debug("Downloaded %d", writed_local);
writed.set(writed_local);
}
} else {
HttpsURLConnection connect = (HttpsURLConnection) (url).openConnection();
connect.setRequestProperty("User-Agent",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11"); // for
// stupid
// servers
connect.setInstanceFollowRedirects(true);
if (handler != null)
handler.check(connect.getServerCertificates());
if (!(connect.getResponseCode() >= 200 && connect.getResponseCode() < 300))
throw new IOException(String.format("Invalid response of http server %d.", connect.getResponseCode()));
try (BufferedInputStream in = new BufferedInputStream(connect.getInputStream(), IOHelper.BUFFER_SIZE);
FileOutputStream fout = new FileOutputStream(file, skip != 0)) {
final byte data[] = new byte[IOHelper.BUFFER_SIZE];
int count = -1;
long timestamp = System.currentTimeMillis();
int writed_local = 0;
in.skip(skip);
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;
}
}
}
LogHelper.debug("Downloaded %d", writed_local);
writed.set(writed_local);
}
}
interrupted.set(true);
}
@Override @Override
public void run() { public void run() {
@ -77,6 +146,6 @@ public void run() {
} catch (Throwable ex) { } catch (Throwable ex) {
this.ex.set(ex); this.ex.set(ex);
LogHelper.error(ex); LogHelper.error(ex);
} }
} }
} }