|
19 | 19 |
|
20 | 20 | import java.io.BufferedInputStream;
|
21 | 21 | import java.io.ByteArrayInputStream;
|
22 |
| -import java.io.ByteArrayOutputStream; |
23 | 22 | import java.io.File;
|
24 | 23 | import java.io.IOException;
|
25 | 24 | import java.io.InputStream;
|
|
64 | 63 | import org.slf4j.Logger;
|
65 | 64 | import org.slf4j.LoggerFactory;
|
66 | 65 |
|
| 66 | +import org.apache.hbase.thirdparty.com.google.common.io.ByteStreams; |
| 67 | +import org.apache.hbase.thirdparty.com.google.common.io.Closeables; |
| 68 | + |
67 | 69 | /**
|
68 | 70 | * A wrapper around HttpClient which provides some useful function and semantics for interacting
|
69 | 71 | * with the REST gateway.
|
@@ -491,29 +493,30 @@ public Response get(String path, Header[] headers) throws IOException {
|
491 | 493 | * @return The response body, null if body is empty
|
492 | 494 | * @throws IOException If an I/O (transport) problem occurs while obtaining the response body.
|
493 | 495 | */
|
494 |
| - @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "NP_LOAD_OF_KNOWN_NULL_VALUE", |
495 |
| - justification = "null is possible return value") |
496 | 496 | public static byte[] getResponseBody(HttpResponse resp) throws IOException {
|
497 |
| - if (resp.getEntity() == null) return null; |
498 |
| - try (InputStream instream = resp.getEntity().getContent()) { |
499 |
| - if (instream != null) { |
500 |
| - long contentLength = resp.getEntity().getContentLength(); |
501 |
| - if (contentLength > Integer.MAX_VALUE) { |
502 |
| - // guard integer cast from overflow |
503 |
| - throw new IOException("Content too large to be buffered: " + contentLength + " bytes"); |
504 |
| - } |
505 |
| - ByteArrayOutputStream outstream = |
506 |
| - new ByteArrayOutputStream(contentLength > 0 ? (int) contentLength : 4 * 1024); |
507 |
| - byte[] buffer = new byte[4096]; |
508 |
| - int len; |
509 |
| - while ((len = instream.read(buffer)) > 0) { |
510 |
| - outstream.write(buffer, 0, len); |
511 |
| - } |
512 |
| - outstream.close(); |
513 |
| - return outstream.toByteArray(); |
514 |
| - } |
| 497 | + if (resp.getEntity() == null) { |
| 498 | + return null; |
| 499 | + } |
| 500 | + InputStream instream = resp.getEntity().getContent(); |
| 501 | + if (instream == null) { |
515 | 502 | return null;
|
516 | 503 | }
|
| 504 | + try { |
| 505 | + long contentLength = resp.getEntity().getContentLength(); |
| 506 | + if (contentLength > Integer.MAX_VALUE) { |
| 507 | + // guard integer cast from overflow |
| 508 | + throw new IOException("Content too large to be buffered: " + contentLength + " bytes"); |
| 509 | + } |
| 510 | + if (contentLength > 0) { |
| 511 | + byte[] content = new byte[(int) contentLength]; |
| 512 | + ByteStreams.readFully(instream, content); |
| 513 | + return content; |
| 514 | + } else { |
| 515 | + return ByteStreams.toByteArray(instream); |
| 516 | + } |
| 517 | + } finally { |
| 518 | + Closeables.closeQuietly(instream); |
| 519 | + } |
517 | 520 | }
|
518 | 521 |
|
519 | 522 | /**
|
|
0 commit comments