From 881e7a051c889bfc4b3c3d3d2b9ed3b35333aa1d Mon Sep 17 00:00:00 2001 From: rtm516 Date: Sun, 22 Nov 2020 10:40:53 +0000 Subject: [PATCH] Another android fix (#1575) * Fix en_us hash reading on Android (again) * Fix hash generation methods for Android --- .../geysermc/connector/utils/FileUtils.java | 27 ++++++++++++++----- .../geysermc/connector/utils/LocaleUtils.java | 7 ++--- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/connector/src/main/java/org/geysermc/connector/utils/FileUtils.java b/connector/src/main/java/org/geysermc/connector/utils/FileUtils.java index 0b2b132a..4b0178e6 100644 --- a/connector/src/main/java/org/geysermc/connector/utils/FileUtils.java +++ b/connector/src/main/java/org/geysermc/connector/utils/FileUtils.java @@ -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; + } } diff --git a/connector/src/main/java/org/geysermc/connector/utils/LocaleUtils.java b/connector/src/main/java/org/geysermc/connector/utils/LocaleUtils.java index aac344dc..664ef208 100644 --- a/connector/src/main/java/org/geysermc/connector/utils/LocaleUtils.java +++ b/connector/src/main/java/org/geysermc/connector/utils/LocaleUtils.java @@ -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();