title: Proper use of URLConnection and try-with-resources in Java 8
body: <p>I am not 100% if this code is 100% save against memory leaks or leaving sockets open:</p>
  
  <pre><code>    public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
  
          URLConnection connection = new URL(url).openConnection();
  
          connection.setReadTimeout(5000);
          connection.setConnectTimeout(8000);
  
          try (InputStream is = connection.getInputStream()) {
  
              BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
              String jsonText = readAll(rd);
  
              JSONObject json = new JSONObject(jsonText);
  
              return json;
          }
      }
  
      private static String readAll(Reader rd) throws IOException {
  
          StringBuilder sb = new StringBuilder();
  
          int cp;
  
          while ((cp = rd.read()) != -1) {
              sb.append((char) cp);
          }
  
          return sb.toString();
      }
  </code></pre>
  
  <p>Would I need to put the "BufferedReader rd" into an inner second try with too, or would it be obsolete? What if a read or connection timeout occurs, and the try isn't finished yet? Why doesnt the URLConnection have a disconnect() or close() function btw?</p>