Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ByteBuffer should have a bunch of hex dump helpers #2447

Closed
weissi opened this issue Jun 21, 2023 · 5 comments · Fixed by #2475
Closed

ByteBuffer should have a bunch of hex dump helpers #2447

weissi opened this issue Jun 21, 2023 · 5 comments · Fixed by #2475
Labels
kind/enhancement Improvements to existing feature.

Comments

@weissi
Copy link
Member

weissi commented Jun 21, 2023

I frequently need at least the following kinds of functionality, no idea how many times I've implemented them in random projects:

  1. hex dumps of a ByteBuffer one (long) line xxd -r -p compatible (example 42 41 42 41)
  2. same as 1 but length-limited (as in produce xxd compatible output but only up to 1024 bytes, if more, keep beginning and end and replace middle with [...] or so)
  3. produce multi-line hexdump -C compatible output (in full)
  4. product multi-line hexdump -C-compatible output but length limited

and probably there are others. This is mostly an API-design problem I think because if we're smart we can probably fit this all into one method like buffer.hexDump(renderer: .xxdCompatible(maxLength: 1024)) or something.

@weissi weissi added the kind/enhancement Improvements to existing feature. label Jun 21, 2023
@glbrntt
Copy link
Contributor

glbrntt commented Jun 22, 2023

Yeah this would be handy! I think I've seen something similar to this in Netty too.

@natikgadzhi
Copy link
Contributor

@weissi, @glbrntt, hi there! I don't know if I'll be out of my depth in details, but I would love to try and work through this. Would you be open to guiding me and reviewing my code?

@glbrntt
Copy link
Contributor

glbrntt commented Jul 11, 2023

hey @natikgadzhi, I'd be happy to guide you through this 🙂

As Johannes points out the API should probably be one top-level function. However, we can break this down into a few chunks and do it bit-by-bit.

I think the rough approach should be:

  1. Add a function to ByteBuffer which produces a space separated hex dump of all bytes contained in the buffer (we can use this to build the other functions). If the buffer contains the bytes of the string "foo" then this function should return the string "66 6f 6f".
  2. Add a function to ByteBuffer to produce the xxd compatible hex dump of its contents which may be length limited. For the non-length-limited case we can just call the function above. For the length limited case we need to get slices from the front and back of the buffer and feed those into the function above and join them "..." or similar.
  3. This is where the fun starts! Add another function to ByteBuffer to produce the same output as hexdump -C and another version which limits the length. This one is a bit more involved so we can revisit it when you get to it.
  4. Add a public function to ByteBuffer (all of the above should be internal) which you can provide a 'format' option to similar to what Johannes mentioned in the post above. This would just drive one of the above functions.

@natikgadzhi
Copy link
Contributor

natikgadzhi commented Jul 11, 2023

@glbrntt, thank you! Yep, the approach you laid out makes sense — I thought about the same way to build it.

Let me try things out and write a rough draft — I'll put together a PR and tag you. Aiming for Wednesday.

UPD: The week got out of hand, still working. I've got a simple hexdump to work — just adding the variations and the format argument.

UPD2: Oh, this is what you mean by "This is where the fun starts!".

@natikgadzhi
Copy link
Contributor

Almost there.

TODOs:

  1. Just got to add that hexdump -C compatible formatter that will also respect the desired max bytes length
  2. And then add the public "switcher" function, and make all other functions internal / private.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement Improvements to existing feature.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants