|
1 | 1 | package com.github.dockerjava.jaxrs.filter; |
2 | 2 |
|
3 | | -import java.io.EOFException; |
4 | 3 | import java.io.IOException; |
5 | 4 | import java.io.InputStream; |
6 | 5 | import java.nio.charset.Charset; |
@@ -58,33 +57,24 @@ public void filter(ClientRequestContext requestContext, ClientResponseContext re |
58 | 57 |
|
59 | 58 | private String getBodyAsMessage(ClientResponseContext responseContext) throws IOException { |
60 | 59 | 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()) { |
71 | 61 | Charset charset = null; |
72 | 62 | MediaType mediaType = responseContext.getMediaType(); |
73 | 63 | if (mediaType != null) { |
74 | 64 | String charsetName = mediaType.getParameters().get("charset"); |
75 | 65 | if (charsetName != null) { |
76 | 66 | try { |
77 | 67 | charset = Charset.forName(charsetName); |
78 | | - } catch (Exception e) { |
79 | | - // Do noting... |
80 | | - } |
| 68 | + } catch (Exception ignored) { } |
81 | 69 | } |
82 | 70 | } |
| 71 | + |
83 | 72 | if (charset == null) { |
84 | 73 | charset = Charset.defaultCharset(); |
85 | 74 | } |
86 | | - return new String(buffer, charset); |
87 | | - } |
| 75 | + |
| 76 | + return IOUtils.toString(entityStream, charset); |
| 77 | + } catch (Exception ignored) { } |
88 | 78 | } |
89 | 79 | return null; |
90 | 80 | } |
|
0 commit comments