logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1516477 - /logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/jmx/LoggerContextAdmin.java
Date Thu, 22 Aug 2013 14:58:44 GMT
Author: ggregory
Date: Thu Aug 22 14:58:44 2013
New Revision: 1516477

URL: http://svn.apache.org/r1516477
Log:
Make sure no resources are leaked managing streams and readers.

Modified:
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/jmx/LoggerContextAdmin.java

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/jmx/LoggerContextAdmin.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/jmx/LoggerContextAdmin.java?rev=1516477&r1=1516476&r2=1516477&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/jmx/LoggerContextAdmin.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/jmx/LoggerContextAdmin.java
Thu Aug 22 14:58:44 2013
@@ -32,6 +32,7 @@ import java.nio.charset.Charset;
 import java.util.Map;
 import java.util.concurrent.Executor;
 import java.util.concurrent.atomic.AtomicLong;
+
 import javax.management.MBeanNotificationInfo;
 import javax.management.Notification;
 import javax.management.NotificationBroadcasterSupport;
@@ -186,11 +187,22 @@ public class LoggerContextAdmin extends 
         }
     }
 
+    /**
+     * 
+     * @param uri
+     * @param charset MUST not be null
+     * @return
+     * @throws IOException
+     */
     private String readContents(final URI uri, final Charset charset) throws IOException
{
-        InputStream in = null;
+        if (charset == null) {
+            throw new IllegalArgumentException("charset must not be null");
+        }
+        Reader reader = null;
         try {
-            in = uri.toURL().openStream();
-            final Reader reader = new InputStreamReader(in, charset);
+            InputStream in = uri.toURL().openStream();
+            // The stream is open and charset is not null, we can create the reader safely
without a possible NPE.
+            reader = new InputStreamReader(in, charset);
             final StringBuilder result = new StringBuilder(TEXT_BUFFER);
             final char[] buff = new char[PAGE];
             int count = -1;
@@ -200,7 +212,9 @@ public class LoggerContextAdmin extends 
             return result.toString();
         } finally {
             try {
-                in.close();
+                if (reader != null) {
+                    reader.close();
+                }
             } catch (final Exception ignored) {
                 // ignored
             }



Mime
View raw message