Fix scanner resource leak.

Wraps the Scanner in try-with-resources to ensure resource cleanup after
reading the config-file from the plugin jar.
This commit is contained in:
Tad Hunt 2023-10-21 17:24:53 -06:00 committed by GitHub
parent 15a79bdd7e
commit a8b2cf90e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 3 deletions

View File

@ -16,6 +16,7 @@ import java.text.DecimalFormatSymbols;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.Set;
@ -46,11 +47,15 @@ public class ConfigUtils
if (is == null) {
throw new IllegalStateException("Couldn't read " + res + " from jar, please re-install MobArena");
}
Scanner scanner = new Scanner(is).useDelimiter("\\A");
if (!scanner.hasNext()) {
String contents;
try (Scanner scanner = new Scanner(is)) {
scanner.useDelimiter("\\A");
contents = scanner.next();
} catch (NoSuchElementException e) {
throw new IllegalStateException("No content in " + res + " in jar, please re-install MobArena");
}
String contents = scanner.next();
YamlConfiguration yaml = new YamlConfiguration();
try {
yaml.loadFromString(contents);