Skip to content

Commit 055a028

Browse files
committed
Fix resource leak in error the error handler
1 parent c55ef32 commit 055a028

File tree

1 file changed

+6
-16
lines changed

1 file changed

+6
-16
lines changed

src/main/java/com/github/dockerjava/jaxrs/filter/ResponseStatusExceptionFilter.java

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.github.dockerjava.jaxrs.filter;
22

3-
import java.io.EOFException;
43
import java.io.IOException;
54
import java.io.InputStream;
65
import java.nio.charset.Charset;
@@ -58,33 +57,24 @@ public void filter(ClientRequestContext requestContext, ClientResponseContext re
5857

5958
private String getBodyAsMessage(ClientResponseContext responseContext) throws IOException {
6059
if (responseContext.hasEntity()) {
61-
int contentLength = responseContext.getLength();
62-
if (contentLength != -1) {
63-
byte[] buffer = new byte[contentLength];
64-
try {
65-
InputStream entityStream = responseContext.getEntityStream();
66-
IOUtils.readFully(entityStream, buffer);
67-
entityStream.close();
68-
} catch (EOFException e) {
69-
return null;
70-
}
60+
try (InputStream entityStream = responseContext.getEntityStream()) {
7161
Charset charset = null;
7262
MediaType mediaType = responseContext.getMediaType();
7363
if (mediaType != null) {
7464
String charsetName = mediaType.getParameters().get("charset");
7565
if (charsetName != null) {
7666
try {
7767
charset = Charset.forName(charsetName);
78-
} catch (Exception e) {
79-
// Do noting...
80-
}
68+
} catch (Exception ignored) { }
8169
}
8270
}
71+
8372
if (charset == null) {
8473
charset = Charset.defaultCharset();
8574
}
86-
return new String(buffer, charset);
87-
}
75+
76+
return IOUtils.toString(entityStream, charset);
77+
} catch (Exception ignored) { }
8878
}
8979
return null;
9080
}

0 commit comments

Comments
 (0)