From 02a2e8d81d7516f4ed0b3fa2310092679f474af2 Mon Sep 17 00:00:00 2001 From: MER-C Date: Sun, 23 Jan 2022 11:25:02 +0000 Subject: [PATCH] Fix uploading on Java 17. Frankly I have no idea why this originally worked. Closes #186. --- src/org/wikipedia/Wiki.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/org/wikipedia/Wiki.java b/src/org/wikipedia/Wiki.java index fd925b42..6b864b80 100644 --- a/src/org/wikipedia/Wiki.java +++ b/src/org/wikipedia/Wiki.java @@ -8128,7 +8128,7 @@ public String makeApiCall(Map getparams, Map pos boolean isPOST = (postparams != null && !postparams.isEmpty()); StringBuilder stringPostBody = new StringBuilder(); boolean multipart = false; - ArrayList multipartPostBody = new ArrayList<>(); + ByteArrayOutputStream multipartPostBody = new ByteArrayOutputStream(); String boundary = "----------NEXT PART----------"; if (isPOST) { @@ -8150,18 +8150,18 @@ public String makeApiCall(Map getparams, Map pos for (Map.Entry entry : postparams.entrySet()) { Object value = entry.getValue(); - multipartPostBody.add((nextpart + entry.getKey() + "\"").getBytes(StandardCharsets.UTF_8)); + multipartPostBody.write((nextpart + entry.getKey() + "\"").getBytes(StandardCharsets.UTF_8)); if (value instanceof String) - multipartPostBody.add(("Content-Type: text/plain; charset=UTF-8\r\n\r\n" + (String)value + "\r\n") + multipartPostBody.write(("Content-Type: text/plain; charset=UTF-8\r\n\r\n" + (String)value + "\r\n") .getBytes(StandardCharsets.UTF_8)); else if (value instanceof byte[]) { - multipartPostBody.add("Content-Type: application/octet-stream\r\n\r\n".getBytes(StandardCharsets.UTF_8)); - multipartPostBody.add((byte[])value); - multipartPostBody.add("\r\n".getBytes(StandardCharsets.UTF_8)); + multipartPostBody.write("Content-Type: application/octet-stream\r\n\r\n".getBytes(StandardCharsets.UTF_8)); + multipartPostBody.write((byte[])value); + multipartPostBody.write("\r\n".getBytes(StandardCharsets.UTF_8)); } } - multipartPostBody.add((boundary + "--\r\n").getBytes(StandardCharsets.UTF_8)); + multipartPostBody.write((boundary + "--\r\n").getBytes(StandardCharsets.UTF_8)); } else { @@ -8189,7 +8189,7 @@ else if (value instanceof byte[]) if (isPOST) { if (multipart) - connection = connection.POST(HttpRequest.BodyPublishers.ofByteArrays(multipartPostBody)) + connection = connection.POST(HttpRequest.BodyPublishers.ofByteArray(multipartPostBody.toByteArray())) .header("Content-Type", "multipart/form-data; boundary=" + boundary); else connection = connection.POST(HttpRequest.BodyPublishers.ofString(stringPostBody.toString()))