Added UTF-8 encoding to intput / output streams.

This commit is contained in:
Erik C. Thauvin 2018-07-13 00:36:07 -07:00
parent e953f1962e
commit 2dc748be75
2 changed files with 26 additions and 16 deletions

View file

@ -37,6 +37,7 @@ import com.rometools.rome.io.SyndFeedInput;
import com.rometools.rome.io.SyndFeedOutput; import com.rometools.rome.io.SyndFeedOutput;
import java.io.*; import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.List; import java.util.List;
@ -86,12 +87,13 @@ final class EntriesMgr {
* @throws FeedException If an error occurred while reading the feed. * @throws FeedException If an error occurred while reading the feed.
*/ */
public static void loadBacklogs(final String file, final List<String> history) public static void loadBacklogs(final String file, final List<String> history)
throws IOException, FeedException { throws IOException, FeedException {
history.clear(); history.clear();
final SyndFeedInput input = new SyndFeedInput(); 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); final SyndFeed feed = input.build(reader);
@ -117,14 +119,14 @@ final class EntriesMgr {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static String loadEntries(final String file, final String channel, final List<EntryLink> entries) public static String loadEntries(final String file, final String channel, final List<EntryLink> entries)
throws IOException, FeedException { throws IOException, FeedException {
entries.clear(); entries.clear();
final SyndFeedInput input = new SyndFeedInput(); final SyndFeedInput input = new SyndFeedInput();
final String today; 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); final SyndFeed feed = input.build(reader);
today = Utils.isoLocalDate(feed.getPublishedDate()); today = Utils.isoLocalDate(feed.getPublishedDate());
@ -182,10 +184,11 @@ final class EntriesMgr {
} }
if (Utils.isValidString(bot.getLogsDir()) && Utils.isValidString(bot.getWeblogUrl())) { if (Utils.isValidString(bot.getLogsDir()) && Utils.isValidString(bot.getWeblogUrl())) {
FileWriter fw = null; Writer fw = null;
try { 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(); SyndFeed rss = new SyndFeedImpl();
rss.setFeedType("rss_2.0"); rss.setFeedType("rss_2.0");
@ -249,7 +252,9 @@ final class EntriesMgr {
output.output(rss, fw); output.output(rss, fw);
fw.close(); 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); output.output(rss, fw);
if (isDayBackup) { if (isDayBackup) {
@ -263,7 +268,8 @@ final class EntriesMgr {
} }
fw.close(); 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 = 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

@ -31,6 +31,7 @@
*/ */
package net.thauvin.erik.mobibot.modules; package net.thauvin.erik.mobibot.modules;
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 org.json.JSONArray; import org.json.JSONArray;
@ -41,6 +42,7 @@ import java.io.InputStreamReader;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
/** /**
* The GoogleSearch module. * The GoogleSearch module.
@ -108,22 +110,24 @@ final public class GoogleSearch extends AbstractModule {
/** /**
* Searches Google. * Searches Google.
*/ */
@SuppressFBWarnings(value = "URLCONNECTION_SSRF_FD")
private void run(final Mobibot bot, final String sender, final String query) { private void run(final Mobibot bot, final String sender, final String query) {
try { try {
final String q = URLEncoder.encode(query, "UTF-8"); final String q = URLEncoder.encode(query, "UTF-8");
final URL url = final URL url =
new URL("https://www.googleapis.com/customsearch/v1?key=" new URL("https://www.googleapis.com/customsearch/v1?key="
+ properties.get(GOOGLE_API_KEY_PROP) + properties.get(GOOGLE_API_KEY_PROP)
+ "&cx=" + "&cx="
+ properties.get(GOOGLE_CSE_KEY_PROP) + properties.get(GOOGLE_CSE_KEY_PROP)
+ "&q=" + "&q="
+ q + q
+ "&filter=1&num=5&alt=json"); + "&filter=1&num=5&alt=json");
final URLConnection conn = url.openConnection(); final URLConnection conn = url.openConnection();
final StringBuilder sb = new StringBuilder(); 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; String line;
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
sb.append(line); sb.append(line);