jdk/src/share/classes/java/awt/Font.java
changeset 18214 e9eff0f1f1df
parent 13041 8477cb6992be
child 20455 f6f9a0c2796b
--- a/jdk/src/share/classes/java/awt/Font.java	Thu Mar 28 09:39:26 2013 +0100
+++ b/jdk/src/share/classes/java/awt/Font.java	Fri Mar 29 10:01:19 2013 -0700
@@ -873,6 +873,33 @@
     public static Font createFont(int fontFormat, InputStream fontStream)
         throws java.awt.FontFormatException, java.io.IOException {
 
+        if (hasTempPermission()) {
+            return createFont0(fontFormat, fontStream, null);
+        }
+
+        // Otherwise, be extra conscious of pending temp file creation and
+        // resourcefully handle the temp file resources, among other things.
+        CreatedFontTracker tracker = CreatedFontTracker.getTracker();
+        boolean acquired = false;
+        try {
+            acquired = tracker.acquirePermit();
+            if (!acquired) {
+                throw new IOException("Timed out waiting for resources.");
+            }
+            return createFont0(fontFormat, fontStream, tracker);
+        } catch (InterruptedException e) {
+            throw new IOException("Problem reading font data.");
+        } finally {
+            if (acquired) {
+                tracker.releasePermit();
+            }
+        }
+    }
+
+    private static Font createFont0(int fontFormat, InputStream fontStream,
+                                    CreatedFontTracker tracker)
+        throws java.awt.FontFormatException, java.io.IOException {
+
         if (fontFormat != Font.TRUETYPE_FONT &&
             fontFormat != Font.TYPE1_FONT) {
             throw new IllegalArgumentException ("font format not recognized");
@@ -886,9 +913,11 @@
                     }
                 }
             );
+            if (tracker != null) {
+                tracker.add(tFile);
+            }
 
             int totalSize = 0;
-            CreatedFontTracker tracker = null;
             try {
                 final OutputStream outStream =
                     AccessController.doPrivileged(
@@ -898,8 +927,8 @@
                             }
                         }
                     );
-                if (!hasTempPermission()) {
-                    tracker = CreatedFontTracker.getTracker();
+                if (tracker != null) {
+                    tracker.set(tFile, outStream);
                 }
                 try {
                     byte[] buf = new byte[8192];
@@ -940,6 +969,9 @@
                 Font font = new Font(tFile, fontFormat, true, tracker);
                 return font;
             } finally {
+                if (tracker != null) {
+                    tracker.remove(tFile);
+                }
                 if (!copiedFontData) {
                     if (tracker != null) {
                         tracker.subBytes(totalSize);