@@ -86,8 +86,9 @@ public String header(String name) {
8686 *
8787 * @return the response body
8888 * @throws IOException
89- * if an I/O Exception occurs.
89+ * if response stream is null or an I/O Exception occurs.
9090 */
91+ @ Nonnull
9192 public abstract InputStream bodyStream () throws IOException ;
9293
9394 /**
@@ -121,7 +122,7 @@ public Map<String, List<String>> allHeaders() {
121122 }
122123
123124 /**
124- * Handles the "Content-Encoding" header.
125+ * Handles wrapping the body stream if indicated by the "Content-Encoding" header.
125126 *
126127 * @param stream
127128 * the stream to possibly wrap
@@ -139,6 +140,24 @@ protected InputStream wrapStream(InputStream stream) throws IOException {
139140 throw new UnsupportedOperationException ("Unexpected Content-Encoding: " + encoding );
140141 }
141142
143+ /**
144+ * Parse a header value as a signed decimal integer.
145+ *
146+ * @param name
147+ * the header field to parse
148+ * @return integer value of the header field
149+ * @throws NumberFormatException
150+ * if the header is missing or does not contain a parsable integer.
151+ */
152+ public final int parseInt (String name ) throws NumberFormatException {
153+ try {
154+ String headerValue = header (name );
155+ return Integer .parseInt (headerValue );
156+ } catch (NumberFormatException e ) {
157+ throw new NumberFormatException (name + ": " + e .getMessage ());
158+ }
159+ }
160+
142161 public abstract static class ByteArrayResponse extends GitHubConnectorResponse {
143162
144163 private boolean inputStreamRead = false ;
@@ -155,6 +174,7 @@ protected ByteArrayResponse(@Nonnull GitHubConnectorRequest request,
155174 * {@inheritDoc}
156175 */
157176 @ Override
177+ @ Nonnull
158178 public InputStream bodyStream () throws IOException {
159179 if (isClosed ) {
160180 throw new IOException ("Response is closed" );
@@ -171,7 +191,11 @@ public InputStream bodyStream() throws IOException {
171191 }
172192 }
173193
174- return inputBytes == null ? null : new ByteArrayInputStream (inputBytes );
194+ if (inputBytes == null ) {
195+ throw new IOException ("Response body missing, stream null" );
196+ }
197+
198+ return new ByteArrayInputStream (inputBytes );
175199 }
176200
177201 /**
0 commit comments