移除Charsets类

This commit is contained in:
yushijinhun 2018-06-07 11:47:36 +08:00
parent 17615b1791
commit a08be2e11d
No known key found for this signature in database
GPG Key ID: 5BC167F73EA558E4
11 changed files with 40 additions and 84 deletions

View File

@ -24,7 +24,6 @@ import org.jackhuang.hmcl.task.FileDownloadTask;
import org.jackhuang.hmcl.task.Task; import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.task.TaskExecutor; import org.jackhuang.hmcl.task.TaskExecutor;
import org.jackhuang.hmcl.ui.Controllers; import org.jackhuang.hmcl.ui.Controllers;
import org.jackhuang.hmcl.util.Charsets;
import org.jackhuang.hmcl.util.Logging; import org.jackhuang.hmcl.util.Logging;
import org.jackhuang.hmcl.util.VersionNumber; import org.jackhuang.hmcl.util.VersionNumber;
@ -35,6 +34,8 @@ import java.util.List;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level; import java.util.logging.Level;
import static java.nio.charset.StandardCharsets.UTF_8;
/** /**
* *
* @author huangyuhui * @author huangyuhui
@ -79,7 +80,7 @@ public class NewFileUpgrader extends IUpgrader {
File file = new File(realPath); File file = new File(realPath);
realPath = file.getAbsolutePath(); realPath = file.getAbsolutePath();
try { try {
realPath = java.net.URLDecoder.decode(realPath, Charsets.DEFAULT_CHARSET.name()); realPath = java.net.URLDecoder.decode(realPath, UTF_8.name());
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -25,7 +25,6 @@ import org.jackhuang.hmcl.auth.yggdrasil.YggdrasilService;
import org.jackhuang.hmcl.auth.yggdrasil.YggdrasilSession; import org.jackhuang.hmcl.auth.yggdrasil.YggdrasilSession;
import org.jackhuang.hmcl.game.Arguments; import org.jackhuang.hmcl.game.Arguments;
import org.jackhuang.hmcl.task.GetTask; import org.jackhuang.hmcl.task.GetTask;
import org.jackhuang.hmcl.util.Charsets;
import org.jackhuang.hmcl.util.ExceptionalSupplier; import org.jackhuang.hmcl.util.ExceptionalSupplier;
import org.jackhuang.hmcl.util.Lang; import org.jackhuang.hmcl.util.Lang;
import org.jackhuang.hmcl.util.NetworkUtils; import org.jackhuang.hmcl.util.NetworkUtils;
@ -34,6 +33,8 @@ import java.util.Base64;
import java.util.Map; import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import static java.nio.charset.StandardCharsets.UTF_8;
public class AuthlibInjectorAccount extends YggdrasilAccount { public class AuthlibInjectorAccount extends YggdrasilAccount {
private final String serverBaseURL; private final String serverBaseURL;
private final ExceptionalSupplier<String, ?> injectorJarPath; private final ExceptionalSupplier<String, ?> injectorJarPath;
@ -69,7 +70,7 @@ public class AuthlibInjectorAccount extends YggdrasilAccount {
Arguments arguments = Arguments.addJVMArguments(null, arg); Arguments arguments = Arguments.addJVMArguments(null, arg);
if (flag.get()) if (flag.get())
arguments = Arguments.addJVMArguments(arguments, "-Dorg.to2mbn.authlibinjector.config.prefetched=" + new String(Base64.getEncoder().encode(getTask.getResult().getBytes()), Charsets.UTF_8)); arguments = Arguments.addJVMArguments(arguments, "-Dorg.to2mbn.authlibinjector.config.prefetched=" + new String(Base64.getEncoder().encode(getTask.getResult().getBytes()), UTF_8));
return info.setArguments(arguments); return info.setArguments(arguments);
} catch (Exception e) { } catch (Exception e) {

View File

@ -11,6 +11,8 @@ import java.net.Proxy;
import java.net.URL; import java.net.URL;
import java.util.*; import java.util.*;
import static java.nio.charset.StandardCharsets.UTF_8;
public class YggdrasilService { public class YggdrasilService {
private final YggdrasilProvider provider; private final YggdrasilProvider provider;
@ -99,7 +101,7 @@ public class YggdrasilService {
return Optional.ofNullable(profile.getProperties()) return Optional.ofNullable(profile.getProperties())
.map(properties -> properties.get("textures")) .map(properties -> properties.get("textures"))
.map(encodedTextures -> new String(Base64.getDecoder().decode(encodedTextures), Charsets.UTF_8)) .map(encodedTextures -> new String(Base64.getDecoder().decode(encodedTextures), UTF_8))
.map(Lang.liftFunction(textures -> fromJson(textures, TextureResponse.class))) .map(Lang.liftFunction(textures -> fromJson(textures, TextureResponse.class)))
.map(TextureResponse::getTextures); .map(TextureResponse::getTextures);
} }

View File

@ -19,13 +19,14 @@ package org.jackhuang.hmcl.game;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile; import org.apache.commons.compress.archivers.zip.ZipFile;
import org.jackhuang.hmcl.util.Charsets;
import org.jackhuang.hmcl.util.IOUtils; import org.jackhuang.hmcl.util.IOUtils;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Optional; import java.util.Optional;
import static java.nio.charset.StandardCharsets.US_ASCII;
/** /**
* @author huangyuhui * @author huangyuhui
*/ */
@ -55,7 +56,7 @@ public final class GameVersion {
private static Optional<String> getVersionOfOldMinecraft(ZipFile file, ZipArchiveEntry entry) throws IOException { private static Optional<String> getVersionOfOldMinecraft(ZipFile file, ZipArchiveEntry entry) throws IOException {
byte[] tmp = IOUtils.readFullyAsByteArray(file.getInputStream(entry)); byte[] tmp = IOUtils.readFullyAsByteArray(file.getInputStream(entry));
byte[] bytes = "Minecraft Minecraft ".getBytes(Charsets.US_ASCII); byte[] bytes = "Minecraft Minecraft ".getBytes(US_ASCII);
int j = matchArray(tmp, bytes); int j = matchArray(tmp, bytes);
if (j < 0) if (j < 0)
return Optional.empty(); return Optional.empty();
@ -64,25 +65,25 @@ public final class GameVersion {
if ((j = lessThan32(tmp, i)) < 0) if ((j = lessThan32(tmp, i)) < 0)
return Optional.empty(); return Optional.empty();
return Optional.of(new String(tmp, i, j - i, Charsets.US_ASCII)); return Optional.of(new String(tmp, i, j - i, US_ASCII));
} }
private static Optional<String> getVersionOfNewMinecraft(ZipFile file, ZipArchiveEntry entry) throws IOException { private static Optional<String> getVersionOfNewMinecraft(ZipFile file, ZipArchiveEntry entry) throws IOException {
byte[] tmp = IOUtils.readFullyAsByteArray(file.getInputStream(entry)); byte[] tmp = IOUtils.readFullyAsByteArray(file.getInputStream(entry));
byte[] str = "-server.txt".getBytes(Charsets.US_ASCII); byte[] str = "-server.txt".getBytes(US_ASCII);
int j = matchArray(tmp, str); int j = matchArray(tmp, str);
if (j < 0) return Optional.empty(); if (j < 0) return Optional.empty();
int i = j + str.length; int i = j + str.length;
i += 11; i += 11;
j = lessThan32(tmp, i); j = lessThan32(tmp, i);
if (j < 0) return Optional.empty(); if (j < 0) return Optional.empty();
String result = new String(tmp, i, j - i, Charsets.US_ASCII); String result = new String(tmp, i, j - i, US_ASCII);
char ch = result.charAt(0); char ch = result.charAt(0);
// 1.8.1+ // 1.8.1+
if (ch < '0' || ch > '9') { if (ch < '0' || ch > '9') {
str = "Can't keep up! Did the system time change, or is the server overloaded?".getBytes(Charsets.US_ASCII); str = "Can't keep up! Did the system time change, or is the server overloaded?".getBytes(US_ASCII);
j = matchArray(tmp, str); j = matchArray(tmp, str);
if (j < 0) return Optional.empty(); if (j < 0) return Optional.empty();
i = -1; i = -1;
@ -100,7 +101,7 @@ public final class GameVersion {
while (tmp[k] >= 48 && tmp[k] <= 57 || tmp[k] == (int) '-' || tmp[k] == (int) '.' || tmp[k] >= 97 && tmp[k] <= (int) 'z') while (tmp[k] >= 48 && tmp[k] <= 57 || tmp[k] == (int) '-' || tmp[k] == (int) '.' || tmp[k] >= 97 && tmp[k] <= (int) 'z')
k--; k--;
k++; k++;
return Optional.of(new String(tmp, k, i - k + 1, Charsets.US_ASCII)); return Optional.of(new String(tmp, k, i - k + 1, US_ASCII));
} }
return Optional.of(result); return Optional.of(result);
} }

View File

@ -17,7 +17,6 @@
*/ */
package org.jackhuang.hmcl.task; package org.jackhuang.hmcl.task;
import org.jackhuang.hmcl.util.Charsets;
import org.jackhuang.hmcl.util.IOUtils; import org.jackhuang.hmcl.util.IOUtils;
import org.jackhuang.hmcl.util.Logging; import org.jackhuang.hmcl.util.Logging;
import org.jackhuang.hmcl.util.NetworkUtils; import org.jackhuang.hmcl.util.NetworkUtils;
@ -31,6 +30,8 @@ import java.net.URL;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.logging.Level; import java.util.logging.Level;
import static java.nio.charset.StandardCharsets.UTF_8;
/** /**
* *
* @author huang * @author huang
@ -52,7 +53,7 @@ public final class GetTask extends TaskResult<String> {
} }
public GetTask(URL url, Proxy proxy, String id) { public GetTask(URL url, Proxy proxy, String id) {
this(url, proxy, id, Charsets.DEFAULT_CHARSET); this(url, proxy, id, UTF_8);
} }
public GetTask(URL url, Proxy proxy, String id, Charset charset) { public GetTask(URL url, Proxy proxy, String id, Charset charset) {

View File

@ -1,55 +0,0 @@
/*
* Hello Minecraft!.
* Copyright (C) 2018 huangyuhui <huanghongxun2008@126.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see {http://www.gnu.org/licenses/}.
*/
package org.jackhuang.hmcl.util;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
public final class Charsets {
private Charsets() {
}
public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
public static final Charset US_ASCII = Charset.forName("US-ASCII");
public static final Charset UTF_16 = Charset.forName("UTF-16");
public static final Charset UTF_16BE = Charset.forName("UTF-16BE");
public static final Charset UTF_16LE = Charset.forName("UTF-16LE");
public static final Charset UTF_8 = Charset.forName("UTF-8");
public static final Charset DEFAULT_CHARSET = UTF_8;
public static Charset toCharset(String charset) {
if (charset == null)
return Charset.defaultCharset();
try {
return Charset.forName(charset);
} catch (UnsupportedCharsetException ignored) {
return Charset.defaultCharset();
}
}
public static Charset toCharset() {
return toCharset(System.getProperty("sun.jnu.encoding", Constants.DEFAULT_ENCODING));
}
}

View File

@ -43,8 +43,6 @@ public final class Constants {
private Constants() { private Constants() {
} }
public static final String DEFAULT_ENCODING = "UTF-8";
public static final Charset DEFAULT_CHARSET = Charsets.UTF_8;
public static final Charset SYSTEM_CHARSET = Charset.forName(OperatingSystem.ENCODING); public static final Charset SYSTEM_CHARSET = Charset.forName(OperatingSystem.ENCODING);
public static final String DEFAULT_LIBRARY_URL = "https://libraries.minecraft.net/"; public static final String DEFAULT_LIBRARY_URL = "https://libraries.minecraft.net/";

View File

@ -22,6 +22,8 @@ import java.io.InputStream;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import static java.nio.charset.StandardCharsets.UTF_8;
/** /**
* *
* @author huangyuhui * @author huangyuhui
@ -80,7 +82,7 @@ public final class DigestUtils {
} }
public static byte[] md2(String data) { public static byte[] md2(String data) {
return md2(data.getBytes(Charsets.UTF_8)); return md2(data.getBytes(UTF_8));
} }
public static String md2Hex(byte[] data) { public static String md2Hex(byte[] data) {
@ -106,7 +108,7 @@ public final class DigestUtils {
} }
public static byte[] md5(String data) { public static byte[] md5(String data) {
return md5(data.getBytes(Charsets.UTF_8)); return md5(data.getBytes(UTF_8));
} }
public static String md5Hex(byte[] data) { public static String md5Hex(byte[] data) {
@ -132,7 +134,7 @@ public final class DigestUtils {
} }
public static byte[] sha1(String data) { public static byte[] sha1(String data) {
return sha1(data.getBytes(Charsets.UTF_8)); return sha1(data.getBytes(UTF_8));
} }
public static String sha1Hex(byte[] data) { public static String sha1Hex(byte[] data) {
@ -158,7 +160,7 @@ public final class DigestUtils {
} }
public static byte[] sha256(String data) { public static byte[] sha256(String data) {
return sha256(data.getBytes(Charsets.UTF_8)); return sha256(data.getBytes(UTF_8));
} }
public static String sha256Hex(byte[] data) { public static String sha256Hex(byte[] data) {
@ -184,7 +186,7 @@ public final class DigestUtils {
} }
public static byte[] sha384(String data) { public static byte[] sha384(String data) {
return sha384(data.getBytes(Charsets.UTF_8)); return sha384(data.getBytes(UTF_8));
} }
public static String sha384Hex(byte[] data) { public static String sha384Hex(byte[] data) {
@ -210,7 +212,7 @@ public final class DigestUtils {
} }
public static byte[] sha512(String data) { public static byte[] sha512(String data) {
return sha512(data.getBytes(Charsets.UTF_8)); return sha512(data.getBytes(UTF_8));
} }
public static String sha512Hex(byte[] data) { public static String sha512Hex(byte[] data) {
@ -245,7 +247,7 @@ public final class DigestUtils {
} }
public static MessageDigest updateDigest(MessageDigest messageDigest, String valueToDigest) { public static MessageDigest updateDigest(MessageDigest messageDigest, String valueToDigest) {
messageDigest.update(valueToDigest.getBytes(Charsets.UTF_8)); messageDigest.update(valueToDigest.getBytes(UTF_8));
return messageDigest; return messageDigest;
} }
} }

View File

@ -26,6 +26,8 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import static java.nio.charset.StandardCharsets.UTF_8;
/** /**
* *
* @author huang * @author huang
@ -44,7 +46,7 @@ public final class FileUtils {
} }
public static String readText(File file) throws IOException { public static String readText(File file) throws IOException {
return readText(file, Charsets.DEFAULT_CHARSET); return readText(file, UTF_8);
} }
public static String readText(File file, Charset charset) throws IOException { public static String readText(File file, Charset charset) throws IOException {
@ -60,7 +62,7 @@ public final class FileUtils {
} }
public static void writeText(File file, String text) throws IOException { public static void writeText(File file, String text) throws IOException {
writeText(file, text, Charsets.DEFAULT_CHARSET); writeText(file, text, UTF_8);
} }
public static void writeText(File file, String text, Charset charset) throws IOException { public static void writeText(File file, String text, Charset charset) throws IOException {

View File

@ -19,6 +19,8 @@ package org.jackhuang.hmcl.util;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import static java.nio.charset.StandardCharsets.UTF_8;
public final class Hex { public final class Hex {
private static final char[] DIGITS_LOWER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; private static final char[] DIGITS_LOWER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
@ -78,7 +80,7 @@ public final class Hex {
} }
public Hex() { public Hex() {
this(Charsets.DEFAULT_CHARSET); this(UTF_8);
} }
public Hex(Charset charset) { public Hex(Charset charset) {

View File

@ -28,6 +28,7 @@ import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.function.Supplier; import java.util.function.Supplier;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.jackhuang.hmcl.util.StringUtils.*; import static org.jackhuang.hmcl.util.StringUtils.*;
/** /**
@ -86,7 +87,7 @@ public final class NetworkUtils {
} }
public static String doPost(URL url, String post, String contentType, Proxy proxy) throws IOException { public static String doPost(URL url, String post, String contentType, Proxy proxy) throws IOException {
byte[] bytes = post.getBytes(Charsets.UTF_8); byte[] bytes = post.getBytes(UTF_8);
HttpURLConnection con = createConnection(url, proxy); HttpURLConnection con = createConnection(url, proxy);
con.setRequestMethod("POST"); con.setRequestMethod("POST");
@ -108,12 +109,12 @@ public final class NetworkUtils {
InputStream is = null; InputStream is = null;
try { try {
is = con.getInputStream(); is = con.getInputStream();
return IOUtils.readFullyAsString(is, Charsets.UTF_8); return IOUtils.readFullyAsString(is, UTF_8);
} catch (IOException e) { } catch (IOException e) {
IOUtils.closeQuietly(is); IOUtils.closeQuietly(is);
is = con.getErrorStream(); is = con.getErrorStream();
if (is != null) if (is != null)
return IOUtils.readFullyAsString(is, Charsets.UTF_8); return IOUtils.readFullyAsString(is, UTF_8);
throw e; throw e;
} finally { } finally {
IOUtils.closeQuietly(is); IOUtils.closeQuietly(is);