1121N/A/*
2362N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
1121N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1121N/A *
1121N/A * This code is free software; you can redistribute it and/or modify it
1121N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
1121N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
1121N/A *
1121N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1121N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1121N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1121N/A * version 2 for more details (a copy is included in the LICENSE file that
1121N/A * accompanied this code).
1121N/A *
1121N/A * You should have received a copy of the GNU General Public License version
1121N/A * 2 along with this work; if not, write to the Free Software Foundation,
1121N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1121N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1121N/A */
1121N/A
1121N/Apackage sun.font;
1121N/A
6316N/Aimport java.io.File;
6316N/Aimport java.io.OutputStream;
6316N/Aimport java.util.HashMap;
6316N/Aimport java.util.Map;
6316N/Aimport java.util.concurrent.Semaphore;
6316N/Aimport java.util.concurrent.TimeUnit;
6316N/A
6316N/Aimport sun.awt.AppContext;
6316N/A
1121N/Apublic class CreatedFontTracker {
1121N/A
1121N/A public static final int MAX_FILE_SIZE = 32 * 1024 * 1024;
1121N/A public static final int MAX_TOTAL_BYTES = 10 * MAX_FILE_SIZE;
1121N/A
1121N/A static CreatedFontTracker tracker;
6316N/A int numBytes;
1121N/A
1121N/A public static synchronized CreatedFontTracker getTracker() {
1121N/A if (tracker == null) {
1121N/A tracker = new CreatedFontTracker();
1121N/A }
1121N/A return tracker;
1121N/A }
1121N/A
6316N/A private CreatedFontTracker() {
6316N/A numBytes = 0;
6316N/A }
6316N/A
1121N/A public synchronized int getNumBytes() {
1121N/A return numBytes;
1121N/A }
1121N/A
1121N/A public synchronized void addBytes(int sz) {
1121N/A numBytes += sz;
1121N/A }
1121N/A
1121N/A public synchronized void subBytes(int sz) {
1121N/A numBytes -= sz;
1121N/A }
6316N/A
6316N/A /**
6316N/A * Returns an AppContext-specific counting semaphore.
6316N/A */
6316N/A private static synchronized Semaphore getCS() {
6316N/A final AppContext appContext = AppContext.getAppContext();
6316N/A Semaphore cs = (Semaphore) appContext.get(CreatedFontTracker.class);
6316N/A if (cs == null) {
6316N/A // Make a semaphore with 5 permits that obeys the first-in first-out
6316N/A // granting of permits.
6316N/A cs = new Semaphore(5, true);
6316N/A appContext.put(CreatedFontTracker.class, cs);
6316N/A }
6316N/A return cs;
6316N/A }
6316N/A
6316N/A public boolean acquirePermit() throws InterruptedException {
6316N/A // This does a timed-out wait.
6316N/A return getCS().tryAcquire(120, TimeUnit.SECONDS);
6316N/A }
6316N/A
6316N/A public void releasePermit() {
6316N/A getCS().release();
6316N/A }
6316N/A
6316N/A public void add(File file) {
6316N/A TempFileDeletionHook.add(file);
6316N/A }
6316N/A
6316N/A public void set(File file, OutputStream os) {
6316N/A TempFileDeletionHook.set(file, os);
6316N/A }
6316N/A
6316N/A public void remove(File file) {
6316N/A TempFileDeletionHook.remove(file);
6316N/A }
6316N/A
6316N/A /**
6316N/A * Helper class for cleanup of temp files created while processing fonts.
6316N/A * Note that this only applies to createFont() from an InputStream object.
6316N/A */
6316N/A private static class TempFileDeletionHook {
6316N/A private static HashMap<File, OutputStream> files = new HashMap<>();
6316N/A
6316N/A private static Thread t = null;
6316N/A static void init() {
6316N/A if (t == null) {
6316N/A // Add a shutdown hook to remove the temp file.
6316N/A java.security.AccessController.doPrivileged(
6316N/A new java.security.PrivilegedAction() {
6316N/A public Object run() {
6316N/A /* The thread must be a member of a thread group
6316N/A * which will not get GCed before VM exit.
6316N/A * Make its parent the top-level thread group.
6316N/A */
6316N/A ThreadGroup tg =
6316N/A Thread.currentThread().getThreadGroup();
6316N/A for (ThreadGroup tgn = tg;
6316N/A tgn != null;
6316N/A tg = tgn, tgn = tg.getParent());
6316N/A t = new Thread(tg, new Runnable() {
6316N/A public void run() {
6316N/A runHooks();
6316N/A }
6316N/A });
6316N/A t.setContextClassLoader(null);
6316N/A Runtime.getRuntime().addShutdownHook(t);
6316N/A return null;
6316N/A }
6316N/A });
6316N/A }
6316N/A }
6316N/A
6316N/A private TempFileDeletionHook() {}
6316N/A
6316N/A static synchronized void add(File file) {
6316N/A init();
6316N/A files.put(file, null);
6316N/A }
6316N/A
6316N/A static synchronized void set(File file, OutputStream os) {
6316N/A files.put(file, os);
6316N/A }
6316N/A
6316N/A static synchronized void remove(File file) {
6316N/A files.remove(file);
6316N/A }
6316N/A
6316N/A static synchronized void runHooks() {
6316N/A if (files.isEmpty()) {
6316N/A return;
6316N/A }
6316N/A
6316N/A for (Map.Entry<File, OutputStream> entry : files.entrySet()) {
6316N/A // Close the associated output stream, and then delete the file.
6316N/A try {
6316N/A if (entry.getValue() != null) {
6316N/A entry.getValue().close();
6316N/A }
6316N/A } catch (Exception e) {}
6316N/A entry.getKey().delete();
6316N/A }
6316N/A }
6316N/A }
1121N/A}