2

I'm using Apache BCEL to dynamically create java classes that will each have its own associated image. These generated class implement the following interface:

interface ImageOwner {

    byte[] getImage();

}

When dynamically implementing the 'getImage' method, I could generate bytecodes to create/return the byte array when the method is invoked, but I would rather just return a reference to the byte array if possible. Is it possible to store raw data inside a java class file?

7
  • 2
    An array is a reference in java. I'm not sure what it is you're asking. Commented Mar 24, 2012 at 13:58
  • I know, I was thinking of getting that reference from a constant pool or something like that. Commented Mar 24, 2012 at 14:02
  • Exactly what do you mean by "this kind of raw data". Commented Mar 24, 2012 at 14:17
  • What do you mean by "raw data" then? Data is data. Exactly what are you talking about? We don't understand what you mean. Commented Mar 24, 2012 at 14:21
  • Byte arrays = raw data(just a collection of bytes). Instead of a String or a integer, I want to store a collection of bytes in the class and retrieve it at runtime. Commented Mar 24, 2012 at 14:25

5 Answers 5

1

I'm really not sure here, because it seems too simple a question, but for completeness, this will do what I think you want to do:

public class ImageOwnerImpl implements ImageOwner {

    // A literal byte array. Tedious, but you could write code
    // to auto-generate this source from a file
    private byte[] imageByteArray = new byte[] { -128, 127, ...etc };

    public byte[] getImage() {
        return imageByteArray;
    }
}

This is a literal byte array, which answers your question of "how to store the bytes in the class file".

Most folks would load the image bytes from a file at runtime in the constructor, however you may have some special need that warrants this approach

Sign up to request clarification or add additional context in comments.

4 Comments

That's more or less what I need, but I don't want to invoke an external utility class in order to get the bytes, instead I want the bytes to be stored in the class file and retrieve it in the class body
If what I want is not possible and you know for sure, just specify in the answer and I will mark as correct.
@ThiadodeArruda I've edited the code to give an example of how to do it
The problem with this approach is that this new byte array is actually created in the constructor, which will not only allocate the array, but also emit opcodes to store each byte which is what I was trying to avoid. Since the jvm only operated with 4 byte integers, a byte array stored like this would at least use 4x its actual space, without even considering the bytecodes necessary to create the byte array and store each byte on its own slot. This can be bad for large image files.
1

After some research, the best way I found to achieve my needs was to base64 encode the image data and use the constant pool to store the resulting String. Even with the =~ 30% loss, it seems better than using the constructor or other methods to initialize a byte array field.

Comments

1

The only constants you can store in the constant pool are Ints, Floats, Longs, Doubles, Strings, and Classes. You're best bet is to just store it as a string and convert it to a Byte[] at run time. There is no need to base64 encode them, as strings can handle null characters, as long as you apply the proper modified unicode encoding.

Comments

0

Just define an Impl class that has a static initialiser that loads the raw data from a file that is included in your jar and loaded via the classloader.

Comments

0

You could just store it as a byte array the syntax for array in BCEL is

[B

for byte array

[java.lang.String

for a string array etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.