< prev index next >

src/java.desktop/share/classes/sun/font/CreatedFontTracker.java

Print this page




  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.font;
  27 
  28 import java.io.File;
  29 import java.io.OutputStream;
  30 import java.security.AccessController;
  31 import java.security.PrivilegedAction;
  32 import java.util.HashMap;
  33 import java.util.Map;
  34 import java.util.concurrent.Semaphore;
  35 import java.util.concurrent.TimeUnit;
  36 
  37 import sun.awt.AppContext;
  38 import sun.awt.util.ThreadGroupUtils;
  39 import sun.misc.ManagedLocalsThread;
  40 
  41 public class CreatedFontTracker {
  42 
  43     public static final int MAX_FILE_SIZE = 32 * 1024 * 1024;
  44     public static final int MAX_TOTAL_BYTES = 10 * MAX_FILE_SIZE;
  45 
  46     static CreatedFontTracker tracker;
  47     int numBytes;
  48 
  49     public static synchronized CreatedFontTracker getTracker() {
  50         if (tracker == null) {
  51             tracker = new CreatedFontTracker();
  52         }
  53         return tracker;
  54     }
  55 
  56     private CreatedFontTracker() {
  57         numBytes = 0;
  58     }
  59 


 105         TempFileDeletionHook.remove(file);
 106     }
 107 
 108     /**
 109      * Helper class for cleanup of temp files created while processing fonts.
 110      * Note that this only applies to createFont() from an InputStream object.
 111      */
 112     private static class TempFileDeletionHook {
 113         private static HashMap<File, OutputStream> files = new HashMap<>();
 114 
 115         private static Thread t = null;
 116         static void init() {
 117             if (t == null) {
 118                 // Add a shutdown hook to remove the temp file.
 119                 AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
 120                     /* The thread must be a member of a thread group
 121                      * which will not get GCed before VM exit.
 122                      * Make its parent the top-level thread group.
 123                      */
 124                     ThreadGroup rootTG = ThreadGroupUtils.getRootThreadGroup();
 125                     t = new ManagedLocalsThread(rootTG,
 126                                                 TempFileDeletionHook::runHooks);
 127                     /* Set context class loader to null in order to avoid
 128                      * keeping a strong reference to an application classloader.
 129                      */
 130                     t.setContextClassLoader(null);
 131                     Runtime.getRuntime().addShutdownHook(t);
 132                     return null;
 133                 });
 134             }
 135         }
 136 
 137         private TempFileDeletionHook() {}
 138 
 139         static synchronized void add(File file) {
 140             init();
 141             files.put(file, null);
 142         }
 143 
 144         static synchronized void set(File file, OutputStream os) {
 145             files.put(file, os);
 146         }




  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.font;
  27 
  28 import java.io.File;
  29 import java.io.OutputStream;
  30 import java.security.AccessController;
  31 import java.security.PrivilegedAction;
  32 import java.util.HashMap;
  33 import java.util.Map;
  34 import java.util.concurrent.Semaphore;
  35 import java.util.concurrent.TimeUnit;
  36 
  37 import sun.awt.AppContext;
  38 import sun.awt.util.ThreadGroupUtils;

  39 
  40 public class CreatedFontTracker {
  41 
  42     public static final int MAX_FILE_SIZE = 32 * 1024 * 1024;
  43     public static final int MAX_TOTAL_BYTES = 10 * MAX_FILE_SIZE;
  44 
  45     static CreatedFontTracker tracker;
  46     int numBytes;
  47 
  48     public static synchronized CreatedFontTracker getTracker() {
  49         if (tracker == null) {
  50             tracker = new CreatedFontTracker();
  51         }
  52         return tracker;
  53     }
  54 
  55     private CreatedFontTracker() {
  56         numBytes = 0;
  57     }
  58 


 104         TempFileDeletionHook.remove(file);
 105     }
 106 
 107     /**
 108      * Helper class for cleanup of temp files created while processing fonts.
 109      * Note that this only applies to createFont() from an InputStream object.
 110      */
 111     private static class TempFileDeletionHook {
 112         private static HashMap<File, OutputStream> files = new HashMap<>();
 113 
 114         private static Thread t = null;
 115         static void init() {
 116             if (t == null) {
 117                 // Add a shutdown hook to remove the temp file.
 118                 AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
 119                     /* The thread must be a member of a thread group
 120                      * which will not get GCed before VM exit.
 121                      * Make its parent the top-level thread group.
 122                      */
 123                     ThreadGroup rootTG = ThreadGroupUtils.getRootThreadGroup();
 124                     t = new Thread(rootTG, TempFileDeletionHook::runHooks,
 125                                    "TempFontFileDeleter", 0, false);
 126                     /* Set context class loader to null in order to avoid
 127                      * keeping a strong reference to an application classloader.
 128                      */
 129                     t.setContextClassLoader(null);
 130                     Runtime.getRuntime().addShutdownHook(t);
 131                     return null;
 132                 });
 133             }
 134         }
 135 
 136         private TempFileDeletionHook() {}
 137 
 138         static synchronized void add(File file) {
 139             init();
 140             files.put(file, null);
 141         }
 142 
 143         static synchronized void set(File file, OutputStream os) {
 144             files.put(file, os);
 145         }


< prev index next >