Skip to content

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

Merged
ibauersachs merged 1 commit into
dnsjava:masterfrom
MMauro94:bytes
May 10, 2026
Merged

Add getXxxAsByteArray() getters and overload with escape boolean#405
ibauersachs merged 1 commit into
dnsjava:masterfrom
MMauro94:bytes

Conversation

@MMauro94

@MMauro94 MMauro94 commented Apr 13, 2026

Copy link
Copy Markdown
Contributor

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

@MMauro94 MMauro94 marked this pull request as ready for review April 13, 2026 15:51
@MMauro94

Copy link
Copy Markdown
Contributor 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.

@ibauersachs ibauersachs self-requested a review April 26, 2026 10:47

@ibauersachs ibauersachs left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Besides the inline comments, please:

  • Add a proper Javadoc to all new or changed methods, documenting the parameter
  • Mark all new methods with @since 3.6.5

Comment thread src/main/java/org/xbill/DNS/CAARecord.java Outdated
Comment thread src/main/java/org/xbill/DNS/NAPTRRecord.java Outdated
Comment thread src/main/java/org/xbill/DNS/NSAPRecord.java Outdated
Comment thread src/main/java/org/xbill/DNS/URIRecord.java Outdated
Comment thread src/main/java/org/xbill/DNS/X25Record.java Outdated
@ibauersachs ibauersachs added this to the v3.6.5 milestone Apr 26, 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 dnsjava#404
@MMauro94

MMauro94 commented May 9, 2026

Copy link
Copy Markdown
Contributor Author

Sorry it took a while, I've pushed the changes requested in the comments.

@MMauro94 MMauro94 requested a review from ibauersachs May 9, 2026 13:08
@ibauersachs ibauersachs force-pushed the master branch 2 times, most recently from a601d27 to 378aa24 Compare May 10, 2026 11:16
@sonarqubecloud

Copy link
Copy Markdown

@ibauersachs ibauersachs merged commit d9327e8 into dnsjava:master May 10, 2026
74 checks passed
@ibauersachs

Copy link
Copy Markdown
Member

Sorry it took a while, I've pushed the changes requested in the comments.

No worries. Thanks a lot!

@MMauro94 MMauro94 deleted the bytes branch May 10, 2026 11:40
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

2 participants