8344146: Remove temporary font file tracking code.

Reviewed-by: honkar, aivanov
This commit is contained in:
Phil Race 2025-01-13 20:10:19 +00:00
parent 61dc07c118
commit 13a1775718
5 changed files with 14 additions and 276 deletions

View File

@ -55,7 +55,6 @@ import sun.font.AttributeMap;
import sun.font.AttributeValues;
import sun.font.CompositeFont;
import sun.font.CoreMetrics;
import sun.font.CreatedFontTracker;
import sun.font.Font2D;
import sun.font.Font2DHandle;
import sun.font.FontAccess;
@ -629,8 +628,7 @@ public class Font implements java.io.Serializable
}
/* used to implement Font.createFont */
private Font(File fontFile, int fontFormat,
boolean isCopy, CreatedFontTracker tracker)
private Font(File fontFile, int fontFormat, boolean isCopy)
throws FontFormatException {
this.createdFont = true;
/* Font2D instances created by this method track their font file
@ -638,7 +636,7 @@ public class Font implements java.io.Serializable
*/
FontManager fm = FontManagerFactory.getInstance();
Font2D[] fonts =
fm.createFont2D(fontFile, fontFormat, false, isCopy, tracker);
fm.createFont2D(fontFile, fontFormat, false, isCopy);
this.font2DHandle = fonts[0].handle;
this.name = this.font2DHandle.font2D.getFontName(Locale.getDefault());
this.style = Font.PLAIN;
@ -888,16 +886,6 @@ public class Font implements java.io.Serializable
return new Font(attributes);
}
/**
* Used with the byte count tracker for fonts created from streams.
* If a thread can create temp files anyway, no point in counting
* font bytes.
*/
private static boolean hasTempPermission() {
return true;
}
/**
* Returns a new array of {@code Font} decoded from the specified stream.
* The returned {@code Font[]} will have at least one element.
@ -937,28 +925,7 @@ public class Font implements java.io.Serializable
public static Font[] createFonts(InputStream fontStream)
throws FontFormatException, IOException {
final int fontFormat = Font.TRUETYPE_FONT;
if (hasTempPermission()) {
return createFont0(fontFormat, fontStream, true, 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, true, tracker);
} catch (InterruptedException e) {
throw new IOException("Problem reading font data.");
} finally {
if (acquired) {
tracker.releasePermit();
}
}
return createFont0(Font.TRUETYPE_FONT, fontStream, true);
}
/* used to implement Font.createFont */
@ -1014,7 +981,7 @@ public class Font implements java.io.Serializable
fontFile = checkFontFile(fontFormat, fontFile);
FontManager fm = FontManagerFactory.getInstance();
Font2D[] font2DArr =
fm.createFont2D(fontFile, fontFormat, true, false, null);
fm.createFont2D(fontFile, fontFormat, true, false);
int num = font2DArr.length;
Font[] fonts = new Font[num];
for (int i = 0; i < num; i++) {
@ -1054,32 +1021,11 @@ public class Font implements java.io.Serializable
public static Font createFont(int fontFormat, InputStream fontStream)
throws java.awt.FontFormatException, java.io.IOException {
if (hasTempPermission()) {
return createFont0(fontFormat, fontStream, false, null)[0];
}
// 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, false, tracker)[0];
} catch (InterruptedException e) {
throw new IOException("Problem reading font data.");
} finally {
if (acquired) {
tracker.releasePermit();
}
}
return createFont0(fontFormat, fontStream, false)[0];
}
private static Font[] createFont0(int fontFormat, InputStream fontStream,
boolean allFonts,
CreatedFontTracker tracker)
boolean allFonts)
throws java.awt.FontFormatException, java.io.IOException {
if (fontFormat != Font.TRUETYPE_FONT &&
@ -1089,16 +1035,9 @@ public class Font implements java.io.Serializable
boolean copiedFontData = false;
try {
final File tFile = Files.createTempFile("+~JF", ".tmp").toFile();
if (tracker != null) {
tracker.add(tFile);
}
int totalSize = 0;
try {
final OutputStream outStream = new FileOutputStream(tFile);
if (tracker != null) {
tracker.set(tFile, outStream);
}
try (outStream) { /* don't close the input stream */
byte[] buf = new byte[8192];
for (;;) {
@ -1106,35 +1045,13 @@ public class Font implements java.io.Serializable
if (bytesRead < 0) {
break;
}
if (tracker != null) {
if (totalSize+bytesRead > CreatedFontTracker.MAX_FILE_SIZE) {
throw new IOException("File too big.");
}
if (totalSize+tracker.getNumBytes() >
CreatedFontTracker.MAX_TOTAL_BYTES)
{
throw new IOException("Total files too big.");
}
totalSize += bytesRead;
tracker.addBytes(bytesRead);
}
outStream.write(buf, 0, bytesRead);
}
}
/* After all references to a Font2D are dropped, the file
* will be removed. To support long-lived AppContexts,
* we need to then decrement the byte count by the size
* of the file.
* If the data isn't a valid font, the implementation will
* delete the tmp file and decrement the byte count
* in the tracker object before returning from the
* constructor, so we can set 'copiedFontData' to true here
* without waiting for the results of that constructor.
*/
copiedFontData = true;
FontManager fm = FontManagerFactory.getInstance();
Font2D[] font2DArr =
fm.createFont2D(tFile, fontFormat, allFonts, true, tracker);
fm.createFont2D(tFile, fontFormat, allFonts, true);
int num = font2DArr.length;
Font[] fonts = new Font[num];
for (int i = 0; i < num; i++) {
@ -1142,13 +1059,7 @@ public class Font implements java.io.Serializable
}
return fonts;
} finally {
if (tracker != null) {
tracker.remove(tFile);
}
if (!copiedFontData) {
if (tracker != null) {
tracker.subBytes(totalSize);
}
tFile.delete();
}
}
@ -1203,7 +1114,7 @@ public class Font implements java.io.Serializable
throws java.awt.FontFormatException, java.io.IOException {
fontFile = checkFontFile(fontFormat, fontFile);
return new Font(fontFile, fontFormat, false, null);
return new Font(fontFile, fontFormat, false);
}
private static File checkFontFile(int fontFormat, File fontFile)

View File

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

View File

@ -104,11 +104,10 @@ public abstract class FileFont extends PhysicalFont {
}
static void setFileToRemove(List<Font2D> fonts,
File file, int cnt,
CreatedFontTracker tracker)
File file, int cnt)
{
CreatedFontFileDisposerRecord dr =
new CreatedFontFileDisposerRecord(file, cnt, tracker);
new CreatedFontFileDisposerRecord(file, cnt);
for (Font2D f : fonts) {
Disposer.addObjectRecord(f, dr);
@ -239,13 +238,10 @@ public abstract class FileFont extends PhysicalFont {
File fontFile = null;
int count = 0; // number of fonts referencing this file object.
CreatedFontTracker tracker;
private CreatedFontFileDisposerRecord(File file, int cnt,
CreatedFontTracker tracker) {
private CreatedFontFileDisposerRecord(File file, int cnt) {
fontFile = file;
count = (cnt > 0) ? cnt : 1;
this.tracker = tracker;
}
public void dispose() {
@ -257,9 +253,6 @@ public abstract class FileFont extends PhysicalFont {
}
if (fontFile != null) {
try {
if (tracker != null) {
tracker.subBytes((int)fontFile.length());
}
/* REMIND: is it possible that the file is
* still open? It will be closed when the
* font2D is disposed but could this code

View File

@ -85,7 +85,7 @@ public interface FontManager {
* @return the created Font2D instance
*/
public Font2D[] createFont2D(File fontFile, int fontFormat, boolean all,
boolean isCopy, CreatedFontTracker tracker)
boolean isCopy)
throws FontFormatException;
/**

View File

@ -2146,7 +2146,7 @@ public abstract class SunFontManager implements FontSupport, FontManagerForSGE {
private int createdFontCount = 0;
public Font2D[] createFont2D(File fontFile, int fontFormat, boolean all,
boolean isCopy, CreatedFontTracker tracker)
boolean isCopy)
throws FontFormatException {
List<Font2D> fList = new ArrayList<>();
@ -2154,7 +2154,6 @@ public abstract class SunFontManager implements FontSupport, FontManagerForSGE {
String fontFilePath = fontFile.getPath();
FileFont font2D = null;
final File fFile = fontFile;
final CreatedFontTracker _tracker = tracker;
boolean weakRefs = false;
int maxStrikes = 0;
synchronized (this) {
@ -2192,15 +2191,12 @@ public abstract class SunFontManager implements FontSupport, FontManagerForSGE {
}
} catch (FontFormatException e) {
if (isCopy) {
if (_tracker != null) {
_tracker.subBytes((int)fFile.length());
}
fFile.delete();
}
throw(e);
}
if (isCopy) {
FileFont.setFileToRemove(fList, fontFile, cnt, tracker);
FileFont.setFileToRemove(fList, fontFile, cnt);
synchronized (FontManager.class) {
if (tmpFontFiles == null) {