[ANY] Мини-рефакторинг.

This commit is contained in:
Zaxar163 2019-10-03 14:57:53 +02:00
parent e6333e057f
commit 78e882b943
No known key found for this signature in database
GPG key ID: 1FE4F2E1F053831B
7 changed files with 23 additions and 33 deletions

View file

@ -8,7 +8,6 @@
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor; import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import java.util.Arrays;
import java.util.Base64; import java.util.Base64;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;

View file

@ -4,10 +4,8 @@
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Collections; import java.util.Collections;
import pro.gravit.launcher.profiles.ClientProfile.Version;
import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.command.Command; import pro.gravit.launchserver.command.Command;
import pro.gravit.utils.HttpDownloader;
import pro.gravit.utils.helper.IOHelper; import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.LogHelper; import pro.gravit.utils.helper.LogHelper;

View file

@ -10,10 +10,8 @@
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import pro.gravit.launcher.Launcher; import pro.gravit.launcher.Launcher;
import pro.gravit.launcher.profiles.ClientProfile; import pro.gravit.launcher.profiles.ClientProfile;
import pro.gravit.launcher.profiles.ClientProfile.Version;
import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.command.Command; import pro.gravit.launchserver.command.Command;
import pro.gravit.utils.HttpDownloader;
import pro.gravit.utils.command.CommandException; import pro.gravit.utils.command.CommandException;
import pro.gravit.utils.helper.IOHelper; import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.LogHelper; import pro.gravit.utils.helper.LogHelper;

View file

@ -8,7 +8,6 @@
import pro.gravit.launchserver.socket.Client; import pro.gravit.launchserver.socket.Client;
import pro.gravit.launchserver.socket.response.SimpleResponse; import pro.gravit.launchserver.socket.response.SimpleResponse;
import pro.gravit.utils.Version; import pro.gravit.utils.Version;
import pro.gravit.utils.helper.LogHelper;
import pro.gravit.utils.helper.SecurityHelper; import pro.gravit.utils.helper.SecurityHelper;
public class LauncherResponse extends SimpleResponse { public class LauncherResponse extends SimpleResponse {

View file

@ -6,9 +6,6 @@
import java.nio.file.Path; import java.nio.file.Path;
import java.util.jar.JarFile; import java.util.jar.JarFile;
import cpw.mods.fml.SafeExitJVMLegacy;
import net.minecraftforge.fml.SafeExitJVM;
import pro.gravit.launcher.utils.NativeJVMHalt;
import pro.gravit.utils.helper.LogHelper; import pro.gravit.utils.helper.LogHelper;
@LauncherAPI @LauncherAPI

View file

@ -6,7 +6,6 @@
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;

View file

@ -1,59 +1,59 @@
package pro.gravit.utils; package pro.gravit.utils;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream;
import java.net.URL; import java.net.URL;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Observable;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream; import java.util.zip.ZipInputStream;
import pro.gravit.utils.helper.IOHelper; import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.LogHelper; import pro.gravit.utils.helper.LogHelper;
public final class HttpDownloader extends Observable { public final class HttpDownloader {
public static final int BUFER_SIZE = 8192; public static final int INTERVAL = 500;
public static final int INTERVAL = 300; public final AtomicInteger writed;
public AtomicInteger writed = new AtomicInteger(0); private volatile String filename;
private String filename; public final Thread thread;
public Thread thread;
public HttpDownloader(URL url, String file) { public HttpDownloader(URL url, Path file) {
Runnable run = () -> { writed = new AtomicInteger(0);
filename = null;
thread = new Thread(() -> {
try { try {
filename = file; filename = IOHelper.getFileName(file);
downloadFile(url, file); downloadFile(url, file, writed::set);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
}; });
Thread downloader = new Thread(run); thread.setDaemon(true);
thread = downloader; thread.start();
downloader.start();
} }
public synchronized String getFilename() { public String getFilename() {
return filename; return filename;
} }
public void downloadFile(URL url, String file) throws IOException { public static void downloadFile(URL url, Path file, Consumer<Integer> chanheTrack) throws IOException {
try (BufferedInputStream in = new BufferedInputStream(url.openStream()); FileOutputStream fout = new FileOutputStream(file)) { try (BufferedInputStream in = new BufferedInputStream(url.openStream()); OutputStream fout = IOHelper.newOutput(file, false)) {
final byte[] data = new byte[BUFER_SIZE]; final byte[] data = new byte[IOHelper.BUFFER_SIZE];
int count; int count;
long timestamp = System.currentTimeMillis(); long timestamp = System.currentTimeMillis();
int writed_local = 0; int writed_local = 0;
while ((count = in.read(data, 0, BUFER_SIZE)) != -1) { while ((count = in.read(data, 0, IOHelper.BUFFER_SIZE)) != -1) {
fout.write(data, 0, count); fout.write(data, 0, count);
writed_local += count; writed_local += count;
if (System.currentTimeMillis() - timestamp > INTERVAL) { if (System.currentTimeMillis() - timestamp > INTERVAL) {
writed.set(writed_local); chanheTrack.accept(writed_local);
LogHelper.debug("Downloaded %d", writed_local); LogHelper.debug("Downloaded %d", writed_local);
} }
} }
writed.set(writed_local); chanheTrack.accept(writed_local);
} }
} }