HBASE-27473 Fix spotbugs warnings in hbase-rest Client.getResponseBody (#4867)
Signed-off-by: Xin Sun <ddupgs@gmail.com>
This commit is contained in:
parent
b1a648ffea
commit
2d87994f68
|
@ -19,7 +19,6 @@ package org.apache.hadoop.hbase.rest.client;
|
||||||
|
|
||||||
import java.io.BufferedInputStream;
|
import java.io.BufferedInputStream;
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
@ -65,6 +64,9 @@ import org.apache.yetus.audience.InterfaceAudience;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import org.apache.hbase.thirdparty.com.google.common.io.ByteStreams;
|
||||||
|
import org.apache.hbase.thirdparty.com.google.common.io.Closeables;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A wrapper around HttpClient which provides some useful function and semantics for interacting
|
* A wrapper around HttpClient which provides some useful function and semantics for interacting
|
||||||
* with the REST gateway.
|
* with the REST gateway.
|
||||||
|
@ -492,28 +494,29 @@ public class Client {
|
||||||
* @return The response body, null if body is empty
|
* @return The response body, null if body is empty
|
||||||
* @throws IOException If an I/O (transport) problem occurs while obtaining the response body.
|
* @throws IOException If an I/O (transport) problem occurs while obtaining the response body.
|
||||||
*/
|
*/
|
||||||
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "NP_LOAD_OF_KNOWN_NULL_VALUE",
|
|
||||||
justification = "null is possible return value")
|
|
||||||
public static byte[] getResponseBody(HttpResponse resp) throws IOException {
|
public static byte[] getResponseBody(HttpResponse resp) throws IOException {
|
||||||
if (resp.getEntity() == null) return null;
|
if (resp.getEntity() == null) {
|
||||||
try (InputStream instream = resp.getEntity().getContent()) {
|
return null;
|
||||||
if (instream != null) {
|
}
|
||||||
|
InputStream instream = resp.getEntity().getContent();
|
||||||
|
if (instream == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
try {
|
||||||
long contentLength = resp.getEntity().getContentLength();
|
long contentLength = resp.getEntity().getContentLength();
|
||||||
if (contentLength > Integer.MAX_VALUE) {
|
if (contentLength > Integer.MAX_VALUE) {
|
||||||
// guard integer cast from overflow
|
// guard integer cast from overflow
|
||||||
throw new IOException("Content too large to be buffered: " + contentLength + " bytes");
|
throw new IOException("Content too large to be buffered: " + contentLength + " bytes");
|
||||||
}
|
}
|
||||||
ByteArrayOutputStream outstream =
|
if (contentLength > 0) {
|
||||||
new ByteArrayOutputStream(contentLength > 0 ? (int) contentLength : 4 * 1024);
|
byte[] content = new byte[(int) contentLength];
|
||||||
byte[] buffer = new byte[4096];
|
ByteStreams.readFully(instream, content);
|
||||||
int len;
|
return content;
|
||||||
while ((len = instream.read(buffer)) > 0) {
|
} else {
|
||||||
outstream.write(buffer, 0, len);
|
return ByteStreams.toByteArray(instream);
|
||||||
}
|
}
|
||||||
outstream.close();
|
} finally {
|
||||||
return outstream.toByteArray();
|
Closeables.closeQuietly(instream);
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue