mirror of
https://github.com/HMCL-dev/HMCL.git
synced 2025-02-11 16:59:54 +08:00
Clearly specify the in all places
This commit is contained in:
parent
1aefb26575
commit
284ffb7aa0
@ -27,6 +27,7 @@ import java.io.*;
|
||||
import java.net.ConnectException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.Socket;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.TimerTask;
|
||||
import java.util.logging.Level;
|
||||
|
||||
@ -83,8 +84,8 @@ public class MultiplayerClient extends Thread {
|
||||
for (int i = 0; i < 5; i++) {
|
||||
KeepAliveThread keepAliveThread = null;
|
||||
try (Socket socket = new Socket(InetAddress.getLoopbackAddress(), port);
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
||||
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()))) {
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8));
|
||||
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), StandardCharsets.UTF_8))) {
|
||||
MultiplayerServer.Endpoint endpoint = new MultiplayerServer.Endpoint(socket, writer);
|
||||
LOG.info("Connected to 127.0.0.1:" + port);
|
||||
|
||||
|
@ -118,7 +118,7 @@ public final class MultiplayerManager {
|
||||
|
||||
CompletableFuture<CatoSession> future = new CompletableFuture<>();
|
||||
|
||||
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
|
||||
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream(), StandardCharsets.UTF_8));
|
||||
|
||||
session.onExit().register(() -> {
|
||||
try {
|
||||
|
@ -27,6 +27,7 @@ import org.jackhuang.hmcl.util.gson.JsonUtils;
|
||||
import java.io.*;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.logging.Level;
|
||||
@ -129,8 +130,8 @@ public class MultiplayerServer extends Thread {
|
||||
String clientName = null;
|
||||
LOG.info("Accepted client " + address);
|
||||
try (Socket clientSocket = targetSocket;
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
|
||||
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()))) {
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(), StandardCharsets.UTF_8));
|
||||
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream(), StandardCharsets.UTF_8))) {
|
||||
clientSocket.setKeepAlive(true);
|
||||
Endpoint endpoint = new Endpoint(clientSocket, writer);
|
||||
clients.put(address, endpoint);
|
||||
|
@ -447,7 +447,7 @@ public class DefaultLauncher extends Launcher {
|
||||
|
||||
if (!FileUtils.makeFile(scriptFile))
|
||||
throw new IOException("Script file: " + scriptFile + " cannot be created.");
|
||||
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(scriptFile)))) {
|
||||
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(scriptFile), OperatingSystem.NATIVE_CHARSET))) {
|
||||
if (isWindows) {
|
||||
writer.write("@echo off");
|
||||
writer.newLine();
|
||||
@ -495,12 +495,12 @@ public class DefaultLauncher extends Launcher {
|
||||
Thread stdout = Lang.thread(new StreamPump(managedProcess.getProcess().getInputStream(), it -> {
|
||||
processListener.onLog(it, Optional.ofNullable(Log4jLevel.guessLevel(it)).orElse(Log4jLevel.INFO));
|
||||
managedProcess.addLine(it);
|
||||
}), "stdout-pump", isDaemon);
|
||||
}, OperatingSystem.NATIVE_CHARSET), "stdout-pump", isDaemon);
|
||||
managedProcess.addRelatedThread(stdout);
|
||||
Thread stderr = Lang.thread(new StreamPump(managedProcess.getProcess().getErrorStream(), it -> {
|
||||
processListener.onLog(it, Log4jLevel.ERROR);
|
||||
managedProcess.addLine(it);
|
||||
}), "stderr-pump", isDaemon);
|
||||
}, OperatingSystem.NATIVE_CHARSET), "stderr-pump", isDaemon);
|
||||
managedProcess.addRelatedThread(stderr);
|
||||
managedProcess.addRelatedThread(Lang.thread(new ExitWaiter(managedProcess, Arrays.asList(stdout, stderr), processListener::onExit), "exit-waiter", isDaemon));
|
||||
}
|
||||
|
@ -24,6 +24,7 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.logging.Level;
|
||||
|
||||
@ -36,20 +37,27 @@ public final class StreamPump implements Runnable {
|
||||
|
||||
private final InputStream inputStream;
|
||||
private final Consumer<String> callback;
|
||||
private final Charset charset;
|
||||
|
||||
public StreamPump(InputStream inputStream) {
|
||||
this(inputStream, s -> {
|
||||
});
|
||||
this(inputStream, s -> {});
|
||||
}
|
||||
|
||||
public StreamPump(InputStream inputStream, Consumer<String> callback) {
|
||||
this.inputStream = inputStream;
|
||||
this.callback = callback;
|
||||
this.charset = StandardCharsets.UTF_8;
|
||||
}
|
||||
|
||||
public StreamPump(InputStream inputStream, Consumer<String> callback, Charset charset) {
|
||||
this.inputStream = inputStream;
|
||||
this.callback = callback;
|
||||
this.charset = charset;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, Charset.defaultCharset()))) {
|
||||
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, charset))) {
|
||||
String line;
|
||||
while ((line = bufferedReader.readLine()) != null) {
|
||||
if (Thread.currentThread().isInterrupted()) {
|
||||
|
@ -21,6 +21,7 @@ import org.jackhuang.hmcl.util.platform.OperatingSystem;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@ -35,8 +36,12 @@ public final class StringUtils {
|
||||
|
||||
public static String getStackTrace(Throwable throwable) {
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
throwable.printStackTrace(new PrintStream(stream));
|
||||
return stream.toString();
|
||||
try {
|
||||
throwable.printStackTrace(new PrintStream(stream, false, "UTF-8"));
|
||||
return stream.toString("UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new InternalError(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static String getStackTrace(StackTraceElement[] elements) {
|
||||
|
@ -17,6 +17,7 @@
|
||||
*/
|
||||
package org.jackhuang.hmcl.util.io;
|
||||
|
||||
import org.jackhuang.hmcl.util.platform.OperatingSystem;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
@ -89,7 +90,8 @@ public final class CompressingUtils {
|
||||
|
||||
public static Charset findSuitableEncoding(Path zipFile, Collection<Charset> candidates) throws IOException {
|
||||
if (testEncoding(zipFile, StandardCharsets.UTF_8)) return StandardCharsets.UTF_8;
|
||||
if (testEncoding(zipFile, Charset.defaultCharset())) return Charset.defaultCharset();
|
||||
if (OperatingSystem.NATIVE_CHARSET != StandardCharsets.UTF_8 && testEncoding(zipFile, OperatingSystem.NATIVE_CHARSET))
|
||||
return OperatingSystem.NATIVE_CHARSET;
|
||||
|
||||
for (Charset charset : candidates)
|
||||
if (charset != null && testEncoding(zipFile, charset))
|
||||
|
@ -65,7 +65,7 @@ public final class IOUtils {
|
||||
}
|
||||
|
||||
public static String readFullyAsString(InputStream stream) throws IOException {
|
||||
return readFully(stream).toString();
|
||||
return readFully(stream).toString("UTF-8");
|
||||
}
|
||||
|
||||
public static String readFullyAsString(InputStream stream, Charset charset) throws IOException {
|
||||
|
@ -196,7 +196,7 @@ public enum Architecture {
|
||||
try {
|
||||
Process process = Runtime.getRuntime().exec("/usr/bin/arch");
|
||||
if (process.waitFor(3, TimeUnit.SECONDS)) {
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), OperatingSystem.NATIVE_CHARSET))) {
|
||||
sysArchName = reader.readLine().trim();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
@ -137,7 +137,7 @@ public final class JavaVersion {
|
||||
Platform platform = null;
|
||||
|
||||
Process process = new ProcessBuilder(executable.toString(), "-XshowSettings:properties", "-version").start();
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream(), OperatingSystem.NATIVE_CHARSET))) {
|
||||
for (String line; (line = reader.readLine()) != null; ) {
|
||||
Matcher m;
|
||||
|
||||
@ -171,7 +171,7 @@ public final class JavaVersion {
|
||||
if (version == null) {
|
||||
boolean is64Bit = false;
|
||||
process = new ProcessBuilder(executable.toString(), "-version").start();
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream(), OperatingSystem.NATIVE_CHARSET))) {
|
||||
for (String line; (line = reader.readLine()) != null; ) {
|
||||
Matcher m = REGEX.matcher(line);
|
||||
if (m.find())
|
||||
@ -413,7 +413,7 @@ public final class JavaVersion {
|
||||
List<String> res = new ArrayList<>();
|
||||
|
||||
Process process = Runtime.getRuntime().exec(new String[] { "cmd", "/c", "reg", "query", location });
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), OperatingSystem.NATIVE_CHARSET))) {
|
||||
for (String line; (line = reader.readLine()) != null;) {
|
||||
if (line.startsWith(location) && !line.equals(location)) {
|
||||
res.add(line);
|
||||
@ -427,7 +427,7 @@ public final class JavaVersion {
|
||||
boolean last = false;
|
||||
Process process = Runtime.getRuntime().exec(new String[] { "cmd", "/c", "reg", "query", location, "/v", name });
|
||||
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), OperatingSystem.NATIVE_CHARSET))) {
|
||||
for (String line; (line = reader.readLine()) != null;) {
|
||||
if (StringUtils.isNotBlank(line)) {
|
||||
if (last && line.trim().startsWith(name)) {
|
||||
|
@ -22,6 +22,7 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.UnsupportedCharsetException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
@ -84,9 +85,9 @@ public enum OperatingSystem {
|
||||
public static final String LINE_SEPARATOR = System.lineSeparator();
|
||||
|
||||
/**
|
||||
* The system default encoding.
|
||||
* The system default charset.
|
||||
*/
|
||||
public static final String ENCODING = System.getProperty("sun.jnu.encoding", Charset.defaultCharset().name());
|
||||
public static final Charset NATIVE_CHARSET;
|
||||
|
||||
/**
|
||||
* Windows system build number.
|
||||
@ -111,13 +112,25 @@ public enum OperatingSystem {
|
||||
private static final Pattern MEMINFO_PATTERN = Pattern.compile("^(?<key>.*?):\\s+(?<value>\\d+) kB?$");
|
||||
|
||||
static {
|
||||
String nativeEncoding = System.getProperty("native.encoding", System.getProperty("sun.jnu.encoding"));
|
||||
Charset nativeCharset = Charset.defaultCharset();
|
||||
|
||||
if (nativeEncoding != null) {
|
||||
try {
|
||||
nativeCharset = Charset.forName(nativeEncoding);
|
||||
} catch (UnsupportedCharsetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
NATIVE_CHARSET = nativeCharset;
|
||||
|
||||
if (CURRENT_OS == WINDOWS) {
|
||||
String versionNumber = null;
|
||||
int buildNumber = -1;
|
||||
|
||||
try {
|
||||
Process process = Runtime.getRuntime().exec(new String[]{"cmd", "ver"});
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), NATIVE_CHARSET))) {
|
||||
Matcher matcher = Pattern.compile("(?<version>[0-9]+\\.[0-9]+\\.(?<build>[0-9]+)(\\.[0-9]+)?)]$")
|
||||
.matcher(reader.readLine().trim());
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user