views:

36

answers:

3

I have some classes that need a bunch of data tables to do their work (basically arrays of primitives, byte[], short[], int[] and float[]).

Some of the tables are relatively large. Initializing them in the code blows up the class file, and in some cases it also exceeds the size limit for the class initializer.

Currently i have stored the data in files and use getResourceAsStream() to read the files in a static block.

Is there a better way that makes the data inseparable from the class file? There is no need to ever replace the data independently from the class.

+1  A: 

The use of class resources sounds perfectly appropriate. They are abused for many purposes, but this is a great application.

erickson
A: 

maybe you can encode the data in a string literal (64K limit).

final byte[] table = decode("20,5F,A9,3D,E3.........");
irreputable
I dont feel this is a good idea. While it keeps the data in the class file, the String literal would be interned resulting in a hefty increase of memory requirement.
Durandal
interned strings can be gc-ed. the problem is the reference in the class constant pool. a workaround could be using an one-off class loader to load the class with the string literal, after computing the byte array, discard them so they'll be gc-ed. of course this is too much trouble. reading from file is better.
irreputable
A: 

I think it depends on the situation. For something like DES or AES for example, the tables are actually intrinsic to the algorithm: to separate them from the code that uses them doesn't make much sense in my eyes. In these cases the table isn't really "data": they're code in more ways.

When I wrote a DES implementation, I still did want to get them out of the way a bit so I included them as static members of a package-private class in the same package. You can even do a static import then and use the arrays as if they're declared right there in your class.

Mark Peters