Fix scanner resource leak

This commit is contained in:
Tad Hunt 2023-09-19 08:37:54 -06:00
parent 15a79bdd7e
commit fa0301663f
1 changed files with 15 additions and 4 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,21 @@ 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()) {
throw new IllegalStateException("No content in " + res + " in jar, please re-install MobArena");
String contents;
Scanner scanner = null;
try {
scanner = new Scanner(is).useDelimiter("\\A");
contents = scanner.next();
} catch (NoSuchElementException e) {
throw new IllegalStateException("No content in " + res + " in jar, please re-install MobArena");
} catch (Exception e) {
throw e;
} finally {
if (scanner != null) {
scanner.close();
}
}
String contents = scanner.next();
YamlConfiguration yaml = new YamlConfiguration();
try {
yaml.loadFromString(contents);