5

How do I print out the values of the byte array all at once? I seem to recall I could specify a memory range in gdb. Is similar functionality is available in jdb?

I have a Java byte array:

byte [] decompressed = new byte[OUTPUT_FILE_IO_BUFFER_SIZE];

which I populate from a String:

System.arraycopy(decompressedString.getBytes(), 0, decompressed, 0, 
                         decompressedString.length());

In jdb, I want to print the contents of the byte array. I tried

main[1] print decompressed

which returns:

 decompressed = instance of byte[7] (id=342)

2 Answers 2

6

One solution:

dump decompressed

This dumps the byte values! :)

Sign up to request clarification or add additional context in comments.

1 Comment

it's printing them as signed integers -- is there any way to get it to print hex values? Local variables: prefix = instance of byte[16] (id=2384) Main Thread[1] dump prefix prefix = { -118, -113, -73, -125, 13, -42, -63, 87, -10, 54, -122, 57, -63, 3, 81, 116 }
0

It is a bit lengthy, but it can display all the elements in a line.

print java.util.Arrays.toString(decompressed)

Even if the array was a boxed-type array (like Byte[]), Arrays.asList could also provide the same. It is slightly shorter.

print java.util.Arrays.asList(decompressed)

2 Comments

Arrays.asList() will not work - It does not take primitives as variable-arity argument - in posted example it will create a List with one single element: the array itself. So the output will be something like [[B@12345678]!
@user85421, you're right. Arrays.asList does not work. I confused with its behaviour for byte[] and Byte[]. In my personal issue, the target array was of boxed-type, and it made Arrays.asList return a List<Byte> instance, then print command outputs the same result as Array.toString. On the other hand, Arrays.asList returns a List<byte[]> instance when byte[] is given, then print command outputs the result you mentioned. I updated my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.