Skip to content

Commit aa8ca7e

Browse files
committed
HBASE-27473 Fix spotbugs warnings in hbase-rest Client.getResponseBody (#4867)
Signed-off-by: Xin Sun <ddupgs@gmail.com> (cherry picked from commit 2d87994)
1 parent 4605c32 commit aa8ca7e

File tree

1 file changed

+24
-21
lines changed
  • hbase-rest/src/main/java/org/apache/hadoop/hbase/rest/client

1 file changed

+24
-21
lines changed

hbase-rest/src/main/java/org/apache/hadoop/hbase/rest/client/Client.java

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import java.io.BufferedInputStream;
2121
import java.io.ByteArrayInputStream;
22-
import java.io.ByteArrayOutputStream;
2322
import java.io.File;
2423
import java.io.IOException;
2524
import java.io.InputStream;
@@ -64,6 +63,9 @@
6463
import org.slf4j.Logger;
6564
import org.slf4j.LoggerFactory;
6665

66+
import org.apache.hbase.thirdparty.com.google.common.io.ByteStreams;
67+
import org.apache.hbase.thirdparty.com.google.common.io.Closeables;
68+
6769
/**
6870
* A wrapper around HttpClient which provides some useful function and semantics for interacting
6971
* with the REST gateway.
@@ -491,29 +493,30 @@ public Response get(String path, Header[] headers) throws IOException {
491493
* @return The response body, null if body is empty
492494
* @throws IOException If an I/O (transport) problem occurs while obtaining the response body.
493495
*/
494-
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "NP_LOAD_OF_KNOWN_NULL_VALUE",
495-
justification = "null is possible return value")
496496
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) {
515502
return null;
516503
}
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+
}
517520
}
518521

519522
/**

0 commit comments

Comments
 (0)