清理Hex

This commit is contained in:
yushijinhun
2018-06-07 12:17:58 +08:00
parent 27c9abd9ed
commit 6a04ab37d7
6 changed files with 23 additions and 86 deletions

View File

@@ -25,7 +25,7 @@ import java.net.Proxy;
import java.util.Map;
import static org.jackhuang.hmcl.util.DigestUtils.digest;
import static org.jackhuang.hmcl.util.Hex.encodeHexString;
import static org.jackhuang.hmcl.util.Hex.encodeHex;
import static org.jackhuang.hmcl.util.Lang.tryCast;
/**
@@ -57,7 +57,7 @@ public class OfflineAccountFactory extends AccountFactory<OfflineAccount> {
}
private static String getUUIDFromUserName(String username) {
return encodeHexString(digest("MD5", username));
return encodeHex(digest("MD5", username));
}
}

View File

@@ -35,7 +35,7 @@ import java.util.Map;
import java.util.logging.Level;
import static org.jackhuang.hmcl.util.DigestUtils.digest;
import static org.jackhuang.hmcl.util.Hex.encodeHexString;
import static org.jackhuang.hmcl.util.Hex.encodeHex;
/**
*
@@ -93,7 +93,7 @@ public final class GameAssetDownloadTask extends Task {
try {
// check the checksum of file to ensure that the file is not need to re-download.
if (file.exists()) {
String sha1 = encodeHexString(digest("SHA-1", FileUtils.readBytes(file)));
String sha1 = encodeHex(digest("SHA-1", FileUtils.readBytes(file)));
if (sha1.equals(assetObject.getHash())) {
++downloaded;
Logging.LOG.finest("File $file has been downloaded successfully, skipped downloading");

View File

@@ -8,7 +8,7 @@ import org.jackhuang.hmcl.task.Task;
import org.jackhuang.hmcl.util.*;
import static org.jackhuang.hmcl.util.DigestUtils.digest;
import static org.jackhuang.hmcl.util.Hex.encodeHexString;
import static org.jackhuang.hmcl.util.Hex.encodeHex;
import java.io.*;
import java.nio.charset.Charset;
@@ -80,7 +80,7 @@ public final class LibraryDownloadTask extends Task {
return true;
}
byte[] fileData = FileUtils.readBytes(libPath);
boolean valid = checksums.contains(encodeHexString(digest("SHA-1", fileData)));
boolean valid = checksums.contains(encodeHex(digest("SHA-1", fileData)));
if ((!valid) && (libPath.getName().endsWith(".jar"))) {
}
return validateJar(fileData, checksums);
@@ -101,7 +101,7 @@ public final class LibraryDownloadTask extends Task {
hashes = new String(eData, Charset.forName("UTF-8")).split("\n");
}
if (!entry.isDirectory()) {
files.put(entry.getName(), encodeHexString(digest("SHA-1", eData)));
files.put(entry.getName(), encodeHex(digest("SHA-1", eData)));
}
entry = jar.getNextJarEntry();
}

View File

@@ -29,7 +29,7 @@ import java.util.LinkedList;
import java.util.List;
import static org.jackhuang.hmcl.util.DigestUtils.digest;
import static org.jackhuang.hmcl.util.Hex.encodeHexString;
import static org.jackhuang.hmcl.util.Hex.encodeHex;
public final class MinecraftInstanceTask<T> extends Task {
@@ -64,7 +64,7 @@ public final class MinecraftInstanceTask<T> extends Task {
if (path.startsWith("/") || path.startsWith("\\"))
path = path.substring(1);
overrides.add(new ModpackConfiguration.FileInformation(path, encodeHexString(digest("SHA-1", zip))));
overrides.add(new ModpackConfiguration.FileInformation(path, encodeHex(digest("SHA-1", zip))));
}
}

View File

@@ -31,7 +31,7 @@ import java.util.Set;
import java.util.function.Predicate;
import static org.jackhuang.hmcl.util.DigestUtils.digest;
import static org.jackhuang.hmcl.util.Hex.encodeHexString;
import static org.jackhuang.hmcl.util.Hex.encodeHex;
public class ModpackInstallTask<T> extends Task {
@@ -94,8 +94,8 @@ public class ModpackInstallTask<T> extends Task {
byte[] data = os.toByteArray();
if (files.contains(path) && entryFile.exists()) {
String oldHash = encodeHexString(digest("SHA-1", new FileInputStream(entryFile)));
String newHash = encodeHexString(digest("SHA-1", new ByteArrayInputStream(data)));
String oldHash = encodeHex(digest("SHA-1", new FileInputStream(entryFile)));
String newHash = encodeHex(digest("SHA-1", new ByteArrayInputStream(data)));
if (!oldHash.equals(newHash)) {
try (FileOutputStream fos = new FileOutputStream(entryFile)) {
IOUtils.copyTo(new ByteArrayInputStream(data), fos, buf);

View File

@@ -17,22 +17,18 @@
*/
package org.jackhuang.hmcl.util;
import java.nio.charset.Charset;
import static java.nio.charset.StandardCharsets.UTF_8;
import java.io.IOException;
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_UPPER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
private final Charset charset;
public static byte[] decodeHex(char[] data) throws Exception {
public static byte[] decodeHex(String str) throws IOException {
char[] data = str.toCharArray();
int len = data.length;
if ((len & 0x1) != 0)
throw new Exception("Odd number of characters.");
throw new IOException("Odd number of characters.");
byte[] out = new byte[len >> 1];
@@ -48,83 +44,24 @@ public final class Hex {
return out;
}
public static char[] encodeHex(byte[] data) {
return encodeHex(data, true);
}
public static char[] encodeHex(byte[] data, boolean toLowerCase) {
return encodeHex(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
}
protected static char[] encodeHex(byte[] data, char[] toDigits) {
public static String encodeHex(byte[] data) {
int l = data.length;
char[] out = new char[l << 1];
int i = 0;
for (int j = 0; i < l; i++) {
out[(j++)] = toDigits[((0xF0 & data[i]) >>> 4)];
out[(j++)] = toDigits[(0xF & data[i])];
out[(j++)] = DIGITS_LOWER[((0xF0 & data[i]) >>> 4)];
out[(j++)] = DIGITS_LOWER[(0xF & data[i])];
}
return out;
return new String(out);
}
public static String encodeHexString(byte[] data) {
return new String(encodeHex(data));
}
protected static int toDigit(char ch, int index) {
private static int toDigit(char ch, int index) throws IOException {
int digit = Character.digit(ch, 16);
if (digit == -1)
throw new IllegalArgumentException("Illegal hexadecimal character " + ch + " at index " + index);
throw new IOException("Illegal hexadecimal character " + ch + " at index " + index);
return digit;
}
public Hex() {
this(UTF_8);
}
public Hex(Charset charset) {
this.charset = charset;
}
public byte[] decode(byte[] array) throws Exception {
return decodeHex(new String(array, getCharset()).toCharArray());
}
public Object decode(Object object) throws Exception {
try {
char[] charArray = (object instanceof String) ? ((String) object).toCharArray() : (char[]) object;
return decodeHex(charArray);
} catch (ClassCastException e) {
throw new Exception(e.getMessage(), e);
}
}
public byte[] encode(byte[] array) {
return encodeHexString(array).getBytes(getCharset());
}
public Object encode(Object object)
throws Exception {
try {
byte[] byteArray = (object instanceof String) ? ((String) object).getBytes(getCharset()) : (byte[]) object;
return encodeHex(byteArray);
} catch (ClassCastException e) {
throw new Exception(e.getMessage(), e);
}
}
public Charset getCharset() {
return this.charset;
}
public String getCharsetName() {
return this.charset.name();
}
@Override
public String toString() {
return super.toString() + "[charsetName=" + this.charset + "]";
}
private Hex() {}
}