From d0abbf26ec2d5005d6e577851915f0150c6fcaed Mon Sep 17 00:00:00 2001 From: Christian Koop Date: Fri, 9 Jun 2023 14:53:08 +0200 Subject: [PATCH] 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. --- .../songoda/core/http/HttpResponseImpl.java | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/Core/src/main/java/com/songoda/core/http/HttpResponseImpl.java b/Core/src/main/java/com/songoda/core/http/HttpResponseImpl.java index f713b98d..f410e4e6 100644 --- a/Core/src/main/java/com/songoda/core/http/HttpResponseImpl.java +++ b/Core/src/main/java/com/songoda/core/http/HttpResponseImpl.java @@ -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(); + } }