I discovered that Message.sectionToWire returns the wrong number of not successfully rendered records under some conditions, which leads to a malformed message being created by toWire (the length limited version).
The problem occurs if the OPT record is not at the last position in the list (which is allowed according to RFC 6891) and the list is truncated. In this case, the OPT record is counted twice.
To fix this, I would remove the "skipped" variable and would introduce a "count" variable, that counts the actually generated records. In addition, on an RRSet boundary, I would save this count in the "rendered" variable (line 430) instead of the index variable.
The provided patch is based on revision 1696 of the svn repository.
1696 is not the most recent revision - in fact, there's a patch from in a more recent revision (1699). Does this problem still occur with -head?
If you still think this is an issue/use case, please create a pull request on dnsjava's new home at Github: https://github.com/dnsjava/dnsjava