diff --git a/Common/src/main/java/net/frankheijden/serverutils/common/tasks/PluginWatcherTask.java b/Common/src/main/java/net/frankheijden/serverutils/common/tasks/PluginWatcherTask.java index 13c9fb7..d439fd8 100644 --- a/Common/src/main/java/net/frankheijden/serverutils/common/tasks/PluginWatcherTask.java +++ b/Common/src/main/java/net/frankheijden/serverutils/common/tasks/PluginWatcherTask.java @@ -78,7 +78,7 @@ public class PluginWatcherTask extends AbstractTask { String previousHash = hash; long previousHashTimestamp = hashTimestamp; - hash = FileUtils.getHash(file); + hash = FileUtils.getHash(file.toPath()); hashTimestamp = System.currentTimeMillis(); task = ServerUtilsApp.getPlugin().getTaskManager().runTaskLater(() -> { if (hash.equals(previousHash) || previousHashTimestamp < hashTimestamp - 1000L) { diff --git a/Common/src/main/java/net/frankheijden/serverutils/common/utils/FileUtils.java b/Common/src/main/java/net/frankheijden/serverutils/common/utils/FileUtils.java index 91bc17d..e9e63de 100644 --- a/Common/src/main/java/net/frankheijden/serverutils/common/utils/FileUtils.java +++ b/Common/src/main/java/net/frankheijden/serverutils/common/utils/FileUtils.java @@ -2,15 +2,16 @@ package net.frankheijden.serverutils.common.utils; import com.google.gson.JsonElement; import com.google.gson.JsonParser; -import org.apache.commons.codec.digest.DigestUtils; import java.io.BufferedReader; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.nio.file.Path; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; public class FileUtils { @@ -36,17 +37,18 @@ public class FileUtils { } /** - * Get the Hash of a file. + * Get the Hash of a file at given path. * - * @param file The file + * @param path The path * @return The file's hash */ - public static String getHash(File file) { + public static String getHash(Path path) { + byte[] digest; try { - return DigestUtils.md5Hex(new FileInputStream(file)); - } catch (IOException e) { - // Shouldn't happen + digest = MessageDigest.getInstance("MD5").digest(Files.readAllBytes(path)); + } catch (IOException | NoSuchAlgorithmException ex) { return null; } + return StringUtils.bytesToHex(digest); } } diff --git a/Common/src/main/java/net/frankheijden/serverutils/common/utils/StringUtils.java b/Common/src/main/java/net/frankheijden/serverutils/common/utils/StringUtils.java index 8f5be91..a0c2eeb 100644 --- a/Common/src/main/java/net/frankheijden/serverutils/common/utils/StringUtils.java +++ b/Common/src/main/java/net/frankheijden/serverutils/common/utils/StringUtils.java @@ -1,5 +1,7 @@ package net.frankheijden.serverutils.common.utils; +import java.nio.charset.StandardCharsets; + public class StringUtils { /** @@ -44,4 +46,20 @@ public class StringUtils { } return sb.substring(1); } + + private static final byte[] HEX_ARRAY = "0123456789abcdef".getBytes(StandardCharsets.US_ASCII); + + /** + * Converts a bytes array to hex. + * via https://stackoverflow.com/a/9855338/11239174 + */ + public static String bytesToHex(byte[] bytes) { + byte[] hexChars = new byte[bytes.length * 2]; + for (int j = 0; j < bytes.length; j++) { + int v = bytes[j] & 0xFF; + hexChars[j * 2] = HEX_ARRAY[v >>> 4]; + hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F]; + } + return new String(hexChars, StandardCharsets.UTF_8); + } } diff --git a/build.gradle b/build.gradle index 79b1d55..f061886 100644 --- a/build.gradle +++ b/build.gradle @@ -24,7 +24,6 @@ subprojects { } dependencies { - implementation 'commons-codec:commons-codec:1.15' implementation 'com.github.FrankHeijden:MinecraftReflection:1.0.0' testCompile 'org.assertj:assertj-core:3.18.1'