From ee7111d91d97a81bdf66ee8e057e9a457312ecce Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Sat, 20 Apr 2019 05:26:21 -0700 Subject: [PATCH] Now using java.nio newOutputStream and newInputStream. --- .../net/thauvin/erik/mobibot/Mobibot.java | 8 +++--- .../erik/mobibot/entries/EntriesMgr.java | 26 ++++++++++--------- .../erik/mobibot/tell/TellMessagesMgr.java | 9 ++++--- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/main/java/net/thauvin/erik/mobibot/Mobibot.java b/src/main/java/net/thauvin/erik/mobibot/Mobibot.java index a81d108..641d569 100644 --- a/src/main/java/net/thauvin/erik/mobibot/Mobibot.java +++ b/src/main/java/net/thauvin/erik/mobibot/Mobibot.java @@ -73,11 +73,13 @@ import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; import java.io.PrintStream; +import java.nio.file.Files; +import java.nio.file.Paths; import java.time.Clock; import java.time.LocalDateTime; import java.util.ArrayList; @@ -354,8 +356,8 @@ public class Mobibot extends PircBot { } else { final Properties p = new Properties(); - try (final FileInputStream fis = new FileInputStream( - line.getOptionValue(Commands.PROPS_ARG.charAt(0), "./mobibot.properties"))) { + try (final InputStream fis = Files.newInputStream(Paths.get( + line.getOptionValue(Commands.PROPS_ARG.charAt(0), "./mobibot.properties")))) { // Load the properties files p.load(fis); } catch (FileNotFoundException e) { diff --git a/src/main/java/net/thauvin/erik/mobibot/entries/EntriesMgr.java b/src/main/java/net/thauvin/erik/mobibot/entries/EntriesMgr.java index 1e485b9..3a3920f 100644 --- a/src/main/java/net/thauvin/erik/mobibot/entries/EntriesMgr.java +++ b/src/main/java/net/thauvin/erik/mobibot/entries/EntriesMgr.java @@ -41,16 +41,17 @@ import com.rometools.rome.feed.synd.SyndFeedImpl; import com.rometools.rome.io.FeedException; import com.rometools.rome.io.SyndFeedInput; import com.rometools.rome.io.SyndFeedOutput; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import net.thauvin.erik.mobibot.Mobibot; import net.thauvin.erik.mobibot.Utils; -import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.Writer; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Calendar; import java.util.List; @@ -105,15 +106,15 @@ public final class EntriesMgr { final SyndFeedInput input = new SyndFeedInput(); try (final InputStreamReader reader = - new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8)) { + new InputStreamReader(Files.newInputStream(Paths.get(file)), StandardCharsets.UTF_8)) { final SyndFeed feed = input.build(reader); - final List items = feed.getEntries(); + final List items = feed.getEntries(); SyndEntry item; for (int i = items.size() - 1; i >= 0; i--) { - item = (SyndEntryImpl) items.get(i); + item = items.get(i); history.add(item.getTitle()); } } @@ -139,12 +140,12 @@ public final class EntriesMgr { final String today; try (final InputStreamReader reader = new InputStreamReader( - new FileInputStream(file), StandardCharsets.UTF_8)) { + Files.newInputStream(Paths.get(file)), StandardCharsets.UTF_8)) { final SyndFeed feed = input.build(reader); today = Utils.isoLocalDate(feed.getPublishedDate()); - final List items = feed.getEntries(); + final List items = feed.getEntries(); SyndEntry item; SyndContent description; String[] comments; @@ -152,7 +153,7 @@ public final class EntriesMgr { EntryLink entry; for (int i = items.size() - 1; i >= 0; i--) { - item = (SyndEntryImpl) items.get(i); + item = items.get(i); author = item.getAuthor() .substring(item.getAuthor().lastIndexOf('(') + 1, item.getAuthor().length() - 1); entry = new EntryLink(item.getLink(), @@ -188,6 +189,7 @@ public final class EntriesMgr { * @param history The history array. * @param isDayBackup Set the true if the daily backup file should also be created. */ + @SuppressFBWarnings(value = "CE_CLASS_ENVY", justification = "Yes, it does.") public static void saveEntries(final Mobibot bot, final List entries, final List history, @@ -204,7 +206,7 @@ public final class EntriesMgr { SyndEntry item; SyndContent description; try (final Writer fw = new OutputStreamWriter( - new FileOutputStream(bot.getLogsDir() + CURRENT_XML), StandardCharsets.UTF_8)) { + Files.newOutputStream(Paths.get(bot.getLogsDir() + CURRENT_XML)), StandardCharsets.UTF_8)) { rss.setFeedType("rss_2.0"); rss.setTitle(bot.getChannel() + " IRC Links"); rss.setDescription("Links from " + bot.getIrcServer() + " on " + bot.getChannel()); @@ -263,8 +265,8 @@ public final class EntriesMgr { } try (final Writer fw = new OutputStreamWriter( - new FileOutputStream( - bot.getLogsDir() + bot.getToday() + XML_EXT), StandardCharsets.UTF_8)) { + Files.newOutputStream(Paths.get( + bot.getLogsDir() + bot.getToday() + XML_EXT)), StandardCharsets.UTF_8)) { output.output(rss, fw); } @@ -279,7 +281,7 @@ public final class EntriesMgr { } try (final Writer fw = new OutputStreamWriter( - new FileOutputStream(bot.getLogsDir() + NAV_XML), StandardCharsets.UTF_8)) { + Files.newOutputStream(Paths.get(bot.getLogsDir() + NAV_XML)), StandardCharsets.UTF_8)) { rss = new SyndFeedImpl(); rss.setFeedType("rss_2.0"); rss.setTitle(bot.getChannel() + " IRC Links Backlogs"); diff --git a/src/main/java/net/thauvin/erik/mobibot/tell/TellMessagesMgr.java b/src/main/java/net/thauvin/erik/mobibot/tell/TellMessagesMgr.java index fdddd99..a445815 100644 --- a/src/main/java/net/thauvin/erik/mobibot/tell/TellMessagesMgr.java +++ b/src/main/java/net/thauvin/erik/mobibot/tell/TellMessagesMgr.java @@ -36,14 +36,14 @@ import org.apache.logging.log4j.Logger; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; -import java.io.FileInputStream; import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectInputStream; import java.io.ObjectOutput; import java.io.ObjectOutputStream; +import java.nio.file.Files; +import java.nio.file.Paths; import java.time.Clock; import java.time.LocalDateTime; import java.util.ArrayList; @@ -90,7 +90,8 @@ final class TellMessagesMgr { public static List load(final String file, final Logger logger) { try { - try (final ObjectInput input = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file)))) { + try (final ObjectInput input = new ObjectInputStream( + new BufferedInputStream(Files.newInputStream(Paths.get(file))))) { if (logger.isDebugEnabled()) { logger.debug("Loading the messages."); } @@ -119,7 +120,7 @@ final class TellMessagesMgr { try { try (final ObjectOutput output = new ObjectOutputStream( - new BufferedOutputStream(new FileOutputStream(file)))) { + new BufferedOutputStream(Files.newOutputStream(Paths.get(file))))) { if (logger.isDebugEnabled()) { logger.debug("Saving the messages."); }