@@ -73,7 +73,7 @@ static HttpRequest.Builder withAdditionalHeaders(HttpRequest.Builder builder, Ma
7373 private final Consumer <HttpRequest .Builder > memberVarInterceptor ;
7474 private final Duration memberVarReadTimeout ;
7575 private final Consumer <HttpResponse <InputStream >> memberVarResponseInterceptor ;
76- private final Consumer <HttpResponse <String >> memberVarAsyncResponseInterceptor ;
76+ private final Consumer <HttpResponse <InputStream >> memberVarAsyncResponseInterceptor ;
7777
7878 public AuthApi () {
7979 this (Configuration .getDefaultApiClient ());
@@ -91,7 +91,15 @@ public AuthApi(ApiClient apiClient) {
9191
9292
9393 protected ApiException getApiException (String operationId , HttpResponse <InputStream > response ) throws IOException {
94- String body = response .body () == null ? null : new String (response .body ().readAllBytes ());
94+ InputStream responseBody = ApiClient .getResponseBody (response );
95+ String body = null ;
96+ try {
97+ body = responseBody == null ? null : new String (responseBody .readAllBytes ());
98+ } finally {
99+ if (responseBody != null ) {
100+ responseBody .close ();
101+ }
102+ }
95103 String message = formatExceptionMessage (operationId , response .statusCode (), body );
96104 return new ApiException (response .statusCode (), message , response .headers (), body );
97105 }
@@ -110,10 +118,13 @@ private String formatExceptionMessage(String operationId, int statusCode, String
110118 * @return File
111119 * @throws ApiException If fail to read file content from response and write to disk
112120 */
113- public File downloadFileFromResponse (HttpResponse <InputStream > response ) throws ApiException {
121+ public File downloadFileFromResponse (HttpResponse <InputStream > response , InputStream responseBody ) throws ApiException {
122+ if (responseBody == null ) {
123+ throw new ApiException (new IOException ("Response body is empty" ));
124+ }
114125 try {
115126 File file = prepareDownloadFile (response );
116- java .nio .file .Files .copy (response . body () , file .toPath (), java .nio .file .StandardCopyOption .REPLACE_EXISTING );
127+ java .nio .file .Files .copy (responseBody , file .toPath (), java .nio .file .StandardCopyOption .REPLACE_EXISTING );
117128 return file ;
118129 } catch (IOException e ) {
119130 throw new ApiException (e );
@@ -199,14 +210,16 @@ public ApiResponse<String> testAuthHttpBasicWithHttpInfo(Map<String, String> hea
199210 if (memberVarResponseInterceptor != null ) {
200211 memberVarResponseInterceptor .accept (localVarResponse );
201212 }
213+ InputStream localVarResponseBody = null ;
202214 try {
203215 if (localVarResponse .statusCode ()/ 100 != 2 ) {
204216 throw getApiException ("testAuthHttpBasic" , localVarResponse );
205217 }
206218 // for plain text response
207219 if (localVarResponse .headers ().map ().containsKey ("Content-Type" ) &&
208220 "text/plain" .equalsIgnoreCase (localVarResponse .headers ().map ().get ("Content-Type" ).get (0 ).split (";" )[0 ].trim ())) {
209- java .util .Scanner s = new java .util .Scanner (localVarResponse .body ()).useDelimiter ("\\ A" );
221+ localVarResponseBody = ApiClient .getResponseBody (localVarResponse );
222+ java .util .Scanner s = new java .util .Scanner (localVarResponseBody == null ? InputStream .nullInputStream () : localVarResponseBody ).useDelimiter ("\\ A" );
210223 String responseBodyText = s .hasNext () ? s .next () : "" ;
211224 return new ApiResponse <String >(
212225 localVarResponse .statusCode (),
@@ -217,6 +230,9 @@ public ApiResponse<String> testAuthHttpBasicWithHttpInfo(Map<String, String> hea
217230 throw new RuntimeException ("Error! The response Content-Type is supposed to be `text/plain` but it's not: " + localVarResponse );
218231 }
219232 } finally {
233+ if (localVarResponseBody != null ) {
234+ localVarResponseBody .close ();
235+ }
220236 }
221237 } catch (IOException e ) {
222238 throw new ApiException (e );
@@ -297,14 +313,16 @@ public ApiResponse<String> testAuthHttpBearerWithHttpInfo(Map<String, String> he
297313 if (memberVarResponseInterceptor != null ) {
298314 memberVarResponseInterceptor .accept (localVarResponse );
299315 }
316+ InputStream localVarResponseBody = null ;
300317 try {
301318 if (localVarResponse .statusCode ()/ 100 != 2 ) {
302319 throw getApiException ("testAuthHttpBearer" , localVarResponse );
303320 }
304321 // for plain text response
305322 if (localVarResponse .headers ().map ().containsKey ("Content-Type" ) &&
306323 "text/plain" .equalsIgnoreCase (localVarResponse .headers ().map ().get ("Content-Type" ).get (0 ).split (";" )[0 ].trim ())) {
307- java .util .Scanner s = new java .util .Scanner (localVarResponse .body ()).useDelimiter ("\\ A" );
324+ localVarResponseBody = ApiClient .getResponseBody (localVarResponse );
325+ java .util .Scanner s = new java .util .Scanner (localVarResponseBody == null ? InputStream .nullInputStream () : localVarResponseBody ).useDelimiter ("\\ A" );
308326 String responseBodyText = s .hasNext () ? s .next () : "" ;
309327 return new ApiResponse <String >(
310328 localVarResponse .statusCode (),
@@ -315,6 +333,9 @@ public ApiResponse<String> testAuthHttpBearerWithHttpInfo(Map<String, String> he
315333 throw new RuntimeException ("Error! The response Content-Type is supposed to be `text/plain` but it's not: " + localVarResponse );
316334 }
317335 } finally {
336+ if (localVarResponseBody != null ) {
337+ localVarResponseBody .close ();
338+ }
318339 }
319340 } catch (IOException e ) {
320341 throw new ApiException (e );
0 commit comments