Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit c8c9554

Browse files
committed
Replace ByteStreams with NIO methods
1 parent 5a00863 commit c8c9554

File tree

10 files changed

+56
-62
lines changed

10 files changed

+56
-62
lines changed

discovery/src/main/java/com/proofpoint/discovery/client/HttpDiscoveryLookupClient.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package com.proofpoint.discovery.client;
1717

18-
import com.google.common.io.ByteStreams;
1918
import com.google.common.net.HttpHeaders;
2019
import com.google.common.util.concurrent.ListenableFuture;
2120
import com.proofpoint.http.client.CacheControl;
@@ -32,6 +31,7 @@
3231
import org.weakref.jmx.Flatten;
3332

3433
import java.io.IOException;
34+
import java.io.InputStream;
3535
import java.net.URI;
3636
import java.util.concurrent.CancellationException;
3737
import java.util.concurrent.TimeUnit;
@@ -128,8 +128,8 @@ public ServiceDescriptors handle(Request request, Response response)
128128
}
129129

130130
byte[] json;
131-
try {
132-
json = ByteStreams.toByteArray(response.getInputStream());
131+
try (InputStream stream = response.getInputStream()) {
132+
json = stream.readAllBytes();
133133
}
134134
catch (IOException e) {
135135
throw new DiscoveryException(format("Lookup of %s failed", type), e);

http-client/src/main/java/com/proofpoint/http/client/DefaultingJsonResponseHandler.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
package com.proofpoint.http.client;
1717

1818
import com.google.common.collect.ImmutableSet;
19-
import com.google.common.io.ByteStreams;
2019
import com.google.common.net.MediaType;
2120
import com.google.common.primitives.Ints;
2221
import com.proofpoint.json.JsonCodec;
2322

23+
import java.io.InputStream;
2424
import java.util.Set;
2525

2626
import static com.google.common.net.HttpHeaders.CONTENT_TYPE;
@@ -72,9 +72,9 @@ public T handle(Request request, Response response)
7272
if (contentType != null && !MediaType.parse(contentType).is(MEDIA_TYPE_JSON)) {
7373
return defaultValue;
7474
}
75-
try {
76-
return jsonCodec.fromJson(ByteStreams.toByteArray(response.getInputStream()));
77-
}
75+
try (InputStream inputStream = response.getInputStream()) {
76+
return jsonCodec.fromJson(inputStream.readAllBytes());
77+
}
7878
catch (Exception e) {
7979
return defaultValue;
8080
}

http-client/src/main/java/com/proofpoint/http/client/FullJsonResponseHandler.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,19 @@
1717

1818
import com.google.common.collect.ImmutableListMultimap;
1919
import com.google.common.collect.ListMultimap;
20-
import com.google.common.io.ByteStreams;
2120
import com.google.common.net.MediaType;
2221
import com.proofpoint.http.client.FullJsonResponseHandler.JsonResponse;
2322
import com.proofpoint.json.JsonCodec;
2423
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2524
import jakarta.annotation.Nullable;
2625

27-
import java.io.IOException;
2826
import java.nio.charset.Charset;
2927
import java.util.List;
3028

3129
import static com.google.common.base.MoreObjects.toStringHelper;
3230
import static com.google.common.net.HttpHeaders.CONTENT_TYPE;
3331
import static com.proofpoint.http.client.ResponseHandlerUtils.propagate;
32+
import static com.proofpoint.http.client.ResponseHandlerUtils.readResponseBytes;
3433
import static java.lang.String.format;
3534
import static java.nio.charset.StandardCharsets.UTF_8;
3635
import static java.util.Objects.requireNonNull;
@@ -61,24 +60,14 @@ public JsonResponse<T> handleException(Request request, Exception exception)
6160
@Override
6261
public JsonResponse<T> handle(Request request, Response response)
6362
{
64-
byte[] bytes = readResponseBytes(response);
63+
byte[] bytes = readResponseBytes(request, response);
6564
String contentType = response.getHeader(CONTENT_TYPE);
6665
if ((contentType == null) || !MediaType.parse(contentType).is(MEDIA_TYPE_JSON)) {
6766
return new JsonResponse<>(response.getStatusCode(), response.getStatusMessage(), response.getHeaders(), bytes);
6867
}
6968
return new JsonResponse<>(response.getStatusCode(), response.getStatusMessage(), response.getHeaders(), jsonCodec, bytes);
7069
}
7170

72-
private static byte[] readResponseBytes(Response response)
73-
{
74-
try {
75-
return ByteStreams.toByteArray(response.getInputStream());
76-
}
77-
catch (IOException e) {
78-
throw new RuntimeException("Error reading response from server", e);
79-
}
80-
}
81-
8271
public static class JsonResponse<T>
8372
{
8473
private final int statusCode;

http-client/src/main/java/com/proofpoint/http/client/JsonResponseHandler.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,16 @@
1616
package com.proofpoint.http.client;
1717

1818
import com.google.common.collect.ImmutableSet;
19-
import com.google.common.io.ByteStreams;
2019
import com.google.common.net.MediaType;
2120
import com.google.common.primitives.Ints;
2221
import com.proofpoint.json.JsonCodec;
2322
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2423

25-
import java.io.IOException;
2624
import java.util.Set;
2725

2826
import static com.google.common.net.HttpHeaders.CONTENT_TYPE;
2927
import static com.proofpoint.http.client.ResponseHandlerUtils.propagate;
28+
import static com.proofpoint.http.client.ResponseHandlerUtils.readResponseBytes;
3029
import static java.nio.charset.StandardCharsets.UTF_8;
3130

3231
public class JsonResponseHandler<T> implements ResponseHandler<T, RuntimeException>
@@ -80,13 +79,9 @@ public T handle(Request request, Response response)
8079
if (!MediaType.parse(contentType).is(MEDIA_TYPE_JSON)) {
8180
throw new UnexpectedResponseException("Expected application/json response from server but got " + contentType, request, response);
8281
}
83-
byte[] bytes;
84-
try {
85-
bytes = ByteStreams.toByteArray(response.getInputStream());
86-
}
87-
catch (IOException e) {
88-
throw new RuntimeException("Error reading JSON response from server", e);
89-
}
82+
83+
byte[] bytes = readResponseBytes(request, response);
84+
9085
try {
9186
return jsonCodec.fromJson(bytes);
9287
}

http-client/src/main/java/com/proofpoint/http/client/ResponseHandlerUtils.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,19 @@ public static RuntimeException propagate(Request request, Throwable exception)
2424
throwIfUnchecked(exception);
2525
throw new RuntimeException(exception);
2626
}
27+
28+
public static byte[] readResponseBytes(Request request, Response response)
29+
{
30+
try {
31+
return response.getInputStream().readAllBytes();
32+
}
33+
catch (IOException e) {
34+
throw new UncheckedIOException("Failed reading response from server: " + urlFor(request), e);
35+
}
36+
}
37+
38+
private static String urlFor(Request request)
39+
{
40+
return request.getUri().toASCIIString();
41+
}
2742
}

http-client/src/main/java/com/proofpoint/http/client/StringResponseHandler.java

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,17 @@
1717

1818
import com.google.common.collect.ImmutableListMultimap;
1919
import com.google.common.collect.ListMultimap;
20-
import com.google.common.io.ByteStreams;
2120
import com.google.common.net.MediaType;
2221
import com.proofpoint.http.client.StringResponseHandler.StringResponse;
2322
import jakarta.annotation.Nullable;
2423

25-
import java.io.IOException;
26-
import java.io.UncheckedIOException;
24+
import java.nio.charset.Charset;
2725
import java.util.List;
26+
import java.util.Optional;
2827

29-
import static com.google.common.io.ByteStreams.toByteArray;
3028
import static com.google.common.net.HttpHeaders.CONTENT_TYPE;
3129
import static com.proofpoint.http.client.ResponseHandlerUtils.propagate;
30+
import static com.proofpoint.http.client.ResponseHandlerUtils.readResponseBytes;
3231
import static java.nio.charset.StandardCharsets.UTF_8;
3332

3433
public class StringResponseHandler implements ResponseHandler<StringResponse, RuntimeException>
@@ -53,27 +52,18 @@ public StringResponse handleException(Request request, Exception exception)
5352
@Override
5453
public StringResponse handle(Request request, Response response)
5554
{
56-
try {
57-
String contentType = response.getHeader(CONTENT_TYPE);
58-
59-
if (contentType != null) {
60-
MediaType mediaType = MediaType.parse(contentType);
61-
return new StringResponse(
62-
response.getStatusCode(),
63-
response.getStatusMessage(),
64-
response.getHeaders(),
65-
new String(ByteStreams.toByteArray(response.getInputStream()), mediaType.charset().or(UTF_8)));
66-
}
67-
68-
return new StringResponse(
69-
response.getStatusCode(),
70-
response.getStatusMessage(),
71-
response.getHeaders(),
72-
new String(toByteArray(response.getInputStream()), UTF_8));
73-
}
74-
catch (IOException e) {
75-
throw new UncheckedIOException(e);
76-
}
55+
byte[] bytes = readResponseBytes(request, response);
56+
57+
Charset charset = Optional.ofNullable(response.getHeader(CONTENT_TYPE))
58+
.map(MediaType::parse)
59+
.flatMap(mediaType -> mediaType.charset().toJavaUtil())
60+
.orElse(UTF_8);
61+
62+
return new StringResponse(
63+
response.getStatusCode(),
64+
response.getStatusMessage(),
65+
response.getHeaders(),
66+
new String(bytes, charset));
7767
}
7868

7969
public static class StringResponse

http-client/src/test/java/com/proofpoint/http/client/EchoServlet.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
import com.google.common.collect.ArrayListMultimap;
1919
import com.google.common.collect.ImmutableListMultimap;
2020
import com.google.common.collect.ListMultimap;
21-
import com.google.common.io.ByteStreams;
2221
import jakarta.servlet.http.HttpServlet;
2322
import jakarta.servlet.http.HttpServletRequest;
2423
import jakarta.servlet.http.HttpServletResponse;
2524

2625
import java.io.IOException;
26+
import java.io.InputStream;
2727
import java.net.URI;
2828
import java.util.Collections;
2929
import java.util.List;
@@ -61,7 +61,9 @@ protected void service(HttpServletRequest request, HttpServletResponse response)
6161
requestHeaders.putAll(HeaderName.of(name), Collections.list(request.getHeaders(name)));
6262
}
6363

64-
requestBytes = ByteStreams.toByteArray(request.getInputStream());
64+
try (InputStream inputStream = request.getInputStream()) {
65+
requestBytes = inputStream.readAllBytes();
66+
}
6567

6668
if (responseStatusMessage != null) {
6769
response.sendError(responseStatusCode, responseStatusMessage);

http-client/src/test/java/com/proofpoint/http/client/TestFullJsonResponseHandler.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88

99
import java.io.IOException;
1010
import java.io.InputStream;
11+
import java.net.URI;
1112
import java.nio.charset.StandardCharsets;
1213

1314
import static com.google.common.net.MediaType.JSON_UTF_8;
1415
import static com.google.common.net.MediaType.PLAIN_TEXT_UTF_8;
1516
import static com.proofpoint.http.client.FullJsonResponseHandler.JsonResponse;
1617
import static com.proofpoint.http.client.FullJsonResponseHandler.createFullJsonResponseHandler;
1718
import static com.proofpoint.http.client.HttpStatus.INTERNAL_SERVER_ERROR;
19+
import static com.proofpoint.http.client.Request.Builder.prepareGet;
1820
import static com.proofpoint.http.client.testing.TestingResponse.mockResponse;
1921
import static com.proofpoint.testing.Assertions.assertInstanceOf;
2022
import static java.lang.String.format;
@@ -161,14 +163,14 @@ public void testJsonReadException()
161163
when(inputStream.read(any(byte[].class), anyInt(), anyInt())).thenThrow(expectedException);
162164

163165
try {
164-
handler.handle(null, mockResponse()
166+
handler.handle(prepareGet().setUri(URI.create("https://invalid.invalid/test")).build(), mockResponse()
165167
.contentType(JSON_UTF_8)
166168
.body(inputStream)
167169
.build());
168170
fail("expected exception");
169171
}
170172
catch (RuntimeException e) {
171-
assertEquals(e.getMessage(), "Error reading response from server");
173+
assertEquals(e.getMessage(), "Failed reading response from server: https://invalid.invalid/test");
172174
assertSame(e.getCause(), expectedException);
173175
}
174176
}

http-client/src/test/java/com/proofpoint/http/client/TestJsonResponseHandler.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66

77
import java.io.IOException;
88
import java.io.InputStream;
9+
import java.net.URI;
910

1011
import static com.google.common.net.MediaType.JSON_UTF_8;
1112
import static com.google.common.net.MediaType.PLAIN_TEXT_UTF_8;
1213
import static com.proofpoint.http.client.HttpStatus.INTERNAL_SERVER_ERROR;
1314
import static com.proofpoint.http.client.JsonResponseHandler.createJsonResponseHandler;
15+
import static com.proofpoint.http.client.Request.Builder.prepareGet;
1416
import static com.proofpoint.http.client.TestFullJsonResponseHandler.User;
1517
import static com.proofpoint.http.client.testing.TestingResponse.mockResponse;
1618
import static com.proofpoint.testing.Assertions.assertInstanceOf;
@@ -96,14 +98,14 @@ public void testJsonReadException()
9698
when(inputStream.read(any(byte[].class), anyInt(), anyInt())).thenThrow(expectedException);
9799

98100
try {
99-
handler.handle(null, mockResponse()
101+
handler.handle(prepareGet().setUri(URI.create("https://invalid.invalid/test")).build(), mockResponse()
100102
.contentType(JSON_UTF_8)
101103
.body(inputStream)
102104
.build());
103105
fail("expected exception");
104106
}
105107
catch (RuntimeException e) {
106-
assertEquals(e.getMessage(), "Error reading JSON response from server");
108+
assertEquals(e.getMessage(), "Failed reading response from server: https://invalid.invalid/test");
107109
assertSame(e.getCause(), expectedException);
108110
}
109111
}

http-client/src/test/java/com/proofpoint/http/client/testing/TestTestingResponse.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.util.Set;
2020
import java.util.function.Function;
2121

22-
import static com.google.common.io.ByteStreams.toByteArray;
2322
import static com.proofpoint.http.client.testing.TestingResponse.mockResponse;
2423
import static java.nio.charset.StandardCharsets.UTF_8;
2524
import static org.testng.Assert.assertEquals;
@@ -218,7 +217,7 @@ private static void assertResponse(Response response, HttpStatus status, Immutab
218217
}
219218
assertEquals(response.getHeaders(), builder.build());
220219
try {
221-
assertEquals(toByteArray(response.getInputStream()), body);
220+
assertEquals(response.getInputStream().readAllBytes(), body);
222221
}
223222
catch (IOException e) {
224223
throw new UncheckedIOException(e);

0 commit comments

Comments
 (0)