make pasting more system agnostic

This commit is contained in:
Kermina Awad 2020-06-06 20:16:42 -04:00
parent e821611744
commit 676c3a2e3d
3 changed files with 19 additions and 7 deletions

View File

@ -11,6 +11,7 @@ import java.io.OutputStreamWriter;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -64,14 +65,18 @@ public class GithubPasteService implements PasteService {
try { try {
URLConnection conn = url.openConnection(); URLConnection conn = url.openConnection();
conn.setDoOutput(true); 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.write(encodedData);
wr.flush(); wr.flush();
rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line; String line;
StringBuilder responseString = new StringBuilder(); 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) { while ((line = rd.readLine()) != null) {
responseString.append(line); responseString.append(line);
} }

View File

@ -5,6 +5,7 @@ import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
/** /**
* HTTP API-client. * HTTP API-client.
@ -32,11 +33,11 @@ public abstract class HttpAPIClient {
StringBuilder ret = new StringBuilder(); StringBuilder ret = new StringBuilder();
BufferedReader reader = null; BufferedReader reader = null;
try { 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()); // wait until reader is ready, may not be necessary, SUPPRESS CHECKSTYLE: EmptyStatement
while (reader.ready()) { while (reader.ready()) {
ret.append(reader.readLine()).append('\n'); ret.append(reader.readLine()).append(System.lineSeparator());
} }
} finally { } finally {
if (reader != null) { if (reader != null) {

View File

@ -9,6 +9,7 @@ import java.net.MalformedURLException;
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;
import java.util.Map; import java.util.Map;
/** /**
@ -65,13 +66,18 @@ public class PastebinPasteService implements PasteService {
try { try {
URLConnection conn = url.openConnection(); URLConnection conn = url.openConnection();
conn.setDoOutput(true); 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.write(encodedData);
wr.flush(); wr.flush();
rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line; String line;
String pastebinUrl = ""; 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) { while ((line = rd.readLine()) != null) {
pastebinUrl = line; pastebinUrl = line;
} }