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