[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.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Arrays;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

View file

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

View file

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

View file

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

View file

@ -6,9 +6,6 @@
import java.nio.file.Path;
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;
@LauncherAPI

View file

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

View file

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