I am expanding a utility class that combines a set of images and .xml description files. Currently, I store all the files in a directory and download them from there. The directory is as follows:
8.png 8.xml 9.png 9.xml 10.png 10.xml ... ... 50.png 50.xml ...
Here is my current constructor. He is lightning fast and does what I need. (I removed the error checking part to make it easier to read):
public DivineFont(String directory ) { File dir = new File(directory); //children is an array that looks like this: '10.fnt', '11.fnt', etc. String[] children = dir.list(fntFileFilter); fonts = new Hashtable<Integer, AngelCodeFont>(100); AngelCodeFont buffer; int number; String fntFile; String imgFile; for(int k = 0; k < children.length; k++ ) { number = Integer.parseInt( children[k].split("\\.")[0] ); fntFile = directory + File.separator + number + ".xml"; imgFile = directory + File.separator + number + ".png"; buffer = new AngelCodeFont(fntFile, imgFile); fonts.put(number, buffer); } }
For the sake of web start and cleanliness, I am trying to download these resources from the Jar. It works for me, but the download time is instant to several seconds, and this is unacceptable. Here is the code I tried (again, error checking was stripped):
(This is not the best way to do what I want to do, it’s a layout to see if this idea works. It’s not. Two for-loops are by no means the source of the problem, it’s the process of creating all those InputStream that slow it down )
public DivineFont(String jarFileName ) { JarFile jarfile = new JarFile(jarFileName); Enumeration<JarEntry> em = jarfile.entries(); ArrayList<Integer> fontHeights = new ArrayList<Integer>(100); for (Enumeration em1 = jarfile.entries(); em1.hasMoreElements(); ) { String fileName = em1.nextElement().toString(); if( fileName.endsWith(".fnt") ) { fontHeights.add( Integer.parseInt(fileName.split("\\.")[0] ) ); } } fonts = new Hashtable<Integer, AngelCodeFont>(100); AngelCodeFont buffer; int number; for(int k = 0; k < fontHeights.size(); k++ ) { number = fontHeights.get(k); InputStream fntFileStream = jarfile.getInputStream(jarfile.getEntry(number + ".xml")); InputStream pngFileStream = jarfile.getInputStream(jarfile.getEntry(number + ".png")); buffer = new AngelCodeFont(String.valueOf(number), fntFileStream, pngFileStream ); fonts.put(number, buffer); } }
Does anyone know how best to work with .jar files, besides how I tried here? Here's the AngelCodeFont API . If it were absolutely necessary, I could write a patch for this, but I would not want this. It seems to me that there is probably a way to do what I want to do, I'm just not familiar with this.
I am not terribly opposed to quickly dumping the jar into a temporary directory and then reading files from there, but if there is a way to do this by reading directly from the jar, I would rather do it.
Also: compression is not a problem at all. The only reason I use the can is because of the packaging problem.