0

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?

enter image description here

2
  • Just to understand you question better: you have OQL query, which returns instances of byte[] and you want modify it to return strings representing base64 of those byte arrays. Am I right? Commented Dec 20, 2023 at 12:13
  • @TomasHurka Yep
    – daisy
    Commented Dec 20, 2023 at 12:36

1 Answer 1

1

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);
})
6
  • Is it possible to join the whole array? The current output encodes each of them separately and not encoded correctly ibb.co/B4PVJZ9
    – daisy
    Commented 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?
    – daisy
    Commented Dec 21, 2023 at 9:31
  • Sorry, there is a typo in my OQL - replace it with s. in it.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 ..
    – daisy
    Commented Dec 21, 2023 at 11:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.