javavisualvmoql

Convert bytes to base64 encoded string with OQL query


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


Solution

  • 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);
    })