Hi all,

I'm working on something that requires a static array of almost 400k.
I figured that the best way to initialize this array would be to
compress the data, add a static array with an initializer to put the
compressed data there (gzip compresses it to about 22k, which seems
reasonable), and then add a static {} block that uses java.util.zip
to decompress this data.

Here's the code, edited for clarity:

import java.util.zip.*;

public class MyClass
{
    private static final byte myGzippedData =
    {
        31, -117, 8, 8, -3, -3, 14, 54, 0, 3, 97, 0, -19, 93, 11,
        -126, -37, -86, 14, -35, 42, 60, 46, -39, -1, 18, -34, 88, 31,
        // and so on and so on... 22426 bytes in all
    }

    private static byte[] myData;

    static
    {
        Inflater inf = new Inflater(true);

        myData = new byte[368212];
        inf.setInput(myGzippedData, 0, myGzippedData.length);
        try
        {
            inf.inflate(myData);
        }
        catch (DataFormatException e) {}
    }

    public static void main(String[] argv)
    {
        for (int i=0; i<myData.length; i++)
        {
            System.out.write(myData[i]);
        }
    }
}

I'm having all sorts of problems with this. First of all, it just
doesn't work. Inflater.inflate() throws a DataFormatException with
message "invalid block type" (the initializer for myGzippedData was
generated from a .gz file, and if I dump that data to System.out
using write(), and pipe that to gunzip, it works fine).
Also, the .class file is huge -- more than 330k. I would expect an
initializer that initializes a 22k-array to add only slightly more
than 22k to the class file! What's going on?

If I could solve these to problems, I would be a happy camper once
again; but I was also thinking of a different approach: I could put
the data into a separate file, and read that to initialize the array;
I wouldn't even worry about compressing that file, because everything
will eventually be put into a jar file, and that will take care of
that. But that leads to the next problem: how do I read a file from
the jar containing my package, if possible in such a way that the same
code will work for an application, *and* if this jar is loaded as an
applet? If someone could share some working code to do that, or point
me to an example, I would be very grateful!

AtDhVaAnNkCsE!

 - Thomas

Reply via email to