Don't use Apache commons-io to read HTTP response body bytes

At least in Spigot 1.19 it is not available by default - That much we can do ourselves in that case.
This commit is contained in:
Christian Koop 2023-06-09 14:53:08 +02:00
parent 5f02c06ce8
commit d0abbf26ec
No known key found for this signature in database
GPG Key ID: 89A8181384E010A3

View File

@ -1,7 +1,6 @@
package com.songoda.core.http;
import org.apache.commons.io.IOUtils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
@ -30,14 +29,16 @@ public class HttpResponseImpl implements HttpResponse, AutoCloseable {
public byte[] getBody() throws IOException {
if (this.body == null) {
try (InputStream in = this.connection.getInputStream();
InputStream err = this.connection.getErrorStream()) {
try (InputStream err = this.connection.getErrorStream()) {
if (err != null) {
this.body = IOUtils.toByteArray(err);
} else {
this.body = IOUtils.toByteArray(in);
this.body = toByteArray(err);
return this.body;
}
}
try (InputStream in = this.connection.getInputStream()) {
this.body = toByteArray(in);
}
}
return this.body;
@ -51,4 +52,14 @@ public class HttpResponseImpl implements HttpResponse, AutoCloseable {
public void close() throws Exception {
this.connection.disconnect();
}
private byte[] toByteArray(InputStream in) throws IOException {
byte[] buffer = new byte[8192];
int bytesRead;
ByteArrayOutputStream output = new ByteArrayOutputStream();
while ((bytesRead = in.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
return output.toByteArray();
}
}