Skip to content

Commit

Permalink
uses InputStreamByteArray instead of InputStream to handle client res…
Browse files Browse the repository at this point in the history
…ponse and fix the InputStream being closed
  • Loading branch information
rach-id committed Jan 15, 2021
1 parent 9af39ee commit 6d5b2fe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
29 changes: 16 additions & 13 deletions core/src/main/java/org/web3j/protocol/http/HttpService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
}
Expand Down

0 comments on commit 6d5b2fe

Please sign in to comment.