1

I run application using idea and dump heap useing jvisualvm. a byte[] instance looks like 5, 0, -110, 104, 6, 9, 1, 2, -112, -128, -127, 0, 9, -110, 100, 2, 5, 84, 114, 101, 101, 115, 10, 2, 3, 4, 1, 3, 97, 112, 105, 10, 2, 5, 6, 1, 7, 114, 101, 102, 108, 101, 99, 116, 10, 1, 7, 1, 5, 115, 99, 97, 108, 97, 3, 0, 19, 2, 0, 10, 16, 2, 11, 12, 13, 1... too many byte[] enter image description here I want to see byte message , but the result turns Garbled, what should i do?

System.out.println(Charset.defaultCharset());  // UTF-8
byte[] bytes = {5, 0, -110, 104, 6, 9, 1, 2, -112...};
System.out.println(new String(bytes, "UTF-8"));
roamer
  • 143
  • 1
  • 9
  • 1
    Are you sure that it's actually String data? – Andy Turner Sep 30 '20 at 11:42
  • 2
    There are some ascii strings hidden in the data you’ve posted (four, to be precise), each of them being preceded by its length as a single byte, but there are also other data in between. Seems to be Scala reflection information. – Holger Sep 30 '20 at 12:42
  • @AndyTurner maybe i choose wrong one. another example `123, 34, 105, 110, 100, 101, 120, 34, 58, 123, 34, 95, 105, 100, 34, 58, 34, 100, 100, 55, 97, 55, 50, 97, 56, 99, 55, 101, 50, 56, 50, 55, 57, 97, 55, 55, 54, 97, 51, 55, 52, 97, 48, 57, 50, 57, 102, 57, 101, 34, 125, 125, 10, 123, 34, 111, 118, 101, 114, 118` It is a kafka message – roamer Oct 01 '20 at 14:20
  • @Holger I can decode ascii byte[], but i do not know how to deal with others `String str = FileUtils.readFileToString(file, "utf-8"); String[] list = str.split(","); for (String s : list) { byte b = Byte.valueOf(s.trim()); char c = (char) b; System.out.print(c); i++;} ` – roamer Oct 01 '20 at 14:23
  • 1
    I’m not sure what you are trying to do. If the byte array truly is the backing array of a `String` instance, you only need do expand the `` tree node in the heap dump analyzer and click on the referencing string object, to see the content as decoded string. Or browse the string instances in the first place. If the byte array is not an actual string, it can be anything and we don’t know how to decode it. By the way, the code of your comment can be simplified to `for(String s: str.split(",\\s*")) System.out.printf("%c", Byte.valueOf(s));` – Holger Oct 01 '20 at 15:19

0 Answers0