Another android fix (#1575)

* Fix en_us hash reading on Android (again)

* Fix hash generation methods for Android
This commit is contained in:
rtm516 2020-11-22 10:40:53 +00:00 committed by GitHub
parent 9ed3197191
commit 881e7a051c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 9 deletions

View File

@ -34,10 +34,7 @@ import org.reflections.Reflections;
import org.reflections.serializers.XmlSerializer;
import org.reflections.util.ConfigurationBuilder;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.net.URL;
import java.nio.file.Files;
import java.security.MessageDigest;
@ -168,7 +165,7 @@ public class FileUtils {
byte[] sha256;
try {
sha256 = MessageDigest.getInstance("SHA-256").digest(Files.readAllBytes(file.toPath()));
sha256 = MessageDigest.getInstance("SHA-256").digest(readAllBytes(file));
} catch (Exception e) {
throw new RuntimeException("Could not calculate pack hash", e);
}
@ -186,7 +183,7 @@ public class FileUtils {
byte[] sha1;
try {
sha1 = MessageDigest.getInstance("SHA-1").digest(Files.readAllBytes(file.toPath()));
sha1 = MessageDigest.getInstance("SHA-1").digest(readAllBytes(file));
} catch (Exception e) {
throw new RuntimeException("Could not calculate pack hash", e);
}
@ -210,4 +207,22 @@ public class FileUtils {
return reflections;
}
/**
* An android compatible version of {@link Files#readAllBytes}
*
* @param file File to read bytes of
* @return The byte array of the file
*/
public static byte[] readAllBytes(File file) {
int size = (int) file.length();
byte[] bytes = new byte[size];
try {
BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
buf.read(bytes, 0, bytes.length);
buf.close();
} catch (IOException ignored) { }
return bytes;
}
}

View File

@ -140,9 +140,10 @@ public class LocaleUtils {
if (locale.equals("en_us")) {
try {
Path hashFile = GeyserConnector.getInstance().getBootstrap().getConfigFolder().resolve("locales/en_us.hash");
if (hashFile.toFile().exists()) {
curHash = String.join("", Files.readAllLines(hashFile));
File hashFile = GeyserConnector.getInstance().getBootstrap().getConfigFolder().resolve("locales/en_us.hash").toFile();
if (hashFile.exists()) {
BufferedReader br = new BufferedReader(new FileReader(hashFile));
curHash = br.readLine().trim();
}
} catch (IOException ignored) { }
targetHash = clientJarInfo.getSha1();