mirror of
https://github.com/songoda/SongodaCore.git
synced 2024-11-26 20:16:18 +01:00
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:
parent
5f02c06ce8
commit
d0abbf26ec
@ -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();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user