Use try-with-resources

This commit is contained in:
vemacs 2016-03-01 11:52:29 -07:00
parent 833713bee2
commit 5b60998b0a

View File

@ -97,8 +97,7 @@ public class EssentialsConf extends YamlConfiguration {
try { try {
final FileInputStream inputStream = new FileInputStream(configFile); try (FileInputStream inputStream = new FileInputStream(configFile)) {
try {
long startSize = configFile.length(); long startSize = configFile.length();
if (startSize > Integer.MAX_VALUE) { if (startSize > Integer.MAX_VALUE) {
throw new InvalidConfigurationException("File too big"); throw new InvalidConfigurationException("File too big");
@ -123,11 +122,11 @@ public class EssentialsConf extends YamlConfiguration {
if (result.isError()) { if (result.isError()) {
buffer.rewind(); buffer.rewind();
data.clear(); 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(); decoder = Charset.defaultCharset().newDecoder();
result = decoder.decode(buffer, data, true); result = decoder.decode(buffer, data, true);
if (result.isError()) { if (result.isError()) {
throw new InvalidConfigurationException("Invalid Characters in file " + configFile.getAbsolutePath().toString()); throw new InvalidConfigurationException("Invalid Characters in file " + configFile.getAbsolutePath());
} else { } else {
decoder.flush(data); decoder.flush(data);
} }
@ -137,8 +136,6 @@ public class EssentialsConf extends YamlConfiguration {
final int end = data.position(); final int end = data.position();
data.rewind(); data.rewind();
super.loadFromString(data.subSequence(0, end).toString()); super.loadFromString(data.subSequence(0, end).toString());
} finally {
inputStream.close();
} }
} catch (IOException ex) { } catch (IOException ex) {
LOGGER.log(Level.SEVERE, ex.getMessage(), ex); LOGGER.log(Level.SEVERE, ex.getMessage(), ex);
@ -251,9 +248,7 @@ public class EssentialsConf extends YamlConfiguration {
if (future != null) { if (future != null) {
future.get(); future.get();
} }
} catch (InterruptedException ex) { } catch (InterruptedException | ExecutionException ex) {
LOGGER.log(Level.SEVERE, ex.getMessage(), ex);
} catch (ExecutionException ex) {
LOGGER.log(Level.SEVERE, ex.getMessage(), ex); LOGGER.log(Level.SEVERE, ex.getMessage(), ex);
} }
} }
@ -274,9 +269,8 @@ public class EssentialsConf extends YamlConfiguration {
} }
pendingDiskWrites.incrementAndGet(); 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 (FileOutputStream fos = new FileOutputStream(configFile)) {
try { try (OutputStreamWriter writer = new OutputStreamWriter(fos, UTF8)) {
final OutputStreamWriter writer = new OutputStreamWriter(fos, UTF8);
try {
writer.write(data); writer.write(data);
} finally {
writer.close();
} }
} finally {
fos.close();
} }
} catch (IOException e) { } catch (IOException e) {
LOGGER.log(Level.SEVERE, e.getMessage(), 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) { public void setProperty(final String path, final ItemStack stack) {
final Map<String, Object> map = new HashMap<String, Object>(); final Map<String, Object> map = new HashMap<>();
map.put("type", stack.getType().toString()); map.put("type", stack.getType().toString());
map.put("amount", stack.getAmount()); map.put("amount", stack.getAmount());
map.put("damage", stack.getDurability()); map.put("damage", stack.getDurability());
Map<Enchantment, Integer> enchantments = stack.getEnchantments(); Map<Enchantment, Integer> enchantments = stack.getEnchantments();
if (!enchantments.isEmpty()) { if (!enchantments.isEmpty()) {
Map<String, Integer> enchant = new HashMap<String, Integer>(); Map<String, Integer> enchant = new HashMap<>();
for (Map.Entry<Enchantment, Integer> entry : enchantments.entrySet()) { for (Map.Entry<Enchantment, Integer> entry : enchantments.entrySet()) {
enchant.put(entry.getKey().getName().toLowerCase(Locale.ENGLISH), entry.getValue()); enchant.put(entry.getKey().getName().toLowerCase(Locale.ENGLISH), entry.getValue());
} }