Can anyone help with how I would initialize an emp...
# cfml-general
p
Can anyone help with how I would initialize an empty Java byte array in ColdFusion? For instance:
byte[] values = new byte[5];
I realize I can do something like this
values = javaCast("byte[]", [ 83, 65, 82, 65, 72 ]);
But I need to initial byte array to be empty?
t
What about just setting an empty array to start? ``values = javaCast("byte[]", []);``
p
For my purposes, I need it to be initialized to a certain size. Thanks.
t
Understood I had a feeling I was missing something.
p
Copy code
function createByteArray(string){
    var objString = createObject("Java", "java.lang.String").init(JavaCast("string", string));
    return objString.getBytes();
}
p
How would I use the function you sent to make sure I got an array back of certain size? For instance, if I needed "new byte[5]", would I need to send in a string argument of 5 characters?
And thanks, I've been reading Ben's article. Admittedly, working with bytes/byte arrays is not my strong suit.
s
values = CreateObject("java","java.lang.reflect.Array").
NewInstance( CreateObject("java","java.lang.Byte").TYPE, JavaCast( "int", 5 ) );
1
maybe?
a
Which version of CF?
p
2018
a
😞 If it ws 2021 you could just write the Java code directly in yer CFML file.
p
Sigh....I know
a
A Byte can't have a null value, as the backing byte doesn't support it. So this is as close as yer gonna get, I think:
Copy code
nullBytes = javaCast("Byte[]", [0,0,0,0,0])
That results in the same thing @s1deburn's one does, plus another angle I tried until I did my reading and worked out what yer asking seems invalid: https://trycf.com/gist/384565addd4995d708b2c052794353db/acf2021?theme=monokai
👍 1
(and, sorry, bad variable names in that. Should be
zeroBytes
not
nullBytes
.... didn't refactor after I changed my mind about what's going on.
z
re writing java directly in cfml, with lucee https://luceeserver.atlassian.net/browse/LDEV-4267
1