From 5b60998b0a22084879a96fdd64c750842045fdf8 Mon Sep 17 00:00:00 2001 From: vemacs Date: Tue, 1 Mar 2016 11:52:29 -0700 Subject: [PATCH] Use try-with-resources --- .../earth2me/essentials/EssentialsConf.java | 31 ++++++------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/EssentialsConf.java b/Essentials/src/com/earth2me/essentials/EssentialsConf.java index 2e5f1aecd..d1bba99ab 100644 --- a/Essentials/src/com/earth2me/essentials/EssentialsConf.java +++ b/Essentials/src/com/earth2me/essentials/EssentialsConf.java @@ -97,8 +97,7 @@ public class EssentialsConf extends YamlConfiguration { try { - final FileInputStream inputStream = new FileInputStream(configFile); - try { + try (FileInputStream inputStream = new FileInputStream(configFile)) { long startSize = configFile.length(); if (startSize > Integer.MAX_VALUE) { throw new InvalidConfigurationException("File too big"); @@ -123,11 +122,11 @@ public class EssentialsConf extends YamlConfiguration { if (result.isError()) { buffer.rewind(); data.clear(); - LOGGER.log(Level.INFO, "File " + configFile.getAbsolutePath().toString() + " is not utf-8 encoded, trying " + Charset.defaultCharset().displayName()); + LOGGER.log(Level.INFO, "File " + configFile.getAbsolutePath() + " is not utf-8 encoded, trying " + Charset.defaultCharset().displayName()); decoder = Charset.defaultCharset().newDecoder(); result = decoder.decode(buffer, data, true); if (result.isError()) { - throw new InvalidConfigurationException("Invalid Characters in file " + configFile.getAbsolutePath().toString()); + throw new InvalidConfigurationException("Invalid Characters in file " + configFile.getAbsolutePath()); } else { decoder.flush(data); } @@ -137,8 +136,6 @@ public class EssentialsConf extends YamlConfiguration { final int end = data.position(); data.rewind(); super.loadFromString(data.subSequence(0, end).toString()); - } finally { - inputStream.close(); } } catch (IOException ex) { LOGGER.log(Level.SEVERE, ex.getMessage(), ex); @@ -251,9 +248,7 @@ public class EssentialsConf extends YamlConfiguration { if (future != null) { future.get(); } - } catch (InterruptedException ex) { - LOGGER.log(Level.SEVERE, ex.getMessage(), ex); - } catch (ExecutionException ex) { + } catch (InterruptedException | ExecutionException ex) { LOGGER.log(Level.SEVERE, ex.getMessage(), ex); } } @@ -274,9 +269,8 @@ public class EssentialsConf extends YamlConfiguration { } pendingDiskWrites.incrementAndGet(); - Future future = EXECUTOR_SERVICE.submit(new WriteRunner(configFile, data, pendingDiskWrites)); - return future; + return EXECUTOR_SERVICE.submit(new WriteRunner(configFile, data, pendingDiskWrites)); } @@ -318,17 +312,10 @@ public class EssentialsConf extends YamlConfiguration { } } - final FileOutputStream fos = new FileOutputStream(configFile); - try { - final OutputStreamWriter writer = new OutputStreamWriter(fos, UTF8); - - try { + try (FileOutputStream fos = new FileOutputStream(configFile)) { + try (OutputStreamWriter writer = new OutputStreamWriter(fos, UTF8)) { writer.write(data); - } finally { - writer.close(); } - } finally { - fos.close(); } } catch (IOException e) { LOGGER.log(Level.SEVERE, e.getMessage(), e); @@ -388,13 +375,13 @@ public class EssentialsConf extends YamlConfiguration { } public void setProperty(final String path, final ItemStack stack) { - final Map map = new HashMap(); + final Map map = new HashMap<>(); map.put("type", stack.getType().toString()); map.put("amount", stack.getAmount()); map.put("damage", stack.getDurability()); Map enchantments = stack.getEnchantments(); if (!enchantments.isEmpty()) { - Map enchant = new HashMap(); + Map enchant = new HashMap<>(); for (Map.Entry entry : enchantments.entrySet()) { enchant.put(entry.getKey().getName().toLowerCase(Locale.ENGLISH), entry.getValue()); }