< prev index next >

modules/graphics/src/main/java/com/sun/javafx/font/PrismFontFile.java

Print this page

        

*** 101,112 **** this.isCopy = copy; this.isTracked = tracked; init(name, fIndex); } ! WeakReference<PrismFontFile> createFileDisposer(PrismFontFactory factory) { ! FileDisposer disposer = new FileDisposer(filename, isTracked); WeakReference<PrismFontFile> ref = Disposer.addRecord(this, disposer); disposer.setFactory(factory, ref); return ref; } --- 101,113 ---- this.isCopy = copy; this.isTracked = tracked; init(name, fIndex); } ! WeakReference<PrismFontFile> createFileDisposer(PrismFontFactory factory, ! FileRefCounter rc) { ! FileDisposer disposer = new FileDisposer(filename, isTracked, rc); WeakReference<PrismFontFile> ref = Disposer.addRecord(this, disposer); disposer.setFactory(factory, ref); return ref; }
*** 119,128 **** --- 120,138 ---- protected synchronized void disposeOnShutdown() { if (isCopy || isDecoded) { AccessController.doPrivileged( (PrivilegedAction<Void>) () -> { try { + /* Although there is likely no harm in calling + * delete on a file > once, we want to refrain + * from deleting it until the shutdown hook + * code in subclasses has had an opportunity + * to clean up native accesses on the resource. + */ + if (decFileRefCount() > 0) { + return null; + } boolean delOK = (new File(filename)).delete(); if (!delOK && PrismFontFactory.debugFonts) { System.err.println("Temp file not deleted : " + filename); }
*** 151,169 **** fontInstallationType = factory.isInstalledFont(filename) ? 1 : 0; } return fontInstallationType > 0; } static class FileDisposer implements DisposerRecord { String fileName; boolean isTracked; PrismFontFactory factory; WeakReference<PrismFontFile> refKey; ! public FileDisposer(String fileName, boolean isTracked) { this.fileName = fileName; this.isTracked = isTracked; } public void setFactory(PrismFontFactory factory, WeakReference<PrismFontFile> refKey) { this.factory = factory; --- 161,226 ---- fontInstallationType = factory.isInstalledFont(filename) ? 1 : 0; } return fontInstallationType > 0; } + + /* A TTC file resource is shared, so reference count and delete + * only when no longer using the file from any PrismFontFile instance + */ + static class FileRefCounter { + private int refCnt = 1; // start with 1. + + synchronized int getRefCount() { + return refCnt; + } + + synchronized int increment() { + return ++refCnt; + } + + synchronized int decrement() { + return (refCnt == 0) ? 0 : --refCnt; + } + } + + private FileRefCounter refCounter = null; + + FileRefCounter getFileRefCounter() { + return refCounter; + } + + FileRefCounter createFileRefCounter() { + refCounter = new FileRefCounter(); + return refCounter; + } + + void setAndIncFileRefCounter(FileRefCounter rc) { + this.refCounter = rc; + this.refCounter.increment(); + } + + int decFileRefCount() { + if (refCounter == null) { + return 0; + } else { + return refCounter.decrement(); + } + } + static class FileDisposer implements DisposerRecord { String fileName; boolean isTracked; + FileRefCounter refCounter; PrismFontFactory factory; WeakReference<PrismFontFile> refKey; ! public FileDisposer(String fileName, boolean isTracked, ! FileRefCounter rc) { this.fileName = fileName; this.isTracked = isTracked; + this.refCounter = rc; } public void setFactory(PrismFontFactory factory, WeakReference<PrismFontFile> refKey) { this.factory = factory;
*** 173,182 **** --- 230,244 ---- public synchronized void dispose() { if (fileName != null) { AccessController.doPrivileged( (PrivilegedAction<Void>) () -> { try { + if (refCounter != null && + refCounter.decrement() > 0) + { + return null; + } File file = new File(fileName); int size = (int)file.length(); file.delete(); // decrement tracker only after // successful deletion.
< prev index next >