Правильное закрытие URLConnection и InputStream?

Я видел много различных примеров использования HttpURLConnection + InputStream и их закрытия (или не закрытия) после использования. Вот что я придумал, чтобы убедиться, что все закрывается после завершения, независимо от того, есть ошибка или нет. Правильно ли это?:

HttpURLConnection conn = null;
InputStream is = null;
try {
    URL url = new URL("http://example.com");

    // (set connection and read timeouts on the connection)
    conn = (HttpURLConnection)url.openConnection();

    is = new BufferedInputStream(conn.getInputStream());

    doSomethingWithInputStream(is);

} catch (Exception ex) {
} finally {
    if (is != null) {
        try {
            is.close();
        } catch (IOException e) {
        }
    }
    if (conn != null) {
        conn.disconnect();
    }
}

Спасибо

40
задан user291701 5 February 2012 в 15:21
поделиться