From 676c3a2e3d338c1789172f57edb804ac353ff057 Mon Sep 17 00:00:00 2001 From: Kermina Awad Date: Sat, 6 Jun 2020 20:16:42 -0400 Subject: [PATCH] make pasting more system agnostic --- ...ithubPasteService.java => GitHubPasteService.java} | 11 ++++++++--- .../MultiverseCore/utils/webpaste/HttpAPIClient.java | 5 +++-- .../utils/webpaste/PastebinPasteService.java | 10 ++++++++-- 3 files changed, 19 insertions(+), 7 deletions(-) rename src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/{GithubPasteService.java => GitHubPasteService.java} (88%) diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/GithubPasteService.java b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/GitHubPasteService.java similarity index 88% rename from src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/GithubPasteService.java rename to src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/GitHubPasteService.java index fe7fafc4..f098e295 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/GithubPasteService.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/GitHubPasteService.java @@ -11,6 +11,7 @@ import java.io.OutputStreamWriter; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; @@ -64,14 +65,18 @@ public class GithubPasteService implements PasteService { try { URLConnection conn = url.openConnection(); conn.setDoOutput(true); - wr = new OutputStreamWriter(conn.getOutputStream()); + + // this isn't required, but is technically correct + conn.addRequestProperty("Content-Type", "application/json; charset=utf-8"); + + wr = new OutputStreamWriter(conn.getOutputStream(), StandardCharsets.UTF_8); wr.write(encodedData); wr.flush(); - rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; StringBuilder responseString = new StringBuilder(); - + // this has to be initialized AFTER the data has been flushed! + rd = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8)); while ((line = rd.readLine()) != null) { responseString.append(line); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/HttpAPIClient.java b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/HttpAPIClient.java index 55d69c5c..962fa356 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/HttpAPIClient.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/HttpAPIClient.java @@ -5,6 +5,7 @@ import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; +import java.nio.charset.StandardCharsets; /** * HTTP API-client. @@ -32,11 +33,11 @@ public abstract class HttpAPIClient { StringBuilder ret = new StringBuilder(); BufferedReader reader = null; try { - reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); + reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8)); while (!reader.ready()); // wait until reader is ready, may not be necessary, SUPPRESS CHECKSTYLE: EmptyStatement while (reader.ready()) { - ret.append(reader.readLine()).append('\n'); + ret.append(reader.readLine()).append(System.lineSeparator()); } } finally { if (reader != null) { diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/PastebinPasteService.java b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/PastebinPasteService.java index 7aeba797..c370c1d2 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/PastebinPasteService.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/webpaste/PastebinPasteService.java @@ -9,6 +9,7 @@ import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.Map; /** @@ -65,13 +66,18 @@ public class PastebinPasteService implements PasteService { try { URLConnection conn = url.openConnection(); conn.setDoOutput(true); - wr = new OutputStreamWriter(conn.getOutputStream()); + + // this isn't required, but is technically correct + conn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); + + wr = new OutputStreamWriter(conn.getOutputStream(), StandardCharsets.UTF_8); wr.write(encodedData); wr.flush(); - rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; String pastebinUrl = ""; + // this has to be initialized AFTER the data has been flushed! + rd = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8)); while ((line = rd.readLine()) != null) { pastebinUrl = line; }