I'm using VisualVM to extract information from java heapdump. Is it possible to convert this into a base64 encoded string directly in the query?
1 Answer
You can use OQL map function and java interoperability to map byte arrays to base64 strings. The OQL below returns all byte[]
objects:
select map(heap.objects('byte[]'), function (byteArr) {
buff = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, byteArr.length);
for (i=0; i<buff.length; i++) buff[i] = Number(byteArr[i]);
return java.util.Base64.getEncoder().encodeToString(buff);
})
For better testing you can enhance the output with the link to the actual byte[]
instance:
select map(heap.objects('byte[]'), function (byteArr) {
buff = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, byteArr.length);
for (i=0; i<buff.length; i++) buff[i] = Number(byteArr[i]);
return toHtml(byteArr)+" "+java.util.Base64.getEncoder().encodeToString(buff);
})
-
Is it possible to join the whole array? The current output encodes each of them separately and not encoded correctly ibb.co/B4PVJZ9– daisyCommented Dec 21, 2023 at 0:01
-
You cannot use
map
this way, since it operates on array/iterator/enumeration. It needs arrays as input. Based on your image, the correct OQL should be (looks terrible, no formatting): select map([it.decryptionCypherKey], function (byteArr) { buff = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, byteArr.length); for (i=0; i<buff.length; i++) buff[i] = byteArr[i]; return toHtml(byteArr)+" "+java.util.Base64.getEncoder().encodeToString(buff); }) from org.apache.shiro.web.mgt.CookieRememberManager s Commented Dec 21, 2023 at 8:43 -
The map function worked but the byte[] is encoded to AAAAAAAAAAAAAAAAAAAAAA==, which is incorrect. Any ideas about that?– daisyCommented Dec 21, 2023 at 9:31
-
Sorry, there is a typo in my OQL - replace
it
withs
. init.decryptionCypherKey
Commented Dec 21, 2023 at 10:57 -
That was not the cause. I fixed it before posting the question, you can take look at the original screenshot, every char is encoded to AAA before joining the array ... so the problem is from somewhere else ..– daisyCommented Dec 21, 2023 at 11:13
byte[]
and you want modify it to return strings representing base64 of those byte arrays. Am I right?