views:

227

answers:

2

The code below does its work but leaves copies of the font file in the temp directory each time it is run. These files are named +~JF7154903081130224445.tmp where the number seems random for each created file.

InputStream fontStream = this.getClass().getResourceAsStream("handsean.ttf");
Font baseFont = Font.createFont(Font.TRUETYPE_FONT, fontStream);
fontStream.close();

I have found years-old discussions in forums at sun.com and other resources on the web where this is recognized as a bug in JDK, where upgrading from 1.5.0_06 to 1.5.0_08 would solve the problem; however, the version I am using is a later version (1.6.0_13).

I tried solving the problem by deleting the files after the font related operations are finished, but the files are locked at that time. The files can only be deleted after the web application has stopped.

Does anybody have a solution to this?

Best regards, Ben Verhees.

+1  A: 

With JDK1.6.0_16, the font manager seem to be using the temporary file as a kind of cache and will only read glyphs from the font when they are required. It is also adding a shutdown hook, which will delete the file when the JVM terminates ordinarily. Depending on the VM, font rendering is perhaps also delegated to native code which needs access to the file, so keeping a lock on the file seems reasonable to me.

Are the files actually kept, even if your servlet container (you are mentioning a web application) terminates regularly, or are you killing it without allowing it to cleanup its resources properly?

jarnbjo
Yes, the files are also kept after normal termination.
Ben Verhees
I'm sorry, but I cannot reproduce your problem with 1.6.0_13 either. The font files are indeed kept and locked, but a shutdown hook is used to delete the file. Are you doing something else to prevent shutdown hooks from being run or can you attach with a remote debugger and really confirm that the sun.font.FontManager.fileCloser hook is really not run? In theory, the delete invokation in line 2302 may silently fail if something else is holding the file open (native code?).
jarnbjo
Thanks for taking the time to try and reproduce my problem. Taking into account that you could not reproduce it, it might be related to my environment. I am developing on tomcat running in XP, and testing with marking other files with deleteOnExit shows that the process is probably never terminated in a way that allows for cleanup as these files are kept as well.
Ben Verhees
+1  A: 

If your ttf files are not inside an archive, you can call createFont(File) instead of createFont(InputStream)

As to the best of my knowledge, this bug exists in Java 6, it is enough to look at the sources of Font class.

Dmitry
Good suggestion to have a look at the sources, using createFont(File) looks promising, as it doesn't use a temporary file. Will try that and let you know.
Ben Verhees
I modified the code to call createFont(File) which prevented the temporary files from being created at all.
Ben Verhees