开发者

How can i include (constant) primitive data tables in a class?

开发者 https://www.devze.com 2023-01-07 03:16 出处:网络
I have some classes that need a bunch of data tables to do their work (basically arrays of primitives, byte[], short[], int[] and float[]).

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 fil开发者_JS百科e, 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.


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


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

final byte[] table = decode("20,5F,A9,3D,E3.........");


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号