changeset 3064:a4fd754f895d

6991300: MimeTable is unsafe Reviewed-by: alanb, michaelm
author chegar
date Tue, 12 Oct 2010 17:01:07 +0100
parents 4dbd83eb0250
children 5eb6755dde8e
files src/share/classes/sun/net/www/MimeTable.java
diffstat 1 files changed, 16 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/sun/net/www/MimeTable.java	Tue Oct 12 11:11:32 2010 +0100
+++ b/src/share/classes/sun/net/www/MimeTable.java	Tue Oct 12 17:01:07 2010 +0100
@@ -73,29 +73,32 @@
 
     private static final String filePreamble = "sun.net.www MIME content-types table";
     private static final String fileMagic = "#" + filePreamble;
-    private static MimeTable defaultInstance = null;
 
     MimeTable() {
         load();
     }
 
+    private static class DefaultInstanceHolder {
+        static final MimeTable defaultInstance = getDefaultInstance();
+
+        static MimeTable getDefaultInstance() {
+            return java.security.AccessController.doPrivileged(
+                new java.security.PrivilegedAction<MimeTable>() {
+                public MimeTable run() {
+                    MimeTable instance = new MimeTable();
+                    URLConnection.setFileNameMap(instance);
+                    return instance;
+                }
+            });
+        }
+    }
+
     /**
      * Get the single instance of this class.  First use will load the
      * table from a data file.
      */
     public static MimeTable getDefaultTable() {
-        if (defaultInstance == null) {
-            java.security.AccessController.doPrivileged(
-                new java.security.PrivilegedAction<Void>() {
-                    public Void run() {
-                    defaultInstance = new MimeTable();
-                    URLConnection.setFileNameMap(defaultInstance);
-                    return null;
-                }
-            });
-        }
-
-        return defaultInstance;
+        return DefaultInstanceHolder.defaultInstance;
     }
 
     /**