I'm extending a utility class that bundles a set of images and .xml description files. Currently I keep all the files in a directory and load them from there. The directory looks like this:
8.png
8.xml
9.png
9.xml
10.png
10.xml
...
...
50.png
50.xml
...
Here's my current constructor. It is lightning fast and does what I need it to do. (I've stripped out some of the error checking 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 webstart and cleanliness, I've been trying to load these resources from a Jar instead. I've got it working, but the load time went from instantaneous to a few seconds, and that's not acceptable. Here's the code I tried (again, error checking stripped):
(This isn't the best way to do what I want to do, it's a mock-up to see if the idea worked. It didn't. The two for-loops is in no way the source of the problem; it's the process of creating all those InputStreams that slows 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);
}
}
Anyone know of a better way to work with .jar files besides the way I've tried here? Here's the AngelCodeFont API. If it was absolutely necessary I could submit a patch for that, but I'd rather not have to. It seems to me that there's probably a way to do what I want to do, I'm just not familiar with it.
I'm not terribly against quickly dumping the jar to a temporary directory and then reading the files from there, but if there's a way to do it reading directly from the jar quickly, I'd much rather do that.
Also: Compression isn't an issue at all. The only reason I'm using a jar is for the packing issue.