Skip to content

Add getXxxAsByteArray() getters and overload with escape boolean#405

Open
MMauro94 wants to merge 1 commit intodnsjava:masterfrom
MMauro94:bytes
Open

Add getXxxAsByteArray() getters and overload with escape boolean#405
MMauro94 wants to merge 1 commit intodnsjava:masterfrom
MMauro94:bytes

Conversation

@MMauro94
Copy link
Copy Markdown

@MMauro94 MMauro94 commented Apr 13, 2026

As discussed in the issue below, the library currently exposes character strings only as escaped strings for textual representation. This is a limitation as users are forced to deal with unnecessary escapes for application logic, and are also limited to interpret bytes as UTF-16 (as byteArrayToString casts bytes to char), whilst RFCs do not state which encoding for bytes should be used.

This commit adds:

  • getXxxAsByteArray() getters that expose the "raw" byte array, allowing full control to users. This follows the already established pattern in TXTBase with the getStringsAsByteArrays() method
  • an overload of the existing getters, which allows to pass an escape boolean: true (the default) is the current behavior, while false simply converts the bytes to a String using the UTF-8 encoding

Closes #404

As discussed in the issue below, the library currently exposes character strings only as escaped strings for textual representation. This is a limitation as users are forced to deal with unnecessary escapes for application logic, and are also limited to interpret bytes as UTF-16 (as `byteArrayToString` casts bytes to `char`), whilst RFCs do not state which encoding for bytes should be used.

This commit adds:
- `getXxxAsByteArray()` getters that expose the "raw" byte array, allowing full control to users. This follows the already established pattern in 'TXTBase` with the `getStringsAsByteArrays()` method
- an overload of the existing getters, which allows to pass an `escape` boolean: `true` (the default) is the current behavior, while `false` simply converts the bytes to a String using the UTF-8 encoding

Closes dnsjava#404
@MMauro94 MMauro94 marked this pull request as ready for review April 13, 2026 15:51
@MMauro94
Copy link
Copy Markdown
Author

Forgot to add: note that this has the unintended side-effect of exposing byte arrays outside of the record classes, which means that they could be changed outside of the class' control, hence making the class not fully immutable.

I assume that this is OK as AFAIK this library does not make any hard contracts about class mutability, especially as this pattern of exposing the byte array already exists in the aforementioned getStringsAsByteArrays().

An alternative is to return a read-only ByteBuffer wrapping our original byte array (see wrap()). However, this would make it slightly less ergonomic as there's no one-liner to convert it back to a byte array and also contains state about the position.

Another option is to copy the array each time: not great performance-wise, but data for a DNS record is quite limited anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Access to "raw" strings in NAPTR record

1 participant