From d65c09ea24592a5942a51f674ecaf1f74c6dcc9f Mon Sep 17 00:00:00 2001 From: Bryce Chester Newman Date: Mon, 24 Apr 2023 08:17:20 -0600 Subject: [PATCH] httpURLConnection.getErrorStream() can return null so handle it. --- .../io/github/jopenlibs/vault/rest/Rest.java | 41 +++++++++++++------ .../github/jopenlibs/vault/rest/GetTests.java | 11 +++++ 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src/main/java/io/github/jopenlibs/vault/rest/Rest.java b/src/main/java/io/github/jopenlibs/vault/rest/Rest.java index 75680ebe..ccd04763 100644 --- a/src/main/java/io/github/jopenlibs/vault/rest/Rest.java +++ b/src/main/java/io/github/jopenlibs/vault/rest/Rest.java @@ -545,17 +545,7 @@ private byte[] responseBodyBytes(final URLConnection connection) throws RestExce inputStream = httpURLConnection.getErrorStream(); } } - final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - // getErrorStream() can return null so handle it. - if(inputStream != null) { - int bytesRead; - final byte[] bytes = new byte[16384]; - while ((bytesRead = inputStream.read(bytes, 0, bytes.length)) != -1) { - byteArrayOutputStream.write(bytes, 0, bytesRead); - } - } - byteArrayOutputStream.flush(); - return byteArrayOutputStream.toByteArray(); + return handleResponseInputStream(inputStream); } catch (IOException e) { return new byte[0]; } @@ -589,4 +579,31 @@ private int connectionStatus(final URLConnection connection) throws IOException, return statusCode; } -} + /** + *

This method

+ * + * @param inputStream The input stream from the connection. + * @return The body payload, downloaded from the HTTP connection response + */ + protected byte[] handleResponseInputStream(final InputStream inputStream) { + try { + // getErrorStream() can return null so handle it. + if (inputStream != null) { + final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + + int bytesRead; + final byte[] bytes = new byte[16384]; + while ((bytesRead = inputStream.read(bytes, 0, bytes.length)) != -1) { + byteArrayOutputStream.write(bytes, 0, bytesRead); + } + + byteArrayOutputStream.flush(); + return byteArrayOutputStream.toByteArray(); + } else { + return new byte[0]; + } + } catch (IOException e) { + return new byte[0]; + } + } +} \ No newline at end of file diff --git a/src/test/java/io/github/jopenlibs/vault/rest/GetTests.java b/src/test/java/io/github/jopenlibs/vault/rest/GetTests.java index fc77f561..cc95f61d 100644 --- a/src/test/java/io/github/jopenlibs/vault/rest/GetTests.java +++ b/src/test/java/io/github/jopenlibs/vault/rest/GetTests.java @@ -192,4 +192,15 @@ public void testGet_RetrievesResponseBodyWhenStatusIs418() throws RestException responseBody.contains("User-Agent")); } + + /** + *

Verify that response body does not cause NPE when input stream is null.

+ */ + @Test + public void test_handleResponseInputStream() { + final Rest rest = new Rest(); + final byte[] result = rest.handleResponseInputStream(null); + assertEquals(0, result.length); + } + }