From 2dc748be75d2317a4bfdd966c5a4587011cb9d03 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Fri, 13 Jul 2018 00:36:07 -0700 Subject: [PATCH] Added UTF-8 encoding to intput / output streams. --- .../net/thauvin/erik/mobibot/EntriesMgr.java | 22 ++++++++++++------- .../erik/mobibot/modules/GoogleSearch.java | 20 ++++++++++------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/main/java/net/thauvin/erik/mobibot/EntriesMgr.java b/src/main/java/net/thauvin/erik/mobibot/EntriesMgr.java index 8535ebf..6d494c7 100644 --- a/src/main/java/net/thauvin/erik/mobibot/EntriesMgr.java +++ b/src/main/java/net/thauvin/erik/mobibot/EntriesMgr.java @@ -37,6 +37,7 @@ import com.rometools.rome.io.SyndFeedInput; import com.rometools.rome.io.SyndFeedOutput; import java.io.*; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Calendar; import java.util.List; @@ -86,12 +87,13 @@ final class EntriesMgr { * @throws FeedException If an error occurred while reading the feed. */ public static void loadBacklogs(final String file, final List history) - throws IOException, FeedException { + throws IOException, FeedException { history.clear(); final SyndFeedInput input = new SyndFeedInput(); - try (final InputStreamReader reader = new InputStreamReader(new FileInputStream(new File(file)))) { + try (final InputStreamReader reader = + new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8)) { final SyndFeed feed = input.build(reader); @@ -117,14 +119,14 @@ final class EntriesMgr { */ @SuppressWarnings("unchecked") public static String loadEntries(final String file, final String channel, final List entries) - throws IOException, FeedException { + throws IOException, FeedException { entries.clear(); final SyndFeedInput input = new SyndFeedInput(); final String today; - try (InputStreamReader reader = new InputStreamReader(new FileInputStream(new File(file)))) { + try (InputStreamReader reader = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8)) { final SyndFeed feed = input.build(reader); today = Utils.isoLocalDate(feed.getPublishedDate()); @@ -182,10 +184,11 @@ final class EntriesMgr { } if (Utils.isValidString(bot.getLogsDir()) && Utils.isValidString(bot.getWeblogUrl())) { - FileWriter fw = null; + Writer fw = null; try { - fw = new FileWriter(new File(bot.getLogsDir() + CURRENT_XML)); + fw = new OutputStreamWriter( + new FileOutputStream(bot.getLogsDir() + CURRENT_XML), StandardCharsets.UTF_8); SyndFeed rss = new SyndFeedImpl(); rss.setFeedType("rss_2.0"); @@ -249,7 +252,9 @@ final class EntriesMgr { output.output(rss, fw); fw.close(); - fw = new FileWriter(new File(bot.getLogsDir() + bot.getToday() + XML_EXT)); + fw = new OutputStreamWriter( + new FileOutputStream( + bot.getLogsDir() + bot.getToday() + XML_EXT), StandardCharsets.UTF_8); output.output(rss, fw); if (isDayBackup) { @@ -263,7 +268,8 @@ final class EntriesMgr { } fw.close(); - fw = new FileWriter(new File(bot.getLogsDir() + NAV_XML)); + fw = new OutputStreamWriter( + new FileOutputStream(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/modules/GoogleSearch.java b/src/main/java/net/thauvin/erik/mobibot/modules/GoogleSearch.java index ef19b94..0097a28 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/GoogleSearch.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/GoogleSearch.java @@ -31,6 +31,7 @@ */ package net.thauvin.erik.mobibot.modules; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import net.thauvin.erik.mobibot.Mobibot; import net.thauvin.erik.mobibot.Utils; import org.json.JSONArray; @@ -41,6 +42,7 @@ import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; /** * The GoogleSearch module. @@ -108,22 +110,24 @@ final public class GoogleSearch extends AbstractModule { /** * Searches Google. */ + @SuppressFBWarnings(value = "URLCONNECTION_SSRF_FD") private void run(final Mobibot bot, final String sender, final String query) { try { final String q = URLEncoder.encode(query, "UTF-8"); final URL url = - new URL("https://www.googleapis.com/customsearch/v1?key=" - + properties.get(GOOGLE_API_KEY_PROP) - + "&cx=" - + properties.get(GOOGLE_CSE_KEY_PROP) - + "&q=" - + q - + "&filter=1&num=5&alt=json"); + new URL("https://www.googleapis.com/customsearch/v1?key=" + + properties.get(GOOGLE_API_KEY_PROP) + + "&cx=" + + properties.get(GOOGLE_CSE_KEY_PROP) + + "&q=" + + q + + "&filter=1&num=5&alt=json"); final URLConnection conn = url.openConnection(); final StringBuilder sb = new StringBuilder(); - try (final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()))) { + try (final BufferedReader reader = + new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) { String line; while ((line = reader.readLine()) != null) { sb.append(line);