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,17 +81,19 @@ public class FeedReader implements Runnable {
public final void run() {
try {
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();
if (items.isEmpty()) {
bot.send(sender, "There is currently nothing to view.", false);
} else {
SyndEntry item;
for (int i = 0; (i < items.size()) && (i < MAX_ITEMS); i++) {
item = items.get(i);
bot.send(sender, item.getTitle(), false);
bot.send(sender, Utils.helpIndent(Utils.green(item.getLink()), false), false);
final List<SyndEntry> items = feed.getEntries();
if (items.isEmpty()) {
bot.send(sender, "There is currently nothing to view.", false);
} else {
SyndEntry item;
for (int i = 0; (i < items.size()) && (i < MAX_ITEMS); i++) {
item = items.get(i);
bot.send(sender, item.getTitle(), false);
bot.send(sender, Utils.helpIndent(Utils.green(item.getLink()), false), false);
}
}
}
} catch (MalformedURLException e) {

View file

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