Now using java.nio newOutputStream and newInputStream.

This commit is contained in:
Erik C. Thauvin 2019-04-20 05:26:21 -07:00
parent 48a6c29fc9
commit ee7111d91d
3 changed files with 24 additions and 19 deletions

View file

@ -73,11 +73,13 @@ import org.jsoup.Jsoup;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream; import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.Clock; import java.time.Clock;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.ArrayList; import java.util.ArrayList;
@ -354,8 +356,8 @@ public class Mobibot extends PircBot {
} else { } else {
final Properties p = new Properties(); final Properties p = new Properties();
try (final FileInputStream fis = new FileInputStream( try (final InputStream fis = Files.newInputStream(Paths.get(
line.getOptionValue(Commands.PROPS_ARG.charAt(0), "./mobibot.properties"))) { line.getOptionValue(Commands.PROPS_ARG.charAt(0), "./mobibot.properties")))) {
// Load the properties files // Load the properties files
p.load(fis); p.load(fis);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {

View file

@ -41,16 +41,17 @@ import com.rometools.rome.feed.synd.SyndFeedImpl;
import com.rometools.rome.io.FeedException; import com.rometools.rome.io.FeedException;
import com.rometools.rome.io.SyndFeedInput; import com.rometools.rome.io.SyndFeedInput;
import com.rometools.rome.io.SyndFeedOutput; import com.rometools.rome.io.SyndFeedOutput;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import net.thauvin.erik.mobibot.Mobibot; import net.thauvin.erik.mobibot.Mobibot;
import net.thauvin.erik.mobibot.Utils; import net.thauvin.erik.mobibot.Utils;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.Writer; import java.io.Writer;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.List; import java.util.List;
@ -105,15 +106,15 @@ public final class EntriesMgr {
final SyndFeedInput input = new SyndFeedInput(); final SyndFeedInput input = new SyndFeedInput();
try (final InputStreamReader reader = 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 SyndFeed feed = input.build(reader);
final List items = feed.getEntries(); final List<SyndEntry> items = feed.getEntries();
SyndEntry item; SyndEntry item;
for (int i = items.size() - 1; i >= 0; i--) { for (int i = items.size() - 1; i >= 0; i--) {
item = (SyndEntryImpl) items.get(i); item = items.get(i);
history.add(item.getTitle()); history.add(item.getTitle());
} }
} }
@ -139,12 +140,12 @@ public final class EntriesMgr {
final String today; final String today;
try (final InputStreamReader reader = new InputStreamReader( 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); final SyndFeed feed = input.build(reader);
today = Utils.isoLocalDate(feed.getPublishedDate()); today = Utils.isoLocalDate(feed.getPublishedDate());
final List items = feed.getEntries(); final List<SyndEntry> items = feed.getEntries();
SyndEntry item; SyndEntry item;
SyndContent description; SyndContent description;
String[] comments; String[] comments;
@ -152,7 +153,7 @@ public final class EntriesMgr {
EntryLink entry; EntryLink entry;
for (int i = items.size() - 1; i >= 0; i--) { for (int i = items.size() - 1; i >= 0; i--) {
item = (SyndEntryImpl) items.get(i); item = items.get(i);
author = item.getAuthor() author = item.getAuthor()
.substring(item.getAuthor().lastIndexOf('(') + 1, item.getAuthor().length() - 1); .substring(item.getAuthor().lastIndexOf('(') + 1, item.getAuthor().length() - 1);
entry = new EntryLink(item.getLink(), entry = new EntryLink(item.getLink(),
@ -188,6 +189,7 @@ public final class EntriesMgr {
* @param history The history array. * @param history The history array.
* @param isDayBackup Set the true if the daily backup file should also be created. * @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, public static void saveEntries(final Mobibot bot,
final List<EntryLink> entries, final List<EntryLink> entries,
final List<String> history, final List<String> history,
@ -204,7 +206,7 @@ public final class EntriesMgr {
SyndEntry item; SyndEntry item;
SyndContent description; SyndContent description;
try (final Writer fw = new OutputStreamWriter( 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.setFeedType("rss_2.0");
rss.setTitle(bot.getChannel() + " IRC Links"); rss.setTitle(bot.getChannel() + " IRC Links");
rss.setDescription("Links from " + bot.getIrcServer() + " on " + bot.getChannel()); rss.setDescription("Links from " + bot.getIrcServer() + " on " + bot.getChannel());
@ -263,8 +265,8 @@ public final class EntriesMgr {
} }
try (final Writer fw = new OutputStreamWriter( try (final Writer fw = new OutputStreamWriter(
new FileOutputStream( Files.newOutputStream(Paths.get(
bot.getLogsDir() + bot.getToday() + XML_EXT), StandardCharsets.UTF_8)) { bot.getLogsDir() + bot.getToday() + XML_EXT)), StandardCharsets.UTF_8)) {
output.output(rss, fw); output.output(rss, fw);
} }
@ -279,7 +281,7 @@ public final class EntriesMgr {
} }
try (final Writer fw = new OutputStreamWriter( 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 = new SyndFeedImpl();
rss.setFeedType("rss_2.0"); rss.setFeedType("rss_2.0");
rss.setTitle(bot.getChannel() + " IRC Links Backlogs"); rss.setTitle(bot.getChannel() + " IRC Links Backlogs");

View file

@ -36,14 +36,14 @@ import org.apache.logging.log4j.Logger;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectInput; import java.io.ObjectInput;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectOutput; import java.io.ObjectOutput;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.Clock; import java.time.Clock;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.ArrayList; import java.util.ArrayList;
@ -90,7 +90,8 @@ final class TellMessagesMgr {
public static List<TellMessage> load(final String file, final Logger logger) { public static List<TellMessage> load(final String file, final Logger logger) {
try { 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()) { if (logger.isDebugEnabled()) {
logger.debug("Loading the messages."); logger.debug("Loading the messages.");
} }
@ -119,7 +120,7 @@ final class TellMessagesMgr {
try { try {
try (final ObjectOutput output = new ObjectOutputStream( try (final ObjectOutput output = new ObjectOutputStream(
new BufferedOutputStream(new FileOutputStream(file)))) { new BufferedOutputStream(Files.newOutputStream(Paths.get(file))))) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("Saving the messages."); logger.debug("Saving the messages.");
} }