-
1. Re: How to value a byte array
chiba Jan 12, 2005 12:39 PM (in response to dechamps)Well, the value of fs should be something like:
String fs = "private static byte[] x = { 1, 2, 3 }";
Unfortunately, "{ 1, 2, 3 }" has not been supported yet,
you must use insertBefore in CtConstructor to
append the code like:
{ x[0] = 1; x[1] = 2; x[2] = 3; } -
2. Re: How to value a byte array
dechamps Jan 12, 2005 7:24 PM (in response to dechamps)Ok I'll try, hoping it will cover my need. I want to "write" only once in the bytecode a private field whose value (an encoded and compressed one) can't be calculated at time of code writing
String fs = "private static byte[] x = { 1, 2, 3 }";
FYI:I have tried this above and it fails (don't remenber the message), so I have had a look in your Parser code and see a String that might send a warning message about this unsupported feature, but it doesn't raise it? -
3. Re: How to value a byte array
chiba Jan 12, 2005 9:50 PM (in response to dechamps)If I'm correct, static final fields can be initialized
in a static class initializer, that is,
class C {
static { .... }
:
} -
4. Re: How to value a byte array
dechamps Jan 18, 2005 6:05 PM (in response to dechamps)Bonjour,
You are right about static initializer. So I have done an insert before of
String build with a lot of b[O]=1,b[1]=........b[2002=0
That's ok, it works.
But the code of my class become a huge one
Before insertion java bytecode=1377 bytes
After insertion of 2 byte arrays (size=2002+1061 bytes) the java bytecode size=62394 bytes!
I have also tested directly in the java source the {0,34,78...}, the resulting size is quite the same.
I decided to use String (initialized with an hexa decimal encoding of my compressed bytes) instead of byte arrays then the java bytecode size=7123 bytes
Better, even not perfect
Thanks you -
5. Re: How to value a byte array
chiba Jan 19, 2005 9:05 AM (in response to dechamps)Yes, array initialization is quite inefficient in Java.
I think your technique using String is a right way. -
6. Re: How to value a byte array
jmarranz Jan 27, 2006 6:45 AM (in response to dechamps)I have a trick to solve this problem, create a utility class like:
class ByteArray
{
private byte[] array;
public ByteArray(int size)
{
array = new byte[size];
}
public ByteArray set(int index,byte b)
{
array[index]=b;
return this;
}
public byte[] getArray()
{
return array;
}
}
Then this code now works:
private static byte[] x = new ByteArray(3).set(0,1).set(1,2).set(2,3).getArray();
ok, is not marvellous and not very efficient, but works :)
Jose M. Arranz -
7. Re: How to value a byte array
jmarranz Jan 27, 2006 6:49 AM (in response to dechamps)With minor modifications , it may be simpler
class ByteArray
{
private byte[] array;
private int counter = 0;
public ByteArray(int size)
{
array = new byte[size];
}
public ByteArray add(byte b)
{
array[counter]=b;
counter++;
return this;
}
public byte[] getArray()
{
return array;
}
}
private static byte[] x = new ByteArray(3).add(1).add(2).add(3).getArray();
Jose M. Arranz