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