Clearly specify the in all places

This commit is contained in:
Glavo 2021-10-14 22:57:37 +08:00 committed by Yuhui Huang
parent 1aefb26575
commit 284ffb7aa0
11 changed files with 53 additions and 23 deletions

View File

@ -27,6 +27,7 @@ import java.io.*;
import java.net.ConnectException; import java.net.ConnectException;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.Socket; import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.TimerTask; import java.util.TimerTask;
import java.util.logging.Level; import java.util.logging.Level;
@ -83,8 +84,8 @@ public class MultiplayerClient extends Thread {
for (int i = 0; i < 5; i++) { for (int i = 0; i < 5; i++) {
KeepAliveThread keepAliveThread = null; KeepAliveThread keepAliveThread = null;
try (Socket socket = new Socket(InetAddress.getLoopbackAddress(), port); try (Socket socket = new Socket(InetAddress.getLoopbackAddress(), port);
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()))) { BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), StandardCharsets.UTF_8))) {
MultiplayerServer.Endpoint endpoint = new MultiplayerServer.Endpoint(socket, writer); MultiplayerServer.Endpoint endpoint = new MultiplayerServer.Endpoint(socket, writer);
LOG.info("Connected to 127.0.0.1:" + port); LOG.info("Connected to 127.0.0.1:" + port);

View File

@ -118,7 +118,7 @@ public final class MultiplayerManager {
CompletableFuture<CatoSession> future = new CompletableFuture<>(); 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(() -> { session.onExit().register(() -> {
try { try {

View File

@ -27,6 +27,7 @@ import org.jackhuang.hmcl.util.gson.JsonUtils;
import java.io.*; import java.io.*;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level; import java.util.logging.Level;
@ -129,8 +130,8 @@ public class MultiplayerServer extends Thread {
String clientName = null; String clientName = null;
LOG.info("Accepted client " + address); LOG.info("Accepted client " + address);
try (Socket clientSocket = targetSocket; try (Socket clientSocket = targetSocket;
BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(), StandardCharsets.UTF_8));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()))) { BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream(), StandardCharsets.UTF_8))) {
clientSocket.setKeepAlive(true); clientSocket.setKeepAlive(true);
Endpoint endpoint = new Endpoint(clientSocket, writer); Endpoint endpoint = new Endpoint(clientSocket, writer);
clients.put(address, endpoint); clients.put(address, endpoint);

View File

@ -447,7 +447,7 @@ public class DefaultLauncher extends Launcher {
if (!FileUtils.makeFile(scriptFile)) if (!FileUtils.makeFile(scriptFile))
throw new IOException("Script file: " + scriptFile + " cannot be created."); 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) { if (isWindows) {
writer.write("@echo off"); writer.write("@echo off");
writer.newLine(); writer.newLine();
@ -495,12 +495,12 @@ public class DefaultLauncher extends Launcher {
Thread stdout = Lang.thread(new StreamPump(managedProcess.getProcess().getInputStream(), it -> { Thread stdout = Lang.thread(new StreamPump(managedProcess.getProcess().getInputStream(), it -> {
processListener.onLog(it, Optional.ofNullable(Log4jLevel.guessLevel(it)).orElse(Log4jLevel.INFO)); processListener.onLog(it, Optional.ofNullable(Log4jLevel.guessLevel(it)).orElse(Log4jLevel.INFO));
managedProcess.addLine(it); managedProcess.addLine(it);
}), "stdout-pump", isDaemon); }, OperatingSystem.NATIVE_CHARSET), "stdout-pump", isDaemon);
managedProcess.addRelatedThread(stdout); managedProcess.addRelatedThread(stdout);
Thread stderr = Lang.thread(new StreamPump(managedProcess.getProcess().getErrorStream(), it -> { Thread stderr = Lang.thread(new StreamPump(managedProcess.getProcess().getErrorStream(), it -> {
processListener.onLog(it, Log4jLevel.ERROR); processListener.onLog(it, Log4jLevel.ERROR);
managedProcess.addLine(it); managedProcess.addLine(it);
}), "stderr-pump", isDaemon); }, OperatingSystem.NATIVE_CHARSET), "stderr-pump", isDaemon);
managedProcess.addRelatedThread(stderr); managedProcess.addRelatedThread(stderr);
managedProcess.addRelatedThread(Lang.thread(new ExitWaiter(managedProcess, Arrays.asList(stdout, stderr), processListener::onExit), "exit-waiter", isDaemon)); managedProcess.addRelatedThread(Lang.thread(new ExitWaiter(managedProcess, Arrays.asList(stdout, stderr), processListener::onExit), "exit-waiter", isDaemon));
} }

View File

@ -24,6 +24,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.logging.Level; import java.util.logging.Level;
@ -36,20 +37,27 @@ public final class StreamPump implements Runnable {
private final InputStream inputStream; private final InputStream inputStream;
private final Consumer<String> callback; private final Consumer<String> callback;
private final Charset charset;
public StreamPump(InputStream inputStream) { public StreamPump(InputStream inputStream) {
this(inputStream, s -> { this(inputStream, s -> {});
});
} }
public StreamPump(InputStream inputStream, Consumer<String> callback) { public StreamPump(InputStream inputStream, Consumer<String> callback) {
this.inputStream = inputStream; this.inputStream = inputStream;
this.callback = callback; 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 @Override
public void run() { public void run() {
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, Charset.defaultCharset()))) { try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, charset))) {
String line; String line;
while ((line = bufferedReader.readLine()) != null) { while ((line = bufferedReader.readLine()) != null) {
if (Thread.currentThread().isInterrupted()) { if (Thread.currentThread().isInterrupted()) {

View File

@ -21,6 +21,7 @@ import org.jackhuang.hmcl.util.platform.OperatingSystem;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.PrintStream; import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.*; import java.util.*;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -35,8 +36,12 @@ public final class StringUtils {
public static String getStackTrace(Throwable throwable) { public static String getStackTrace(Throwable throwable) {
ByteArrayOutputStream stream = new ByteArrayOutputStream(); ByteArrayOutputStream stream = new ByteArrayOutputStream();
throwable.printStackTrace(new PrintStream(stream)); try {
return stream.toString(); 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) { public static String getStackTrace(StackTraceElement[] elements) {

View File

@ -17,6 +17,7 @@
*/ */
package org.jackhuang.hmcl.util.io; package org.jackhuang.hmcl.util.io;
import org.jackhuang.hmcl.util.platform.OperatingSystem;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.io.File; import java.io.File;
@ -89,7 +90,8 @@ public final class CompressingUtils {
public static Charset findSuitableEncoding(Path zipFile, Collection<Charset> candidates) throws IOException { public static Charset findSuitableEncoding(Path zipFile, Collection<Charset> candidates) throws IOException {
if (testEncoding(zipFile, StandardCharsets.UTF_8)) return StandardCharsets.UTF_8; 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) for (Charset charset : candidates)
if (charset != null && testEncoding(zipFile, charset)) if (charset != null && testEncoding(zipFile, charset))

View File

@ -65,7 +65,7 @@ public final class IOUtils {
} }
public static String readFullyAsString(InputStream stream) throws IOException { 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 { public static String readFullyAsString(InputStream stream, Charset charset) throws IOException {

View File

@ -196,7 +196,7 @@ public enum Architecture {
try { try {
Process process = Runtime.getRuntime().exec("/usr/bin/arch"); Process process = Runtime.getRuntime().exec("/usr/bin/arch");
if (process.waitFor(3, TimeUnit.SECONDS)) { 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(); sysArchName = reader.readLine().trim();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -137,7 +137,7 @@ public final class JavaVersion {
Platform platform = null; Platform platform = null;
Process process = new ProcessBuilder(executable.toString(), "-XshowSettings:properties", "-version").start(); 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; ) { for (String line; (line = reader.readLine()) != null; ) {
Matcher m; Matcher m;
@ -171,7 +171,7 @@ public final class JavaVersion {
if (version == null) { if (version == null) {
boolean is64Bit = false; boolean is64Bit = false;
process = new ProcessBuilder(executable.toString(), "-version").start(); 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; ) { for (String line; (line = reader.readLine()) != null; ) {
Matcher m = REGEX.matcher(line); Matcher m = REGEX.matcher(line);
if (m.find()) if (m.find())
@ -413,7 +413,7 @@ public final class JavaVersion {
List<String> res = new ArrayList<>(); List<String> res = new ArrayList<>();
Process process = Runtime.getRuntime().exec(new String[] { "cmd", "/c", "reg", "query", location }); 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;) { for (String line; (line = reader.readLine()) != null;) {
if (line.startsWith(location) && !line.equals(location)) { if (line.startsWith(location) && !line.equals(location)) {
res.add(line); res.add(line);
@ -427,7 +427,7 @@ public final class JavaVersion {
boolean last = false; boolean last = false;
Process process = Runtime.getRuntime().exec(new String[] { "cmd", "/c", "reg", "query", location, "/v", name }); 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;) { for (String line; (line = reader.readLine()) != null;) {
if (StringUtils.isNotBlank(line)) { if (StringUtils.isNotBlank(line)) {
if (last && line.trim().startsWith(name)) { if (last && line.trim().startsWith(name)) {

View File

@ -22,6 +22,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
@ -84,9 +85,9 @@ public enum OperatingSystem {
public static final String LINE_SEPARATOR = System.lineSeparator(); 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. * Windows system build number.
@ -111,13 +112,25 @@ public enum OperatingSystem {
private static final Pattern MEMINFO_PATTERN = Pattern.compile("^(?<key>.*?):\\s+(?<value>\\d+) kB?$"); private static final Pattern MEMINFO_PATTERN = Pattern.compile("^(?<key>.*?):\\s+(?<value>\\d+) kB?$");
static { 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) { if (CURRENT_OS == WINDOWS) {
String versionNumber = null; String versionNumber = null;
int buildNumber = -1; int buildNumber = -1;
try { try {
Process process = Runtime.getRuntime().exec(new String[]{"cmd", "ver"}); 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 matcher = Pattern.compile("(?<version>[0-9]+\\.[0-9]+\\.(?<build>[0-9]+)(\\.[0-9]+)?)]$")
.matcher(reader.readLine().trim()); .matcher(reader.readLine().trim());