net.sf.ivmaidns.dns
Class DNSName

java.lang.Object
  extended by net.sf.ivmaidns.dns.DNSName
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, Immutable, ReallyCloneable, Sortable, Verifiable

public final class DNSName
extends java.lang.Object
implements Immutable, ReallyCloneable, java.io.Serializable, Sortable, Verifiable

Class for representing DNS resource name (as defined in RFC1035).

Version:
3.0
Author:
Ivan Maidanski
See Also:
Serialized Form

Field Summary
protected  byte[] bytes
          NOTE: bytes !
static int COMPRESSED_NAME_TAG
           
static char EMAIL
           
static int ERROR_BAD_CHAR
          NOTE: These are possible error codes for DNSName(name, domain) and parse(name, escapeSeparator, domainBytes, domainOffset).
static int ERROR_BAD_ESCAPING
           
static int ERROR_EMPTY_LABEL
           
static int ERROR_LONG_NAME
           
static int ERROR_UNSUPPORTED
           
static char ESCAPE
           
static DNSName IN_ADDR_ARPA
           
static DNSName IP6_INT
           
static int MAX_LABEL_LEN
           
static char QUOTE
           
static DNSName ROOT
           
static char SEPARATOR
           
static char THIS_ZONE
           
static char WILDCARD
           
 
Constructor Summary
DNSName(byte[] bytes, int offset)
          NOTE: bytes must be !
DNSName(DNSName name)
          NOTE: name must be !
DNSName(java.lang.String name, DNSName domain)
          NOTE: name must be !
 
Method Summary
static boolean canonize(byte[] bytes, int offset)
          NOTE: bytes must be valid (and not compressed).
 java.lang.Object clone()
          Creates and returns a copy of this object.
static int compareNames(byte[] bytesA, int offsetA, byte[] bytesB, int offsetB)
          NOTE: bytesA and bytesB must be valid (and not compressed).
 int compareTo(DNSName name)
          NOTE: name must be !
static int compressAt(byte[] msgBytes, int offset, int baseNameOffset)
          NOTE: msgBytes must be valid (at offset and at baseNameOffset) and not compressed.
static int countLabels(byte[] bytes, int offset)
          NOTE: bytes must be valid (and not compressed).
static byte[] decompress(byte[] bytes, int offset, byte[] msgBytes)
          NOTE: bytes and msgBytes must be !
 boolean equals(java.lang.Object obj)
          NOTE: Letters case is ignored (according to RFC2065).
 boolean equalsExact(DNSName name)
          NOTE: name may be == null.
 boolean equalsWildcard(DNSName wildcard)
          NOTE: wildcard may be == null.
 java.lang.String getAbsolute()
          NOTE: Letters case in result is not adjusted to canonical.
 java.lang.String getAsEmail()
          NOTE: Result is e-mail string represenation of DNS name.
 byte[] getBytes()
          NOTE: Result !
 int getBytesLen()
          NOTE: Result > 0.
 DNSName getDomain()
          NOTE: Result !
 DNSName getDomain(int level)
          NOTE: Negative level is treated as zero.
 java.lang.String getLabelAt(int level)
           
 java.lang.String[] getLabels()
          NOTE: All separator characters in labels (if exist) are escaped.
 java.lang.String getLastLabel()
           
 int getLevel()
           
 java.lang.String getRelative(DNSName domain)
          NOTE: domain may be == null.
 java.lang.String getRelativeAt(int level)
           
 boolean greaterThan(java.lang.Object obj)
          NOTE: Method for canonical ordering (according to RFC2065).
 int hashCode()
           
static int hashCode(byte[] bytes, int offset)
          NOTE: bytes must be valid (and not compressed).
 void integrityCheck()
          NOTE: Check object for its integrity.
 boolean isCaseSensitive()
           
 boolean isInDomain(DNSName domain, boolean strict)
          NOTE: domain may be == null.
 boolean isWildcard()
           
 java.lang.String joinLabels(java.lang.String[] labels, int level, int count, boolean absolute)
           
static int labelOffset(byte[] bytes, int offset, int backLevel)
          NOTE: bytes must be valid (and not compressed).
static int lengthOf(byte[] bytes, int offset)
          NOTE: bytes must be !
static byte[] parse(java.lang.String name, boolean escapeSeparator, byte[] domainBytes, int domainOffset)
          NOTE: name must be !
 int putTo(byte[] bytes, int offset)
          NOTE: bytes must be !
 boolean reversed()
           
 DNSName root()
           
 char separator()
           
 java.lang.String toString()
          NOTE: Result is 'in-line'.
static java.lang.String toString(byte[] bytes, int offset, int labelsCount, boolean escapeSeparator, boolean absolute)
          NOTE: bytes must be valid (and not compressed).
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

SEPARATOR

public static final char SEPARATOR
See Also:
Constant Field Values

WILDCARD

public static final char WILDCARD
See Also:
Constant Field Values

ESCAPE

public static final char ESCAPE
See Also:
Constant Field Values

THIS_ZONE

public static final char THIS_ZONE
See Also:
Constant Field Values

EMAIL

public static final char EMAIL
See Also:
Constant Field Values

QUOTE

public static final char QUOTE
See Also:
Constant Field Values

MAX_LABEL_LEN

public static final int MAX_LABEL_LEN
See Also:
Constant Field Values

COMPRESSED_NAME_TAG

public static final int COMPRESSED_NAME_TAG
See Also:
Constant Field Values

ERROR_BAD_CHAR

public static final int ERROR_BAD_CHAR
NOTE: These are possible error codes for DNSName(name, domain) and parse(name, escapeSeparator, domainBytes, domainOffset).

Since:
3.0
See Also:
Constant Field Values

ERROR_BAD_ESCAPING

public static final int ERROR_BAD_ESCAPING
See Also:
Constant Field Values

ERROR_EMPTY_LABEL

public static final int ERROR_EMPTY_LABEL
See Also:
Constant Field Values

ERROR_LONG_NAME

public static final int ERROR_LONG_NAME
See Also:
Constant Field Values

ERROR_UNSUPPORTED

public static final int ERROR_UNSUPPORTED
See Also:
Constant Field Values

ROOT

public static final DNSName ROOT

IN_ADDR_ARPA

public static final DNSName IN_ADDR_ARPA

IP6_INT

public static final DNSName IP6_INT

bytes

protected final byte[] bytes
NOTE: bytes != null. bytes length must be == lengthOf(bytes, 0). bytes may be not canonical, bytes cannot be compressed. bytes content is described in RFC1035.

Constructor Detail

DNSName

public DNSName(java.lang.String name,
               DNSName domain)
        throws java.lang.NullPointerException,
               ParserException
NOTE: name must be != null, name may be == "", name may be THIS_ZONE. domain may be == null. ParserException(name, index, error) is thrown only if name cannot be parsed.

Throws:
java.lang.NullPointerException
ParserException
Since:
2.0

DNSName

public DNSName(DNSName name)
        throws java.lang.NullPointerException
NOTE: name must be != null. Constructed DNS name is canonized.

Throws:
java.lang.NullPointerException
Since:
2.5

DNSName

public DNSName(byte[] bytes,
               int offset)
        throws java.lang.NullPointerException,
               java.lang.ArrayIndexOutOfBoundsException,
               java.lang.IllegalArgumentException
NOTE: bytes must be != null. ArrayIndexOutOfBoundsException is thrown only if 0 > offset or offset >= bytes length. Decompression is supported. If bytes content is not valid then IllegalArgumentException is thrown. bytes array is not changed anyway.

Throws:
java.lang.NullPointerException
java.lang.ArrayIndexOutOfBoundsException
java.lang.IllegalArgumentException
Since:
2.1
Method Detail

putTo

public int putTo(byte[] bytes,
                 int offset)
          throws java.lang.NullPointerException,
                 java.lang.ArrayIndexOutOfBoundsException
NOTE: bytes must be != null. Method for putting resource name to byte array. No compression. Enough capacity must be provided (at least getBytesLen() bytes). Result is new offset (just after this name).

Throws:
java.lang.NullPointerException
java.lang.ArrayIndexOutOfBoundsException
Since:
2.5

getBytes

public final byte[] getBytes()
NOTE: Result != null, result length > 0, result is copy.


getBytesLen

public final int getBytesLen()
NOTE: Result > 0.

Since:
2.1

getAbsolute

public java.lang.String getAbsolute()
NOTE: Letters case in result is not adjusted to canonical.


getLabels

public java.lang.String[] getLabels()
NOTE: All separator characters in labels (if exist) are escaped.


getLabelAt

public java.lang.String getLabelAt(int level)
                            throws java.lang.ArrayIndexOutOfBoundsException
Throws:
java.lang.ArrayIndexOutOfBoundsException

getLevel

public int getLevel()

getRelative

public java.lang.String getRelative(DNSName domain)
NOTE: domain may be == null. Result != null, result != "". THIS_ZONE may be returned.


isInDomain

public boolean isInDomain(DNSName domain,
                          boolean strict)
NOTE: domain may be == null. Letters case is ignored.


getDomain

public DNSName getDomain(int level)
                  throws java.lang.ArrayIndexOutOfBoundsException
NOTE: Negative level is treated as zero. Result != null.

Throws:
java.lang.ArrayIndexOutOfBoundsException

getDomain

public DNSName getDomain()
NOTE: Result != null.


getLastLabel

public java.lang.String getLastLabel()

getRelativeAt

public java.lang.String getRelativeAt(int level)
                               throws java.lang.ArrayIndexOutOfBoundsException
Throws:
java.lang.ArrayIndexOutOfBoundsException

isCaseSensitive

public boolean isCaseSensitive()

joinLabels

public java.lang.String joinLabels(java.lang.String[] labels,
                                   int level,
                                   int count,
                                   boolean absolute)
                            throws java.lang.NullPointerException,
                                   java.lang.ArrayIndexOutOfBoundsException
Throws:
java.lang.NullPointerException
java.lang.ArrayIndexOutOfBoundsException

reversed

public final boolean reversed()

separator

public final char separator()

root

public DNSName root()

lengthOf

public static final int lengthOf(byte[] bytes,
                                 int offset)
                          throws java.lang.NullPointerException,
                                 java.lang.ArrayIndexOutOfBoundsException
NOTE: bytes must be != null. ArrayIndexOutOfBoundsException is thrown only if 0 > offset or offset >= bytes length. If result > 0 then bytes content is valid (and not compressed), else if 0 > result then bytes content is compressed with result length (compressed), else name is not valid.

Throws:
java.lang.NullPointerException
java.lang.ArrayIndexOutOfBoundsException

decompress

public static final byte[] decompress(byte[] bytes,
                                      int offset,
                                      byte[] msgBytes)
                               throws java.lang.NullPointerException,
                                      java.lang.ArrayIndexOutOfBoundsException
NOTE: bytes and msgBytes must be != null. If bytes content is valid and not compressed then result == bytes. Else if compressed-label tag is found then name is decompressed to new bytes array, preserving (copying) content from bytes array before and after specified name. After processing all compressed-label tags (in specified name) new bytes array is returned (if decompression fails then result == null). ArrayIndexOutOfBoundsException is thrown only if 0 > offset or offset >= bytes length. bytes and msgBytes arrays are not changed anyway.

Throws:
java.lang.NullPointerException
java.lang.ArrayIndexOutOfBoundsException

compressAt

public static final int compressAt(byte[] msgBytes,
                                   int offset,
                                   int baseNameOffset)
                            throws java.lang.NullPointerException,
                                   java.lang.ArrayIndexOutOfBoundsException
NOTE: msgBytes must be valid (at offset and at baseNameOffset) and not compressed. If possible then name at offset is compressed (using only content at baseNameOffset). msgBytes array is altered. Result is new offset (after processed name).

Throws:
java.lang.NullPointerException
java.lang.ArrayIndexOutOfBoundsException
Since:
2.5

canonize

public static final boolean canonize(byte[] bytes,
                                     int offset)
                              throws java.lang.NullPointerException,
                                     java.lang.ArrayIndexOutOfBoundsException
NOTE: bytes must be valid (and not compressed). Upper-case letters are converted to lower-case. Result is true if and only if bytes array is altered.

Throws:
java.lang.NullPointerException
java.lang.ArrayIndexOutOfBoundsException
Since:
2.5

countLabels

public static final int countLabels(byte[] bytes,
                                    int offset)
                             throws java.lang.NullPointerException,
                                    java.lang.ArrayIndexOutOfBoundsException
NOTE: bytes must be valid (and not compressed). Result >= 0.

Throws:
java.lang.NullPointerException
java.lang.ArrayIndexOutOfBoundsException

labelOffset

public static final int labelOffset(byte[] bytes,
                                    int offset,
                                    int backLevel)
                             throws java.lang.NullPointerException,
                                    java.lang.ArrayIndexOutOfBoundsException
NOTE: bytes must be valid (and not compressed). Negative backLevel is treated as zero. Result >= offset.

Throws:
java.lang.NullPointerException
java.lang.ArrayIndexOutOfBoundsException

hashCode

public static final int hashCode(byte[] bytes,
                                 int offset)
                          throws java.lang.NullPointerException,
                                 java.lang.ArrayIndexOutOfBoundsException
NOTE: bytes must be valid (and not compressed). Upper-case letters are treated as lower-case ones.

Throws:
java.lang.NullPointerException
java.lang.ArrayIndexOutOfBoundsException

compareNames

public static final int compareNames(byte[] bytesA,
                                     int offsetA,
                                     byte[] bytesB,
                                     int offsetB)
                              throws java.lang.NullPointerException,
                                     java.lang.ArrayIndexOutOfBoundsException
NOTE: bytesA and bytesB must be valid (and not compressed). Names are compared in label-by-label manner, starting at level 0. Upper-case letters are treated as lower-case (as defined in RFC2065).

Throws:
java.lang.NullPointerException
java.lang.ArrayIndexOutOfBoundsException

toString

public static final java.lang.String toString(byte[] bytes,
                                              int offset,
                                              int labelsCount,
                                              boolean escapeSeparator,
                                              boolean absolute)
                                       throws java.lang.NullPointerException,
                                              java.lang.ArrayIndexOutOfBoundsException
NOTE: bytes must be valid (and not compressed). If !escapeSeparator then separator characters in labels (if any) are not escaped. Result != null, result is escaped, result is 'in-line'. Negative labelsCount is treated as zero.

Throws:
java.lang.NullPointerException
java.lang.ArrayIndexOutOfBoundsException

parse

public static final byte[] parse(java.lang.String name,
                                 boolean escapeSeparator,
                                 byte[] domainBytes,
                                 int domainOffset)
                          throws java.lang.NullPointerException,
                                 ParserException,
                                 java.lang.ArrayIndexOutOfBoundsException
NOTE: name must be != null, domainBytes must be != null. If name is not absolute then name is relative to the specified domain. Escaping in name is allowed. name must not contain non-printable characters. Result != null. ParserException(name, index, error) is thrown only if name cannot be parsed.

Throws:
java.lang.NullPointerException
ParserException
java.lang.ArrayIndexOutOfBoundsException

getAsEmail

public java.lang.String getAsEmail()
NOTE: Result is e-mail string represenation of DNS name. Result != null, result is 'in-line'. Result == "" only if getLevel() == 0.


isWildcard

public boolean isWildcard()

equalsWildcard

public boolean equalsWildcard(DNSName wildcard)
NOTE: wildcard may be == null. Letters case is ignored.

Since:
2.0

equalsExact

public boolean equalsExact(DNSName name)
NOTE: name may be == null. This comparison is case-sensitive.

Since:
2.5

clone

public java.lang.Object clone()
Description copied from interface: ReallyCloneable
Creates and returns a copy of this object. By the clone() standard definition, this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object. Typically, native clone() method of Object class is used inside this method.

Specified by:
clone in interface ReallyCloneable
Overrides:
clone in class java.lang.Object
Returns:
a copy (not null and != this) of this instance.

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

equals

public boolean equals(java.lang.Object obj)
NOTE: Letters case is ignored (according to RFC2065).

Overrides:
equals in class java.lang.Object

greaterThan

public boolean greaterThan(java.lang.Object obj)
NOTE: Method for canonical ordering (according to RFC2065).

Specified by:
greaterThan in interface Sortable
Parameters:
obj - the second compared object (may be null).
Returns:
true if obj is comparable with this and this object is greater than obj, else false.
Since:
2.5

compareTo

public int compareTo(DNSName name)
              throws java.lang.NullPointerException
NOTE: name must be != null.

Throws:
java.lang.NullPointerException

toString

public java.lang.String toString()
NOTE: Result is 'in-line'.

Overrides:
toString in class java.lang.Object

integrityCheck

public void integrityCheck()
NOTE: Check object for its integrity. For debug purpose only.

Specified by:
integrityCheck in interface Verifiable
Since:
2.5