Skip to content

Commit

Permalink
Code Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
isHarryh committed Nov 26, 2023
1 parent cb06cba commit 4754554
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 175 deletions.
51 changes: 25 additions & 26 deletions desktop/src/cn/harryh/arkpets/guitasks/FetchGitHubRemoteTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@

import cn.harryh.arkpets.utils.Logger;
import cn.harryh.arkpets.utils.NetUtils;
import cn.harryh.arkpets.utils.NetUtils.GitHubSource;
import cn.harryh.arkpets.utils.PopupUtils;
import javafx.concurrent.Task;
import javafx.scene.layout.StackPane;

import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.*;
import java.net.URL;
import java.nio.file.Files;

Expand All @@ -25,6 +24,7 @@ abstract public class FetchGitHubRemoteTask extends GuiTask {
protected final String destPath;
protected final boolean isArchive;
protected final boolean isHttpsTrustAll;
protected GitHubSource selectedSource;

public FetchGitHubRemoteTask(StackPane root, GuiTaskStyle style, String remotePathSuffix, String destPath, boolean isHttpsTrustAll, boolean isArchive) {
super(root, style);
Expand All @@ -41,32 +41,38 @@ protected Task<Boolean> getTask() {
protected Boolean call() throws Exception {
this.updateMessage("正在选择最佳线路");
Logger.info("Network", "Testing real delay");
NetUtils.GitHubSource[] sources = NetUtils.GitHubSource.sortByOverallAvailability(NetUtils.ghSources);
NetUtils.GitHubSource source = sources[0];
GitHubSource.sortByOverallAvailability(NetUtils.ghSources);
selectedSource = (GitHubSource)NetUtils.ghSources.get(0);

Logger.info("Network", "Selected the most available source \"" + source.tag + "\" (" + source.delay + "ms)");
String remotePath = (isArchive ? source.archivePreUrl : source.rawPreUrl) + remotePathSuffix;
Logger.info("Network", "Selected the most available " + selectedSource);
String remotePathPrefix = isArchive ? selectedSource.archivePreUrl : selectedSource.rawPreUrl;
String remotePath = remotePathPrefix + remotePathSuffix;

Logger.info("Network", "Fetching " + remotePath + " to " + destPath);
this.updateMessage("正在尝试与 " + source.tag + " 建立连接");
this.updateMessage("正在尝试与 " + selectedSource.tag + " 建立连接");

BufferedInputStream bis = null;
BufferedOutputStream bos = null;
File file = new File(destPath);
URL urlFile = new URL(remotePath);
HttpsURLConnection connection = NetUtils.ConnectionUtil.createHttpsConnection(urlFile, httpTimeoutDefault, httpTimeoutDefault, isHttpsTrustAll);
NetUtils.BufferLog log = new NetUtils.BufferLog(httpBufferSizeDefault);
HttpsURLConnection connection = NetUtils.ConnectionUtil.createHttpsConnection(new URL(remotePath),
httpTimeoutDefault,
httpTimeoutDefault,
isHttpsTrustAll);
final InputStream is = connection.getInputStream();
final OutputStream os = Files.newOutputStream(new File(destPath).toPath());
final BufferedInputStream bis = new BufferedInputStream(is, httpBufferSizeDefault);
final BufferedOutputStream bos = new BufferedOutputStream(os, httpBufferSizeDefault);

try {
bis = new BufferedInputStream(connection.getInputStream(), httpBufferSizeDefault);
bos = new BufferedOutputStream(Files.newOutputStream(file.toPath()), httpBufferSizeDefault);
try (bis; bos; is; os) {
int len = httpBufferSizeDefault;
long sum = 0;
long max = connection.getContentLengthLong();
byte[] bytes = new byte[len];
while ((len = bis.read(bytes)) != -1) {
bos.write(bytes, 0, len);
sum += len;
this.updateMessage("当前已下载:" + NetUtils.getFormattedSizeString(sum));
log.receive();
long speed = log.getSpeedPerSecond(500);
this.updateMessage("当前已下载:" + NetUtils.getFormattedSizeString(sum) +
(speed != 0 ? " (" + NetUtils.getFormattedSizeString(speed) + "/s)" : ""));
this.updateProgress(sum, max);
if (this.isCancelled()) {
this.updateMessage("下载进程已被取消");
Expand All @@ -76,15 +82,6 @@ protected Boolean call() throws Exception {
this.updateProgress(max, max);
bos.flush();
Logger.info("Network", "Fetched to " + destPath + " , size: " + sum);
} finally {
try {
connection.getInputStream().close();
if (bis != null)
bis.close();
if (bos != null)
bos.close();
} catch (Exception ignored) {
}
}
return this.isDone() && !this.isCancelled();
}
Expand All @@ -95,5 +92,7 @@ protected Boolean call() throws Exception {
protected void onFailed(Throwable e) {
if (style != GuiTaskStyle.HIDDEN)
PopupUtils.DialogUtil.createErrorDialog(root, e).show();
if (selectedSource != null)
selectedSource.receiveError();
}
}
36 changes: 15 additions & 21 deletions desktop/src/cn/harryh/arkpets/guitasks/FetchRemoteTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
import javafx.scene.layout.StackPane;

import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.*;
import java.net.URL;
import java.nio.file.Files;

Expand Down Expand Up @@ -40,23 +38,28 @@ protected Boolean call() throws Exception {
Logger.info("Network", "Fetching " + remotePath + " to " + destPath);
this.updateMessage("正在尝试建立连接");

BufferedInputStream bis = null;
BufferedOutputStream bos = null;
File file = new File(destPath);
URL urlFile = new URL(remotePath);
HttpsURLConnection connection = NetUtils.ConnectionUtil.createHttpsConnection(urlFile, httpTimeoutDefault, httpTimeoutDefault, isHttpsTrustAll);
NetUtils.BufferLog log = new NetUtils.BufferLog(httpBufferSizeDefault);
HttpsURLConnection connection = NetUtils.ConnectionUtil.createHttpsConnection(new URL(remotePath),
httpTimeoutDefault,
httpTimeoutDefault,
isHttpsTrustAll);
final InputStream is = connection.getInputStream();
final OutputStream os = Files.newOutputStream(new File(destPath).toPath());
final BufferedInputStream bis = new BufferedInputStream(is, httpBufferSizeDefault);
final BufferedOutputStream bos = new BufferedOutputStream(os, httpBufferSizeDefault);

try {
bis = new BufferedInputStream(connection.getInputStream(), httpBufferSizeDefault);
bos = new BufferedOutputStream(Files.newOutputStream(file.toPath()), httpBufferSizeDefault);
try (bis; bos; is; os) {
int len = httpBufferSizeDefault;
long sum = 0;
long max = connection.getContentLengthLong();
byte[] bytes = new byte[len];
while ((len = bis.read(bytes)) != -1) {
bos.write(bytes, 0, len);
sum += len;
this.updateMessage("当前已下载:" + NetUtils.getFormattedSizeString(sum));
log.receive();
long speed = log.getSpeedPerSecond(500);
this.updateMessage("当前已下载:" + NetUtils.getFormattedSizeString(sum) +
(speed != 0 ? " (" + NetUtils.getFormattedSizeString(speed) + "/s)" : ""));
this.updateProgress(sum, max);
if (this.isCancelled()) {
this.updateMessage("下载进程已被取消");
Expand All @@ -66,15 +69,6 @@ protected Boolean call() throws Exception {
this.updateProgress(max, max);
bos.flush();
Logger.info("Network", "Fetched to " + destPath + " , size: " + sum);
} finally {
try {
connection.getInputStream().close();
if (bis != null)
bis.close();
if (bos != null)
bos.close();
} catch (Exception ignored){
}
}
return this.isDone() && !this.isCancelled();
}
Expand Down
3 changes: 2 additions & 1 deletion desktop/src/cn/harryh/arkpets/guitasks/GuiTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ private JFXDialog getDialog(StackPane root, Task<Boolean> boundTask, boolean can

@Override
public String toString() {
return getClass().getSimpleName();
String name = getClass().getSimpleName();
return name.isEmpty() ? getClass().getSuperclass().getSimpleName() : name;
}
}
Loading

0 comments on commit 4754554

Please sign in to comment.