From b14910d4c424a71bc6136436efbf8b5f5d125d2b Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Fri, 2 Dec 2016 22:18:03 +0100 Subject: [PATCH] Closing fd as it must be done in CustomHTTPClient I feel ashame... :( --- .../java/de/rxjava/tests/httpclient/CustomHTTPClient.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/Allgemeines/RxJava/src/main/java/de/rxjava/tests/httpclient/CustomHTTPClient.java b/Allgemeines/RxJava/src/main/java/de/rxjava/tests/httpclient/CustomHTTPClient.java index d639b03..0c7b510 100644 --- a/Allgemeines/RxJava/src/main/java/de/rxjava/tests/httpclient/CustomHTTPClient.java +++ b/Allgemeines/RxJava/src/main/java/de/rxjava/tests/httpclient/CustomHTTPClient.java @@ -35,12 +35,15 @@ public class CustomHTTPClient { try { connection.setRequestProperty("User-Agent", userAgent); connection.setRequestProperty("Cache-Control", "no-cache"); - final InputStream in = new BufferedInputStream(connection.getInputStream()); - final ByteArrayOutputStream buffer = readInputStream(in); - // No easy way of retrieving the charset from urlConnection.getContentType() - // Currently OpenWeatherMap returns: application/json; charset=utf-8 - // Let's hope they will not change the content-type :/ - return new String(buffer.toByteArray(), Charset.forName("UTF-8")); + + try (final InputStream in = new BufferedInputStream(connection.getInputStream()); + final ByteArrayOutputStream buffer = readInputStream(in)) { + + // No easy way of retrieving the charset from urlConnection.getContentType() + // Currently OpenWeatherMap returns: application/json; charset=utf-8 + // Let's hope they will not change the content-type :/ + return new String(buffer.toByteArray(), Charset.forName("UTF-8")); + } } finally { connection.disconnect(); } -- 2.1.4