Added try-with-resources.

This commit is contained in:
Erik C. Thauvin 2020-08-04 15:06:56 -07:00
parent 94871a4df8
commit 70d99a028d
2 changed files with 18 additions and 15 deletions

View file

@ -81,7 +81,8 @@ public class FeedReader implements Runnable {
public final void run() { public final void run() {
try { try {
final SyndFeedInput input = new SyndFeedInput(); final SyndFeedInput input = new SyndFeedInput();
final SyndFeed feed = input.build(new XmlReader(new URL(url))); try (final XmlReader reader = new XmlReader(new URL(url))) {
final SyndFeed feed = input.build(reader);
final List<SyndEntry> items = feed.getEntries(); final List<SyndEntry> items = feed.getEntries();
if (items.isEmpty()) { if (items.isEmpty()) {
@ -94,6 +95,7 @@ public class FeedReader implements Runnable {
bot.send(sender, Utils.helpIndent(Utils.green(item.getLink()), false), false); bot.send(sender, Utils.helpIndent(Utils.green(item.getLink()), false), false);
} }
} }
}
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
bot.getLogger().debug("Invalid feed URL.", e); bot.getLogger().debug("Invalid feed URL.", e);
bot.send(sender, "The feed URL is invalid.", false); bot.send(sender, "The feed URL is invalid.", false);

View file

@ -113,11 +113,12 @@ final class TellMessagesMgr {
*/ */
public static void save(final String file, final List<TellMessage> messages, final Logger logger) { public static void save(final String file, final List<TellMessage> messages, final Logger logger) {
try { try {
try (final ObjectOutput output = new ObjectOutputStream( try (final BufferedOutputStream bos = new BufferedOutputStream(Files.newOutputStream(Paths.get(file)))) {
new BufferedOutputStream(Files.newOutputStream(Paths.get(file))))) { try (final ObjectOutput output = new ObjectOutputStream(bos)) {
logger.debug("Saving the messages."); logger.debug("Saving the messages.");
output.writeObject(messages); output.writeObject(messages);
} }
}
} catch (IOException e) { } catch (IOException e) {
logger.error("Unable to save messages queue.", e); logger.error("Unable to save messages queue.", e);
} }