@@ -545,14 +545,7 @@ private byte[] responseBodyBytes(final URLConnection connection) throws RestExce
545545 inputStream = httpURLConnection .getErrorStream ();
546546 }
547547 }
548- final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream ();
549- int bytesRead ;
550- final byte [] bytes = new byte [16384 ];
551- while ((bytesRead = inputStream .read (bytes , 0 , bytes .length )) != -1 ) {
552- byteArrayOutputStream .write (bytes , 0 , bytesRead );
553- }
554- byteArrayOutputStream .flush ();
555- return byteArrayOutputStream .toByteArray ();
548+ return handleResponseInputStream (inputStream );
556549 } catch (IOException e ) {
557550 return new byte [0 ];
558551 }
@@ -586,4 +579,31 @@ private int connectionStatus(final URLConnection connection) throws IOException,
586579 return statusCode ;
587580 }
588581
582+ /**
583+ * <p>This method handles the response stream from the connection.</p>
584+ *
585+ * @param inputStream The input stream from the connection.
586+ * @return The body payload, downloaded from the HTTP connection response
587+ */
588+ protected byte [] handleResponseInputStream (final InputStream inputStream ) {
589+ try {
590+ // getErrorStream() can return null so handle it.
591+ if (inputStream != null ) {
592+ final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream ();
593+
594+ int bytesRead ;
595+ final byte [] bytes = new byte [16384 ];
596+ while ((bytesRead = inputStream .read (bytes , 0 , bytes .length )) != -1 ) {
597+ byteArrayOutputStream .write (bytes , 0 , bytesRead );
598+ }
599+
600+ byteArrayOutputStream .flush ();
601+ return byteArrayOutputStream .toByteArray ();
602+ } else {
603+ return new byte [0 ];
604+ }
605+ } catch (IOException e ) {
606+ return new byte [0 ];
607+ }
608+ }
589609}
0 commit comments