From 6d5b2feb7ba96f71dc02f4f02e177d71ca474676 Mon Sep 17 00:00:00 2001 From: rachid chami Date: Fri, 15 Jan 2021 17:35:38 +0100 Subject: [PATCH] uses InputStreamByteArray instead of InputStream to handle client response and fix the InputStream being closed --- .../org/web3j/protocol/http/HttpService.java | 29 ++++++++++--------- .../java/org/web3j/protocol/geth/GethIT.java | 5 ++-- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/core/src/main/java/org/web3j/protocol/http/HttpService.java b/core/src/main/java/org/web3j/protocol/http/HttpService.java index 7a38b23f4..65446f5a7 100644 --- a/core/src/main/java/org/web3j/protocol/http/HttpService.java +++ b/core/src/main/java/org/web3j/protocol/http/HttpService.java @@ -13,6 +13,7 @@ package org.web3j.protocol.http; import java.io.BufferedInputStream; +import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Arrays; @@ -157,20 +158,22 @@ protected InputStream performIO(String request) throws IOException { okhttp3.Request httpRequest = new okhttp3.Request.Builder().url(url).headers(headers).post(requestBody).build(); - okhttp3.Response response = httpClient.newCall(httpRequest).execute(); - processHeaders(response.headers()); - ResponseBody responseBody = response.body(); - if (response.isSuccessful()) { - if (responseBody != null) { - return buildInputStream(responseBody); + try (okhttp3.Response response = httpClient.newCall(httpRequest).execute()) { + processHeaders(response.headers()); + ResponseBody responseBody = response.body(); + if (response.isSuccessful()) { + if (responseBody != null) { + return buildInputStream(responseBody); + } else { + return null; + } } else { - return null; - } - } else { - int code = response.code(); - String text = responseBody == null ? "N/A" : responseBody.string(); + int code = response.code(); + String text = responseBody == null ? "N/A" : responseBody.string(); - throw new ClientConnectionException("Invalid response received: " + code + "; " + text); + throw new ClientConnectionException( + "Invalid response received: " + code + "; " + text); + } } } @@ -179,7 +182,7 @@ protected void processHeaders(Headers headers) { } private InputStream buildInputStream(ResponseBody responseBody) throws IOException { - InputStream inputStream = responseBody.byteStream(); + InputStream inputStream = new ByteArrayInputStream(responseBody.bytes()); if (includeRawResponse) { // we have to buffer the entire input payload, so that after processing diff --git a/integration-tests/src/test/java/org/web3j/protocol/geth/GethIT.java b/integration-tests/src/test/java/org/web3j/protocol/geth/GethIT.java index e1361392a..bc57bc87f 100644 --- a/integration-tests/src/test/java/org/web3j/protocol/geth/GethIT.java +++ b/integration-tests/src/test/java/org/web3j/protocol/geth/GethIT.java @@ -32,13 +32,12 @@ public class GethIT { private Geth web3j; - private String url = "http://localhost:8545"; public GethIT() {} @BeforeEach public void setUp() { - this.web3j = Geth.build(new HttpService(url)); + this.web3j = Geth.build(new HttpService()); } @Disabled @@ -50,7 +49,7 @@ public void testWeb3ClientVersion() throws Exception { @Test public void testPersonalAccountCreation() throws IOException { - Admin admin = Admin.build(new HttpService(url)); + Admin admin = Admin.build(new HttpService()); NewAccountIdentifier accountId = admin.personalNewAccount("web3j-geth-IT").send(); assertFalse(accountId.getResult().isEmpty()); }