From 4afddd8ab53a631e299697e7df817decaedf4e7c Mon Sep 17 00:00:00 2001 From: hengyunabc Date: Mon, 17 Feb 2020 13:21:13 +0800 Subject: [PATCH] fix resource not close problem. close #1023 --- .../main/java/com/taobao/arthas/core/util/NetUtils.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/com/taobao/arthas/core/util/NetUtils.java b/core/src/main/java/com/taobao/arthas/core/util/NetUtils.java index f01a6055f..a3e9a63d1 100644 --- a/core/src/main/java/com/taobao/arthas/core/util/NetUtils.java +++ b/core/src/main/java/com/taobao/arthas/core/util/NetUtils.java @@ -2,9 +2,11 @@ package com.taobao.arthas.core.util; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; +import com.taobao.arthas.common.IOUtils; import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.HttpURLConnection; @@ -28,12 +30,14 @@ public class NetUtils { */ public static Response request(String urlString) { HttpURLConnection urlConnection = null; + InputStream in = null; try { URL url = new URL(urlString); urlConnection = (HttpURLConnection)url.openConnection(); // prefer json to text urlConnection.setRequestProperty("Accept", "application/json,text/plain;q=0.2"); - BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); + in = urlConnection.getInputStream(); + BufferedReader br = new BufferedReader(new InputStreamReader(in)); String line = null; StringBuilder sb = new StringBuilder(); while ((line = br.readLine()) != null) { @@ -52,6 +56,7 @@ public class NetUtils { } catch (IOException e) { return new Response(e.getMessage(), false); } finally { + IOUtils.close(in); if (urlConnection != null) { urlConnection.disconnect(); }