All Implemented Interfaces:
CharSequence, Comparable<DnsLabel>
Direct Known Subclasses:
LdhLabel, NonLdhLabel

public abstract class DnsLabelextends SafeCharSequence implements Comparable<DnsLabel>
A DNS label is an individual component of a DNS name. Labels are usually shown separated by dots.

This class implements Comparable which compares DNS labels according to the Canonical DNS Name Order as specified in RFC 4034 § 6.1.

Note that as per RFC 2181 § 11 DNS labels may contain any byte.

See Also:
  • Field Details

    • MAX_LABEL_LENGTH_IN_OCTETS

      public static final int MAX_LABEL_LENGTH_IN_OCTETS
      The maximum length of a DNS label in octets.
      See Also:
    • WILDCARD_LABEL

      public static final DnsLabel WILDCARD_LABEL
      The wildcard label.
    • VALIDATE

      private static final boolean VALIDATE
      Whether or not the DNS label is validated on construction.
      See Also:
    • label

      public final String label
      The label string.
    • internationalizedRepresentation

      private transient String internationalizedRepresentation
      Cached non-ASCII form of the label.
    • safeToStringRepresentation

      private transient String safeToStringRepresentation
      Cached printable form of the label.
    • lowercasedVariant

      private transient DnsLabel lowercasedVariant
      Cached lower-case form of the label.
    • byteCache

      private transient byte[] byteCache
      Cached wire encoding of the label.
  • Constructor Details

    • DnsLabel

      protected DnsLabel(String label)
      Stores one label, rejecting any longer than 63 octets.
      Parameters:
      label - presentation-format text of one label, checked against the 63-octet limit
  • Method Details

    • getInternationalizedRepresentation

      public final String getInternationalizedRepresentation()
      Get the internationalized representation of this label.
      Returns:
      the internationalized label
    • getInternationalizedRepresentationInternal

      protected String getInternationalizedRepresentationInternal()
      Gets the internal internationalized representation.
      Returns:
      label string
    • getLabelType

      public final String getLabelType()
      Get the type of this label.
      Returns:
      the simple class name
    • toString

      public final String toString()
      Render the label with every character outside the LDH set escaped.
      Specified by:
      toString in interface CharSequence
      Overrides:
      toString in class Object
    • getRawLabel

      public final String getRawLabel()
      Get the raw label. Note that this may return a String containing null bytes. Those Strings are notoriously difficult to handle from a security perspective. Therefore it is recommended to use toString() instead, which will return a sanitized String.
      Returns:
      the raw label.
    • equals

      public final boolean equals(Object other)
      Compare the label text against another label.
      Overrides:
      equals in class Object
    • hashCode

      public final int hashCode()
      Hash the label text.
      Overrides:
      hashCode in class Object
    • asLowercaseVariant

      public final DnsLabel asLowercaseVariant()
      Get the lowercase variant of this label.
      Returns:
      the lowercase label
    • setBytesIfRequired

      private void setBytesIfRequired()
      Fills the byte cache if it is still empty.
    • writeToBoas

      public final void writeToBoas(ByteArrayOutputStream byteArrayOutputStream)
      Write this label to a ByteArrayOutputStream prefixed by its length.
      Parameters:
      byteArrayOutputStream - the output stream to write to
    • compareTo

      public final int compareTo(DnsLabel other)
      Order labels case-insensitively, ignoring letter case.
      Specified by:
      compareTo in interface Comparable<DnsLabel>
    • from

      public static DnsLabel from(String label)
      Create a DnsLabel from a string.
      Parameters:
      label - the label string
      Returns:
      a LdhLabel if the string is a well-formed LDH label, and a NonLdhLabel otherwise
    • from

      public static DnsLabel[] from(String[] labels)
      Create DnsLabel array from string array.
      Parameters:
      labels - the label strings
      Returns:
      the DnsLabel array
    • isIdnAcePrefixed

      public static boolean isIdnAcePrefixed(String string)
      Check if a string has the IDN ACE prefix.
      Parameters:
      string - the string to check
      Returns:
      true if the string starts with "xn--"
    • toSafeRepesentation

      public static String toSafeRepesentation(String dnsLabel)
      Convert a DNS label string to a safe representation.
      Parameters:
      dnsLabel - the label to convert
      Returns:
      safe string representation
    • isLdhOrMaybeUnderscore

      private static boolean isLdhOrMaybeUnderscore(char c, boolean underscore)
      Checks whether a character is a letter, digit, dash or allowed underscore.
      Parameters:
      c - the character to check
      underscore - true to allow underscores
      Returns:
      true if the character is allowed
    • consistsOnlyOfLdhAndMaybeUnderscore

      private static boolean consistsOnlyOfLdhAndMaybeUnderscore(String string, boolean underscore)
      Checks whether a whole string is LDH plus allowed underscores.
      Parameters:
      string - the string to check
      underscore - true to allow underscores
      Returns:
      true if every character is allowed
    • consistsOnlyOfLettersDigitsAndHypen

      public static boolean consistsOnlyOfLettersDigitsAndHypen(String string)
      Check if a string consists of only letters, digits, and hyphens.
      Parameters:
      string - the string to check
      Returns:
      true if it matches LDH rules
    • consistsOnlyOfLettersDigitsHypenAndUnderscore

      public static boolean consistsOnlyOfLettersDigitsHypenAndUnderscore(String string)
      Check if a string consists of only letters, digits, hyphens, and underscores.
      Parameters:
      string - the string to check
      Returns:
      true if it matches LDH or underscore rules