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.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);
}

View File

@ -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) {

View File

@ -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;
}