src/jdk.dns.client/share/classes/jdk/dns/client/internal/DnsName.java
author aefimov
Thu, 31 Oct 2019 16:16:21 +0000
branchaefimov-dns-client-branch
changeset 58870 35c438a6d45c
child 58971 465a15dd6bed
permissions -rw-r--r--
aefimov-dns-client-branch: Add jdk.dns.client source
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58870
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
     1
/*
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
     4
 *
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    10
 *
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    15
 * accompanied this code).
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    16
 *
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    20
 *
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    23
 * questions.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    24
 */
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    25
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    26
package jdk.dns.client.internal;
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    27
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    28
import jdk.dns.client.ex.DnsInvalidNameException;
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    29
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    30
import java.util.ArrayList;
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    31
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    32
/**
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    33
 * {@code DnsName} implements compound names for DNS as specified by
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    34
 * RFCs 1034 and 1035, and as updated and clarified by RFCs 1123 and 2181.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    35
 *
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    36
 * <p> The labels in a domain name correspond to JNDI atomic names.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    37
 * Each label must be less than 64 octets in length, and only the
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    38
 * optional root label at the end of the name may be 0 octets long.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    39
 * The sum of the lengths of all labels in a name, plus the number of
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    40
 * non-root labels plus 1, must be less than 256.  The textual
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    41
 * representation of a domain name consists of the labels, escaped as
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    42
 * needed, dot-separated, and ordered right-to-left.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    43
 *
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    44
 * <p> A label consists of a sequence of octets, each of which may
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    45
 * have any value from 0 to 255.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    46
 *
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    47
 * <p> <em>Host names</em> are a subset of domain names.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    48
 * Their labels contain only ASCII letters, digits, and hyphens, and
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    49
 * none may begin or end with a hyphen.  While names not conforming to
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    50
 * these rules may be valid domain names, they will not be usable by a
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    51
 * number of DNS applications, and should in most cases be avoided.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    52
 *
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    53
 * <p> DNS does not specify an encoding (such as UTF-8) to use for
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    54
 * octets with non-ASCII values.  As of this writing there is some
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    55
 * work going on in this area, but it is not yet finalized.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    56
 * {@code DnsName} currently converts any non-ASCII octets into
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    57
 * characters using ISO-LATIN-1 encoding, in effect taking the
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    58
 * value of each octet and storing it directly into the low-order byte
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    59
 * of a Java character and <i>vice versa</i>.  As a consequence, no
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    60
 * character in a DNS name will ever have a non-zero high-order byte.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    61
 * When the work on internationalizing domain names has stabilized
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    62
 * (see for example <i>draft-ietf-idn-idna-10.txt</i>), {@code DnsName}
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    63
 * may be updated to conform to that work.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    64
 *
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    65
 * <p> Backslash ({@code \}) is used as the escape character in the
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    66
 * textual representation of a domain name.  The character sequence
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    67
 * `{@code \DDD}', where {@code DDD} is a 3-digit decimal number
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    68
 * (with leading zeros if needed), represents the octet whose value
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    69
 * is {@code DDD}.  The character sequence `{@code \C}', where
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    70
 * {@code C} is a character other than {@code '0'} through
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    71
 * {@code '9'}, represents the octet whose value is that of
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    72
 * {@code C} (again using ISO-LATIN-1 encoding); this is particularly
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    73
 * useful for escaping {@code '.'} or backslash itself.  Backslash is
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    74
 * otherwise not allowed in a domain name.  Note that escape characters
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    75
 * are interpreted when a name is parsed.  So, for example, the character
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    76
 * sequences `{@code S}', `{@code \S}', and `{@code \083}' each
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    77
 * represent the same one-octet name.  The {@code toString()} method
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    78
 * does not generally insert escape sequences except where necessary.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    79
 * If, however, the {@code DnsName} was constructed using unneeded
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    80
 * escapes, those escapes may appear in the {@code toString} result.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    81
 *
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    82
 * <p> Atomic names passed as parameters to methods of
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    83
 * {@code DnsName}, and those returned by them, are unescaped.  So,
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    84
 * for example, <code>(new&nbsp;DnsName()).add("a.b")</code> creates an
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    85
 * object representing the one-label domain name {@code a\.b}, and
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    86
 * calling {@code get(0)} on this object returns {@code "a.b"}.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    87
 *
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    88
 * <p> While DNS names are case-preserving, comparisons between them
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    89
 * are case-insensitive.  When comparing names containing non-ASCII
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    90
 * octets, {@code DnsName} uses case-insensitive comparison
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    91
 * between pairs of ASCII values, and exact binary comparison
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    92
 * otherwise.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    93
 *
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    94
 * <p> A {@code DnsName} instance is not synchronized against
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    95
 * concurrent access by multiple threads.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    96
 *
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    97
 * @author Scott Seligman
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    98
 */
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
    99
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   100
// Stripped copy with removed serialization code and jndi.Name interface
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   101
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   102
public final class DnsName {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   103
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   104
    // If non-null, the domain name represented by this DnsName.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   105
    private String domain = "";
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   106
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   107
    // The labels of this domain name, as a list of strings.  Index 0
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   108
    // corresponds to the leftmost (least significant) label:  note that
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   109
    // this is the reverse of the ordering used by the Name interface.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   110
    private ArrayList<String> labels = new ArrayList<>();
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   111
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   112
    // The number of octets needed to carry this domain name in a DNS
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   113
    // packet.  Equal to the sum of the lengths of each label, plus the
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   114
    // number of non-root labels, plus 1.  Must remain less than 256.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   115
    private short octets = 1;
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   116
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   117
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   118
    /**
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   119
     * Constructs a {@code DnsName} representing the empty domain name.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   120
     */
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   121
    public DnsName() {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   122
    }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   123
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   124
    /**
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   125
     * Constructs a {@code DnsName} representing a given domain name.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   126
     *
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   127
     * @param name the domain name to parse
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   128
     * @throws DnsInvalidNameException if {@code name} does not conform
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   129
     *                                 to DNS syntax.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   130
     */
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   131
    public DnsName(String name) throws DnsInvalidNameException {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   132
        parse(name);
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   133
    }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   134
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   135
    /*
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   136
     * Returns a new DnsName with its name components initialized to
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   137
     * the components of "n" in the range [beg,end).  Indexing is as
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   138
     * for the Name interface, with 0 being the most significant.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   139
     */
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   140
    private DnsName(DnsName n, int beg, int end) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   141
        // Compute indexes into "labels", which has least-significant label
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   142
        // at index 0 (opposite to the convention used for "beg" and "end").
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   143
        int b = n.size() - end;
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   144
        int e = n.size() - beg;
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   145
        labels.addAll(n.labels.subList(b, e));
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   146
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   147
        if (size() == n.size()) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   148
            domain = n.domain;
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   149
            octets = n.octets;
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   150
        } else {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   151
            for (String label : labels) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   152
                if (label.length() > 0) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   153
                    octets += (short) (label.length() + 1);
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   154
                }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   155
            }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   156
        }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   157
    }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   158
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   159
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   160
    public String toString() {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   161
        if (domain == null) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   162
            StringBuilder buf = new StringBuilder();
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   163
            for (String label : labels) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   164
                if (buf.length() > 0 || label.length() == 0) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   165
                    buf.append('.');
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   166
                }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   167
                escape(buf, label);
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   168
            }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   169
            domain = buf.toString();
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   170
        }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   171
        return domain;
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   172
    }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   173
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   174
    /**
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   175
     * Does this domain name follow <em>host name</em> syntax?
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   176
     */
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   177
    public boolean isHostName() {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   178
        for (String label : labels) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   179
            if (!isHostNameLabel(label)) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   180
                return false;
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   181
            }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   182
        }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   183
        return true;
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   184
    }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   185
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   186
    public short getOctets() {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   187
        return octets;
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   188
    }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   189
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   190
    public int size() {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   191
        return labels.size();
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   192
    }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   193
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   194
    public boolean isEmpty() {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   195
        return (size() == 0);
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   196
    }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   197
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   198
    public int hashCode() {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   199
        int h = 0;
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   200
        for (int i = 0; i < size(); i++) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   201
            h = 31 * h + getKey(i).hashCode();
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   202
        }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   203
        return h;
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   204
    }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   205
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   206
    public boolean equals(Object obj) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   207
        if (!(obj instanceof DnsName)) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   208
            return false;
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   209
        }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   210
        DnsName n = (DnsName) obj;
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   211
        return ((size() == n.size()) &&         // shortcut:  do sizes differ?
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   212
                (compareTo(obj) == 0));
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   213
    }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   214
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   215
    public int compareTo(Object obj) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   216
        DnsName n = (DnsName) obj;
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   217
        return compareRange(0, size(), n);      // never 0 if sizes differ
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   218
    }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   219
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   220
    public String get(int pos) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   221
        if (pos < 0 || pos >= size()) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   222
            throw new ArrayIndexOutOfBoundsException();
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   223
        }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   224
        int i = size() - pos - 1;       // index of "pos" component in "labels"
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   225
        return labels.get(i);
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   226
    }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   227
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   228
    public DnsName getPrefix(int pos) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   229
        return new DnsName(this, 0, pos);
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   230
    }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   231
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   232
    public DnsName getSuffix(int pos) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   233
        return new DnsName(this, pos, size());
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   234
    }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   235
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   236
    public Object clone() {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   237
        return new DnsName(this, 0, size());
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   238
    }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   239
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   240
    public DnsName add(int pos, String comp) throws DnsInvalidNameException {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   241
        if (pos < 0 || pos > size()) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   242
            throw new ArrayIndexOutOfBoundsException();
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   243
        }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   244
        // Check for empty labels:  may have only one, and only at end.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   245
        int len = comp.length();
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   246
        if ((pos > 0 && len == 0) ||
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   247
                (pos == 0 && hasRootLabel())) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   248
            throw new DnsInvalidNameException(
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   249
                    "Empty label must be the last label in a domain name");
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   250
        }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   251
        // Check total name length.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   252
        if (len > 0) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   253
            if (octets + len + 1 >= 256) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   254
                throw new DnsInvalidNameException("Name too long");
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   255
            }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   256
            octets += (short) (len + 1);
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   257
        }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   258
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   259
        int i = size() - pos;   // index for insertion into "labels"
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   260
        verifyLabel(comp);
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   261
        labels.add(i, comp);
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   262
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   263
        domain = null;          // invalidate "domain"
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   264
        return this;
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   265
    }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   266
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   267
    public DnsName addAll(int pos, DnsName n) throws DnsInvalidNameException {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   268
        // "n" is a DnsName so we can insert it as a whole, rather than
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   269
        // verifying and inserting it component-by-component.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   270
        // More code, but less work.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   271
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   272
        if (n.isEmpty()) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   273
            return this;
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   274
        }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   275
        // Check for empty labels:  may have only one, and only at end.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   276
        if ((pos > 0 && n.hasRootLabel()) ||
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   277
                (pos == 0 && hasRootLabel())) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   278
            throw new DnsInvalidNameException(
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   279
                    "Empty label must be the last label in a domain name");
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   280
        }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   281
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   282
        short newOctets = (short) (octets + n.octets - 1);
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   283
        if (newOctets > 255) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   284
            throw new DnsInvalidNameException("Name too long");
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   285
        }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   286
        octets = newOctets;
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   287
        int i = size() - pos;       // index for insertion into "labels"
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   288
        labels.addAll(i, n.labels);
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   289
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   290
        // Preserve "domain" if we're appending or prepending,
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   291
        // otherwise invalidate it.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   292
        if (isEmpty()) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   293
            domain = n.domain;
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   294
        } else if (domain == null || n.domain == null) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   295
            domain = null;
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   296
        } else if (pos == 0) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   297
            domain += (n.domain.equals(".") ? "" : ".") + n.domain;
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   298
        } else if (pos == size()) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   299
            domain = n.domain + (domain.equals(".") ? "" : ".") + domain;
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   300
        } else {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   301
            domain = null;
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   302
        }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   303
        return this;
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   304
    }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   305
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   306
    boolean hasRootLabel() {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   307
        return (!isEmpty() &&
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   308
                get(0).isEmpty());
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   309
    }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   310
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   311
    /*
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   312
     * Helper method for public comparison methods.  Lexicographically
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   313
     * compares components of this name in the range [beg,end) with
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   314
     * all components of "n".  Indexing is as for the Name interface,
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   315
     * with 0 being the most significant.  Returns negative, zero, or
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   316
     * positive as these name components are less than, equal to, or
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   317
     * greater than those of "n".
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   318
     */
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   319
    private int compareRange(int beg, int end, DnsName n) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   320
        // aee: Removed CompositeName ClassCastException generation here
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   321
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   322
        // Loop through labels, starting with most significant.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   323
        int minSize = Math.min(end - beg, n.size());
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   324
        for (int i = 0; i < minSize; i++) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   325
            String label1 = get(i + beg);
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   326
            String label2 = n.get(i);
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   327
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   328
            // int j = size() - (i + beg) - 1;     // index of label1 in "labels"
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   329
            // assert (label1 == labels.get(j));
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   330
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   331
            int c = compareLabels(label1, label2);
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   332
            if (c != 0) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   333
                return c;
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   334
            }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   335
        }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   336
        return ((end - beg) - n.size());        // longer range wins
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   337
    }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   338
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   339
    /*
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   340
     * Returns a key suitable for hashing the label at index i.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   341
     * Indexing is as for the Name interface, with 0 being the most
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   342
     * significant.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   343
     */
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   344
    String getKey(int i) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   345
        return keyForLabel(get(i));
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   346
    }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   347
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   348
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   349
    /*
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   350
     * Parses a domain name, setting the values of instance vars accordingly.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   351
     */
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   352
    private void parse(String name) throws DnsInvalidNameException {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   353
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   354
        StringBuilder label = new StringBuilder();      // label being parsed
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   355
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   356
        for (int i = 0; i < name.length(); i++) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   357
            char c = name.charAt(i);
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   358
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   359
            if (c == '\\') {                    // found an escape sequence
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   360
                c = getEscapedOctet(name, i++);
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   361
                if (isDigit(name.charAt(i))) {  // sequence is \DDD
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   362
                    i += 2;                     // consume remaining digits
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   363
                }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   364
                label.append(c);
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   365
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   366
            } else if (c != '.') {              // an unescaped octet
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   367
                label.append(c);
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   368
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   369
            } else {                            // found '.' separator
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   370
                add(0, label.toString());       // check syntax, then add label
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   371
                //   to end of name
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   372
                label.delete(0, i);             // clear buffer for next label
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   373
            }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   374
        }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   375
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   376
        // If name is neither "." nor "", the octets (zero or more)
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   377
        // from the rightmost dot onward are now added as the final
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   378
        // label of the name.  Those two are special cases in that for
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   379
        // all other domain names, the number of labels is one greater
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   380
        // than the number of dot separators.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   381
        if (!name.isEmpty() && !name.equals(".")) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   382
            add(0, label.toString());
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   383
        }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   384
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   385
        domain = name;          // do this last, since add() sets it to null
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   386
    }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   387
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   388
    /*
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   389
     * Returns (as a char) the octet indicated by the escape sequence
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   390
     * at a given position within a domain name.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   391
     * @throws InvalidNameException if a valid escape sequence is not found.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   392
     */
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   393
    private static char getEscapedOctet(String name, int pos)
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   394
            throws DnsInvalidNameException {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   395
        try {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   396
            // assert (name.charAt(pos) == '\\');
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   397
            char c1 = name.charAt(++pos);
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   398
            if (isDigit(c1)) {          // sequence is `\DDD'
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   399
                char c2 = name.charAt(++pos);
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   400
                char c3 = name.charAt(++pos);
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   401
                if (isDigit(c2) && isDigit(c3)) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   402
                    return (char)
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   403
                            ((c1 - '0') * 100 + (c2 - '0') * 10 + (c3 - '0'));
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   404
                } else {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   405
                    throw new DnsInvalidNameException(
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   406
                            "Invalid escape sequence in " + name);
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   407
                }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   408
            } else {                    // sequence is `\C'
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   409
                return c1;
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   410
            }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   411
        } catch (IndexOutOfBoundsException e) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   412
            throw new DnsInvalidNameException(
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   413
                    "Invalid escape sequence in " + name);
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   414
        }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   415
    }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   416
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   417
    /*
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   418
     * Checks that this label is valid.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   419
     * @throws InvalidNameException if label is not valid.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   420
     */
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   421
    private static void verifyLabel(String label) throws DnsInvalidNameException {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   422
        if (label.length() > 63) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   423
            throw new DnsInvalidNameException(
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   424
                    "Label exceeds 63 octets: " + label);
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   425
        }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   426
        // Check for two-byte characters.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   427
        for (int i = 0; i < label.length(); i++) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   428
            char c = label.charAt(i);
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   429
            if ((c & 0xFF00) != 0) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   430
                throw new DnsInvalidNameException(
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   431
                        "Label has two-byte char: " + label);
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   432
            }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   433
        }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   434
    }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   435
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   436
    /*
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   437
     * Does this label conform to host name syntax?
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   438
     */
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   439
    private static boolean isHostNameLabel(String label) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   440
        for (int i = 0; i < label.length(); i++) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   441
            char c = label.charAt(i);
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   442
            if (!isHostNameChar(c)) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   443
                return false;
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   444
            }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   445
        }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   446
        return !(label.startsWith("-") || label.endsWith("-"));
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   447
    }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   448
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   449
    private static boolean isHostNameChar(char c) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   450
        return (c == '-' ||
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   451
                c >= 'a' && c <= 'z' ||
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   452
                c >= 'A' && c <= 'Z' ||
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   453
                c >= '0' && c <= '9');
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   454
    }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   455
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   456
    private static boolean isDigit(char c) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   457
        return (c >= '0' && c <= '9');
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   458
    }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   459
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   460
    /*
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   461
     * Append a label to buf, escaping as needed.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   462
     */
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   463
    private static void escape(StringBuilder buf, String label) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   464
        for (int i = 0; i < label.length(); i++) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   465
            char c = label.charAt(i);
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   466
            if (c == '.' || c == '\\') {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   467
                buf.append('\\');
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   468
            }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   469
            buf.append(c);
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   470
        }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   471
    }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   472
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   473
    /*
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   474
     * Compares two labels, ignoring case for ASCII values.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   475
     * Returns negative, zero, or positive as the first label
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   476
     * is less than, equal to, or greater than the second.
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   477
     * See keyForLabel().
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   478
     */
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   479
    private static int compareLabels(String label1, String label2) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   480
        int min = Math.min(label1.length(), label2.length());
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   481
        for (int i = 0; i < min; i++) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   482
            char c1 = label1.charAt(i);
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   483
            char c2 = label2.charAt(i);
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   484
            if (c1 >= 'A' && c1 <= 'Z') {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   485
                c1 += 'a' - 'A';                        // to lower case
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   486
            }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   487
            if (c2 >= 'A' && c2 <= 'Z') {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   488
                c2 += 'a' - 'A';                        // to lower case
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   489
            }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   490
            if (c1 != c2) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   491
                return (c1 - c2);
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   492
            }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   493
        }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   494
        return (label1.length() - label2.length());     // the longer one wins
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   495
    }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   496
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   497
    /*
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   498
     * Returns a key suitable for hashing a label.  Two labels map to
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   499
     * the same key iff they are equal, taking possible case-folding
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   500
     * into account.  See compareLabels().
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   501
     */
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   502
    private static String keyForLabel(String label) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   503
        StringBuilder sb = new StringBuilder(label.length());
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   504
        for (int i = 0; i < label.length(); i++) {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   505
            char c = label.charAt(i);
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   506
            if (c >= 'A' && c <= 'Z') {
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   507
                c += 'a' - 'A';                         // to lower case
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   508
            }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   509
            sb.append(c);
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   510
        }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   511
        return sb.toString();
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   512
    }
35c438a6d45c aefimov-dns-client-branch: Add jdk.dns.client source
aefimov
parents:
diff changeset
   513
}