@@ -467,10 +467,10 @@ private int sectionToWire(DNSOutput out, int section, Compression c, int maxLeng
467467 return n - count ;
468468 }
469469
470- /* Returns true if the message could be rendered. */
471- private void toWire (DNSOutput out , int maxLength ) {
470+ /* Returns true if the message could be completely rendered (i.e. not truncated) . */
471+ private boolean toWire (DNSOutput out , int maxLength ) {
472472 if (maxLength < Header .LENGTH ) {
473- return ;
473+ return false ;
474474 }
475475
476476 int tempMaxLength = maxLength ;
@@ -529,6 +529,8 @@ private void toWire(DNSOutput out, int maxLength) {
529529 generatedTsig = tsigrec ;
530530 out .writeU16At (additionalCount + 1 , startpos + 10 );
531531 }
532+
533+ return !Header .getFlag (flags , Flags .TC );
532534 }
533535
534536 /**
@@ -546,10 +548,8 @@ public byte[] toWire() {
546548
547549 /**
548550 * Returns an array containing the wire format representation of the Message with the specified
549- * maximum length. This will generate a truncated message (with the TC bit) if the message doesn't
550- * fit, and will also sign the message with the TSIG key set by a call to {@link #setTSIG(TSIG,
551- * int, TSIGRecord)}. This method may return an empty byte array if the message could not be
552- * rendered at all; this could happen if maxLength is smaller than a DNS header, for example.
551+ * maximum length. Equivalent to calling {@link #toWire(int maxLength, boolean truncate)
552+ * toWire(maxLength, true)}.
553553 *
554554 * <p>Do NOT use this method in conjunction with {@link TSIG#apply(Message, TSIGRecord)}, it
555555 * produces inconsistent results! Use {@link #setTSIG(TSIG, int, TSIGRecord)} instead.
@@ -567,6 +567,36 @@ public byte[] toWire(int maxLength) {
567567 return out .toByteArray ();
568568 }
569569
570+ /**
571+ * Returns an array containing the wire format representation of the Message with the specified
572+ * maximum length. If {@code truncate} is {@code true} it will generate a truncated message (with
573+ * the TC bit) if the message doesn't fit, otherwise an exception will be thrown. It will also
574+ * sign the message with the TSIG key set by a call to {@link #setTSIG(TSIG, int, TSIGRecord)}.
575+ * This method may return an empty byte array if the message could not be rendered at all; this
576+ * could happen if maxLength is smaller than a DNS header, for example.
577+ *
578+ * <p>Do NOT use this method in conjunction with {@link TSIG#apply(Message, TSIGRecord)}, it
579+ * produces inconsistent results! Use {@link #setTSIG(TSIG, int, TSIGRecord)} instead.
580+ *
581+ * @param maxLength The maximum length of the message.
582+ * @return The wire format of the message, or an empty array if the message could not be rendered
583+ * into the specified length.
584+ * @throws MessageSizeExceededException When the message size would exceed the specified {@code
585+ * maxLength}.
586+ * @see Flags
587+ * @see TSIG
588+ */
589+ public byte [] toWire (int maxLength , boolean truncate ) throws MessageSizeExceededException {
590+ DNSOutput out = new DNSOutput ();
591+ boolean completelyRendered = toWire (out , maxLength );
592+ if (!completelyRendered && !truncate ) {
593+ throw new MessageSizeExceededException (maxLength );
594+ }
595+
596+ size = out .current ();
597+ return out .toByteArray ();
598+ }
599+
570600 /**
571601 * Sets the TSIG key to sign a message.
572602 *
0 commit comments