API Overview API Index Package Overview Direct link to this page
JDK 1.6
  javax.naming.ldap. Rdn View Javadoc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751

/*
 * @(#)Rdn.java	1.9 05/11/17
 *
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package javax.naming.ldap;

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.ArrayList;
import java.util.Collections;

import javax.naming.InvalidNameException;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.Attributes;
import javax.naming.directory.Attribute;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;

import java.io.Serializable;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.IOException;

/**
 * This class represents a relative distinguished name, or RDN, which is a
 * component of a distinguished name as specified by
 * <a href="http://ietf.org/rfc/rfc2253.txt">RFC 2253</a>.
 * An example of an RDN is "OU=Sales+CN=J.Smith". In this example,
 * the RDN consist of multiple attribute type/value pairs. The
 * RDN is parsed as described in the class description for
 * {@link javax.naming.ldap.LdapName <tt>LdapName</tt>}.
 * <p>
 * The Rdn class represents an RDN as attribute type/value mappings,
 * which can be viewed using
 * {@link javax.naming.directory.Attributes Attributes}.
 * In addition, it contains convenience methods that allow easy retrieval
 * of type and value when the Rdn consist of a single type/value pair,
 * which is how it appears in a typical usage.
 * It also contains helper methods that allow escaping of the unformatted
 * attribute value and unescaping of the value formatted according to the
 * escaping syntax defined in RFC2253. For methods that take or return
 * attribute value as an Object, the value is either a String
 * (in unescaped form) or a byte array.
 * <p>
 * <code>Rdn</code> will properly parse all valid RDNs, but
 * does not attempt to detect all possible violations when parsing
 * invalid RDNs. It is "generous" in accepting invalid RDNs.
 * The "validity" of a name is determined ultimately when it
 * is supplied to an LDAP server, which may accept or
 * reject the name based on factors such as its schema information
 * and interoperability considerations.
 *
 * <p>
 * The following code example shows how to construct an Rdn using the
 * constructor that takes type and value as arguments:
 * <pre>
 *	Rdn rdn = new Rdn("cn", "Juicy, Fruit");
 *	System.out.println(rdn.toString());
 * </pre>
 * The last line will print <tt>cn=Juicy\, Fruit</tt>. The
 * {@link #unescapeValue(String) <tt>unescapeValue()</tt>} method can be
 * used to unescape the escaped comma resulting in the original
 * value <tt>"Juicy, Fruit"</tt>. The {@link #escapeValue(Object)
 * <tt>escapeValue()</tt>} method adds the escape back preceding the comma.
 * <p>
 * This class can be instantiated by a string representation
 * of the RDN defined in RFC 2253 as shown in the following code example:
 * <pre>
 * 	Rdn rdn = new Rdn("cn=Juicy\\, Fruit");
 *	System.out.println(rdn.toString());
 * </pre>
 * The last line will print <tt>cn=Juicy\, Fruit</tt>.
 * <p>
 * Concurrent multithreaded read-only access of an instance of
 * <tt>Rdn</tt> need not be synchronized.
 * <p>
 * Unless otherwise noted, the behavior of passing a null argument
 * to a constructor or method in this class will cause NullPointerException
 * to be thrown.
 *
 * @version 1.9 05/11/17
 * @since 1.5
 */

public class Rdn implements Serializable, Comparable<Object> {

    // private transient ArrayList<RdnEntry> entries;
    private transient ArrayList entries;

    // The common case.
    private static final int DEFAULT_SIZE = 1;

    private static final long serialVersionUID = -5994465067210009656L;

    /**
     * Constructs an Rdn from the given attribute set. See
     * {@link javax.naming.directory.Attributes Attributes}.
     * <p>
     * The string attribute values are not interpretted as
     * <a href="http://ietf.org/rfc/rfc2253.txt">RFC 2253</a>
     * formatted RDN strings. That is, the values are used
     * literally (not parsed) and assumed to be unescaped.
     *
     * @param attrSet The non-null and non-empty attributes containing
     * type/value mappings.
     * @throws InvalidNameException If contents of <tt>attrSet</tt> cannot
     * 		be used to construct a valid RDN.
     */
    public Rdn(Attributes attrSet) throws InvalidNameException {
	if (attrSet.size() == 0) {
	    throw new InvalidNameException("Attributes cannot be empty");
	}
	entries = new ArrayList(attrSet.size());
	NamingEnumeration attrs = attrSet.getAll();
	try {
	    for (int nEntries = 0; attrs.hasMore(); nEntries++) {
		RdnEntry entry = new RdnEntry();
		Attribute attr = (Attribute) attrs.next();
		entry.type = attr.getID();
		entry.value = attr.get();
		entries.add(nEntries, entry);
	    }
	} catch (NamingException e) {
	    InvalidNameException e2 = new InvalidNameException(
					e.getMessage());
	    e2.initCause(e);
	    throw e2;
	}
	sort(); // arrange entries for comparison
    }

    /**
     * Constructs an Rdn from the given string.
     * This constructor takes a string formatted according to the rules
     * defined in <a href="http://ietf.org//rfc/rfc2253.txt">RFC 2253</a>
     * and described in the class description for
     * {@link javax.naming.ldap.LdapName}.
     *
     * @param rdnString	The non-null and non-empty RFC2253 formatted string.
     * @throws InvalidNameException If a syntax error occurs during
     * 			parsing of the rdnString.
     */
    public Rdn(String rdnString) throws InvalidNameException {
	entries = new ArrayList(DEFAULT_SIZE);
	(new Rfc2253Parser(rdnString)).parseRdn(this);
    }

    /**
     * Constructs an Rdn from the given <tt>rdn</tt>.
     * The contents of the <tt>rdn</tt> are simply copied into the newly
     * created Rdn.
     * @param rdn The non-null Rdn to be copied.
     */
    public Rdn(Rdn rdn) {
	entries = new ArrayList(rdn.entries.size());
	entries.addAll(rdn.entries);
    }

    /**
     * Constructs an Rdn from the given attribute type and
     * value.
     * The string attribute values are not interpretted as
     * <a href="http://ietf.org/rfc/rfc2253.txt">RFC 2253</a>
     * formatted RDN strings. That is, the values are used
     * literally (not parsed) and assumed to be unescaped.
     *
     * @param type The non-null and non-empty string attribute type.
     * @param value The non-null and non-empty attribute value.
     * @throws InvalidNameException If type/value cannot be used to
     *			construct a valid RDN.
     * @see #toString()
     */
    public Rdn(String type, Object value) throws InvalidNameException {
	if (value == null) {
	    throw new NullPointerException("Cannot set value to null");
	}
	if (type.equals("") || isEmptyValue(value)) {
	    throw new InvalidNameException(
		"type or value cannot be empty, type:" + type +
		" value:" + value);
	}
	entries = new ArrayList(DEFAULT_SIZE);
	put(type, value);
    }

    private boolean isEmptyValue(Object val) {
	return ((val instanceof String) && val.equals("")) ||
	((val instanceof byte[]) && (((byte[]) val).length == 0));
    }

    // An empty constructor used by the parser
    Rdn() {
	entries = new ArrayList(DEFAULT_SIZE);
    }

    /*
     * Adds the given attribute type and value to this Rdn.
     * The string attribute values are not interpretted as
     * <a href="http://ietf.org/rfc/rfc2253.txt">RFC 2253</a>
     * formatted RDN strings. That is the values are used
     * literally (not parsed) and assumed to be unescaped.
     *
     * @param type The non-null and non-empty string attribute type.
     * @param value The non-null and non-empty attribute value.
     * @return The updated Rdn, not a new one. Cannot be null.
     * @see #toString()
     */
    Rdn put(String type, Object value) {

	// create new Entry
	RdnEntry newEntry = new RdnEntry();
	newEntry.type =  type;
        if (value instanceof byte[]) {	// clone the byte array
	    newEntry.value = ((byte[]) value).clone();
	} else {
	    newEntry.value = value;
	}
	entries.add(newEntry);
	return this;
    }

    void sort() {
	if (entries.size() > 1) {
	    Collections.sort(entries);
	}
    }

    /**
     * Retrieves one of this Rdn's value.
     * This is a convenience method for obtaining the value,
     * when the RDN contains a single type and value mapping,
     * which is the common RDN usage.
     * <p>
     * For a multi-valued RDN, this method returns value corresponding
     * to the type returned by {@link #getType() getType()} method.
     *
     * @return The non-null attribute value.
     */
    public Object getValue() {
	return ((RdnEntry) entries.get(0)).getValue();
    }

    /**
     * Retrieves one of this Rdn's type.
     * This is a convenience method for obtaining the type,
     * when the RDN contains a single type and value mapping,
     * which is the common RDN usage.
     * <p>
     * For a multi-valued RDN, the type/value pairs have
     * no specific order defined on them. In that case, this method
     * returns type of one of the type/value pairs.
     * The {@link #getValue() getValue()} method returns the
     * value corresponding to the type returned by this method.
     *
     * @return The non-null attribute type.
     */
    public String getType() {
	return ((RdnEntry) entries.get(0)).getType();
    }

    /**
     * Returns this Rdn as a string represented in a format defined by
     * <a href="http://ietf.org//rfc/rfc2253.txt">RFC 2253</a> and described
     * in the class description for {@link javax.naming.ldap.LdapName LdapName}.
     *
     * @return The string representation of the Rdn.
     */
    public String toString() {
	StringBuilder builder = new StringBuilder();
	int size = entries.size();
	if (size > 0) {
            builder.append(entries.get(0));
	}
	for (int next = 1; next < size; next++) {
            builder.append('+');
            builder.append(entries.get(next));
        }
        return builder.toString();
    }

    /**
     * Compares this Rdn with the specified Object for order.
     * Returns a negative integer, zero, or a positive integer as this
     * Rdn is less than, equal to, or greater than the given Object.
     * <p>
     * If obj is null or not an instance of Rdn, ClassCastException
     * is thrown.
     * <p>
     * The attribute type and value pairs of the RDNs are lined up
     * against each other and compared lexicographically. The order of
     * components in multi-valued Rdns (such as "ou=Sales+cn=Bob") is not
     * significant.
     *
     * @param obj The non-null object to compare against.
     * @return  A negative integer, zero, or a positive integer as this Rdn
     * 		is less than, equal to, or greater than the given Object.
     * @exception ClassCastException if obj is null or not a Rdn.
     * <p>
     */
    public int compareTo(Object obj) {
	if (!(obj instanceof Rdn)) {
            throw new ClassCastException("The obj is not a Rdn");
        }
	if (obj == this) {
	    return 0;
	}
	Rdn that = (Rdn) obj;
        int minSize = Math.min(entries.size(), that.entries.size());
        for (int i = 0; i < minSize; i++) {

	    // Compare a single pair of type/value pairs.
            int diff = ((RdnEntry) entries.get(i)).compareTo(
					that.entries.get(i));
            if (diff != 0) {
		return diff;
            }
        }
	return (entries.size() - that.entries.size());  // longer RDN wins
    }

    /**
     * Compares the specified Object with this Rdn for equality.
     * Returns true if the given object is also a Rdn and the two Rdns
     * represent the same attribute type and value mappings. The order of
     * components in multi-valued Rdns (such as "ou=Sales+cn=Bob") is not
     * significant.
     * <p>
     * Type and value equalilty matching is done as below:
     * <ul>
     * <li> The types are compared for equality with their case ignored.
     * <li> String values with different but equivalent usage of quoting,
     * escaping, or UTF8-hex-encoding are considered equal.
     * The case of the values is ignored during the comparison.
     * </ul>
     * <p>
     * If obj is null or not an instance of Rdn, false is returned.
     * <p>
     * @param obj object to be compared for equality with this Rdn.
     * @return true if the specified object is equal to this Rdn.
     * @see #hashCode()
     */
    public boolean equals(Object obj) {
	if (obj == this) {
	    return true;
	}
	if (!(obj instanceof Rdn)) {
	    return false;
	}
	Rdn that = (Rdn) obj;
	if (entries.size() != that.size()) {
	    return false;
	}
        for (int i = 0; i < entries.size(); i++) {
	    if (!entries.get(i).equals(that.entries.get(i))) {
		return false;
	    }
	}
	return true;
    }

    /**
     * Returns the hash code of this RDN. Two RDNs that are
     * equal (according to the equals method) will have the same
     * hash code.
     *
     * @return An int representing the hash code of this Rdn.
     * @see #equals
     */
    public int hashCode() {

	// Sum up the hash codes of the components.
        int hash = 0;

        // For each type/value pair...
        for (int i = 0; i < entries.size(); i++) {
	    hash += entries.get(i).hashCode();
        }
        return hash;
    }

    /**
     * Retrieves the {@link javax.naming.directory.Attributes Attributes}
     * view of the type/value mappings contained in this Rdn.
     *
     * @return 	The non-null attributes containing the type/value
     *		mappings of this Rdn.
     */
    public Attributes toAttributes() {
	Attributes attrs = new BasicAttributes(true);
        for (int i = 0; i < entries.size(); i++) {
	    RdnEntry entry = (RdnEntry) entries.get(i);
	    Attribute attr = attrs.put(entry.getType(), entry.getValue());
	    if (attr != null) {
		attr.add(entry.getValue());
		attrs.put(attr);
	    }
	}
        return attrs;
    }


    private static class RdnEntry implements Comparable {
	private String type;
	private Object value;

	// If non-null, a cannonical representation of the value suitable
        // for comparison using String.compareTo()
	private String comparable = null;

	String getType() {
	    return type;
	}

	Object getValue() {
	    return value;
	}

	public int compareTo(Object obj) {

	    // Any change here affecting equality must be
            // reflected in hashCode().
            RdnEntry that = (RdnEntry) obj;

            int diff = type.toUpperCase().compareTo(
                        that.type.toUpperCase());
            if (diff != 0) {
                return diff;
            }
	    if (value.equals(that.value)) {	// try shortcut
                return 0;
            }
            return getValueComparable().compareTo(
			that.getValueComparable());
        }

	public boolean equals(Object obj) {
	    if (obj == this) {
		return true;
	    }
	    if (!(obj instanceof RdnEntry)) {
		return false;
	    }

            // Any change here must be reflected in hashCode()
	    RdnEntry that = (RdnEntry) obj;
	    return (type.equalsIgnoreCase(that.type)) &&
			(getValueComparable().equals(
			that.getValueComparable()));
	}

	public int hashCode() {
	    return (type.toUpperCase().hashCode() +
		getValueComparable().hashCode());
	}

	public String toString() {
	    return type + "=" + escapeValue(value);
	}

	private String getValueComparable() {
	    if (comparable != null) {
		return comparable;		// return cached result
	    }

	    // cache result
	    if (value instanceof byte[]) {
		comparable = escapeBinaryValue((byte[]) value);
	    } else {
		comparable = ((String) value).toUpperCase();
	    }
	    return comparable;
        }
    }

    /**
     * Retrieves the number of attribute type/value pairs in this Rdn.
     * @return The non-negative number of type/value pairs in this Rdn.
     */
    public int size() {
        return entries.size();
    }

    /**
     * Given the value of an attribute, returns a string escaped according
     * to the rules specified in
     * <a href="http://ietf.org/rfc/rfc2253.txt">RFC 2253</a>.
     * <p>
     * For example, if the val is "Sue, Grabbit and Runn", the escaped
     * value returned by this method is "Sue\, Grabbit and Runn".
     * <p>
     * A string value is represented as a String and binary value
     * as a byte array.
     *
     * @param val The non-null object to be escaped.
     * @return Escaped string value.
     * @throws ClassCastException if val is is not a String or byte array.
     */
    public static String escapeValue(Object val) {
	return (val instanceof byte[])
		? escapeBinaryValue((byte[])val)
		: escapeStringValue((String)val);
    }

    /*
     * Given the value of a string-valued attribute, returns a
     * string suitable for inclusion in a DN.  This is accomplished by
     * using backslash (\) to escape the following characters:
     *	leading and trailing whitespace
     *	, = + < > # ; " \
     */
    private static final String escapees = ",=+<>#;\"\\";

    private static String escapeStringValue(String val) {

	    char[] chars = val.toCharArray();
	    StringBuilder builder = new StringBuilder(2 * val.length());

	    // Find leading and trailing whitespace.
	    int lead;	// index of first char that is not leading whitespace
	    for (lead = 0; lead < chars.length; lead++) {
		if (!isWhitespace(chars[lead])) {
		    break;
		}
	    }
	    int trail;	// index of last char that is not trailing whitespace
	    for (trail = chars.length - 1; trail >= 0; trail--) {
		if (!isWhitespace(chars[trail])) {
		    break;
		}
	    }

	    for (int i = 0; i < chars.length; i++) {
		char c = chars[i];
		if ((i < lead) || (i > trail) || (escapees.indexOf(c) >= 0)) {
		    builder.append('\\');
		}
		builder.append(c);
	    }
	    return builder.toString();
    }

    /*
     * Given the value of a binary attribute, returns a string
     * suitable for inclusion in a DN (such as "#CEB1DF80").
     * TBD: This method should actually generate the ber encoding
     * of the binary value
     */
    private static String escapeBinaryValue(byte[] val) {

	StringBuilder builder = new StringBuilder(1 + 2 * val.length);
	builder.append("#");

	for (int i = 0; i < val.length; i++) {
	    byte b = val[i];
	    builder.append(Character.forDigit(0xF & (b >>> 4), 16));
	    builder.append(Character.forDigit(0xF & b, 16));
	}
	return builder.toString();
	// return builder.toString().toUpperCase();
    }

    /**
     * Given an attribute value string formated according to the rules
     * specified in
     * <a href="http://ietf.org//rfc/rfc2253.txt">RFC 2253</a>,
     * returns the unformated value.  Escapes and quotes are
     * stripped away, and hex-encoded UTF-8 is converted to equivalent
     * UTF-16 characters. Returns a string value as a String, and a
     * binary value as a byte array.
     * <p>
     * Legal and illegal values are defined in RFC 2253.
     * This method is generous in accepting the values and does not
     * catch all illegal values.
     * Therefore, passing in an illegal value might not necessarily
     * trigger an <tt>IllegalArgumentException</tt>.
     *
     * @param	val	The non-null string to be unescaped.
     * @return 		Unescaped value.
     * @throws 		IllegalArgumentException When an Illegal value
     *			is provided.
     */
    public static Object unescapeValue(String val) {

	    char[] chars = val.toCharArray();
	    int beg = 0;
	    int end = chars.length;

	    // Trim off leading and trailing whitespace.
	    while ((beg < end) && isWhitespace(chars[beg])) {
		++beg;
	    }

	    while ((beg < end) && isWhitespace(chars[end - 1])) {
		--end;
	    }

	    // Add back the trailing whitespace with a preceeding '\'
	    // (escaped or unescaped) that was taken off in the above
	    // loop. Whether or not to retain this whitespace is decided below.
	    if (end != chars.length &&
		    (beg < end) &&
		    chars[end - 1] == '\\') {
		end++;
	    }
	    if (beg >= end) {
		return "";
	    }

	    if (chars[beg] == '#') {
		// Value is binary (eg: "#CEB1DF80").
		return decodeHexPairs(chars, ++beg, end);
	    }

	    // Trim off quotes.
	    if ((chars[beg] == '\"') && (chars[end - 1] == '\"')) {
		++beg;
		--end;
	    }

	    StringBuilder builder = new StringBuilder(end - beg);
	    int esc = -1; // index of the last escaped character

	    for (int i = beg; i < end; i++) {
		if ((chars[i] == '\\') && (i + 1 < end)) {
		    if (!Character.isLetterOrDigit(chars[i + 1])) {
			++i;				// skip backslash
			builder.append(chars[i]);	// snarf escaped char
			esc = i;
		    } else {

			// Convert hex-encoded UTF-8 to 16-bit chars.
			byte[] utf8 = getUtf8Octets(chars, i, end);
			if (utf8.length > 0) {
			    try {
				builder.append(new String(utf8, "UTF8"));
			    } catch (java.io.UnsupportedEncodingException e) {
				// shouldn't happen
			    }
			    i += utf8.length * 3 - 1;
			} else { // no utf8 bytes available, invalid DN

			    // '/' has no meaning, throw exception
			    throw new IllegalArgumentException(
				"Not a valid attribute string value:" +
				val + ",improper usage of backslash");
			}
		    }
		} else {
		    builder.append(chars[i]);	// snarf unescaped char
		}
	    }

	    // Get rid of the unescaped trailing whitespace with the
	    // preceeding '\' character that was previously added back.
	    int len = builder.length();
	    if (isWhitespace(builder.charAt(len - 1)) && esc != (end - 1)) {
		builder.setLength(len - 1);
	    }
	    return builder.toString();
	}


	/*
	 * Given an array of chars (with starting and ending indexes into it)
	 * representing bytes encoded as hex-pairs (such as "CEB1DF80"),
	 * returns a byte array containing the decoded bytes.
	 */
	private static byte[] decodeHexPairs(char[] chars, int beg, int end) {
	    byte[] bytes = new byte[(end - beg) / 2];
	    for (int i = 0; beg + 1 < end; i++) {
		int hi = Character.digit(chars[beg], 16);
		int lo = Character.digit(chars[beg + 1], 16);
		if (hi < 0 || lo < 0) {
		    break;
		}
		bytes[i] = (byte)((hi<<4) + lo);
		beg += 2;
	    }
	    if (beg != end) {
		throw new IllegalArgumentException(
			"Illegal attribute value: " + new String(chars));
	    }
	    return bytes;
	}

	/*
	 * Given an array of chars (with starting and ending indexes into it),
	 * finds the largest prefix consisting of hex-encoded UTF-8 octets,
	 * and returns a byte array containing the corresponding UTF-8 octets.
	 *
	 * Hex-encoded UTF-8 octets look like this:
	 *	\03\B1\DF\80
	 */
	private static byte[] getUtf8Octets(char[] chars, int beg, int end) {
	    byte[] utf8 = new byte[(end - beg) / 3];	// allow enough room
	    int len = 0;	// index of first unused byte in utf8

	    while ((beg + 2 < end) &&
		   (chars[beg++] == '\\')) {
		int hi = Character.digit(chars[beg++], 16);
		int lo = Character.digit(chars[beg++], 16);
		if (hi < 0 || lo < 0) {
		   break;
		}
		utf8[len++] = (byte)((hi<<4) + lo);
	    }
	    if (len == utf8.length) {
		return utf8;
	    } else {
		byte[] res = new byte[len];
		System.arraycopy(utf8, 0, res, 0, len);
		return res;
	    }
	}

    /*
     * Best guess as to what RFC 2253 means by "whitespace".
     */
    private static boolean isWhitespace(char c) {
        return (c == ' ' || c == '\r');
    }

    /**
     * Serializes only the unparsed RDN, for compactness and to avoid
     * any implementation dependency.
     *
     * @serialData	The RDN string
     */
    private void writeObject(ObjectOutputStream s)
            throws java.io.IOException {
	s.defaultWriteObject();
        s.writeObject(toString());
    }

    private void readObject(ObjectInputStream s)
            throws IOException, ClassNotFoundException {
	s.defaultReadObject();
	entries = new ArrayList(DEFAULT_SIZE);
        String unparsed = (String) s.readObject();
        try {
	    (new Rfc2253Parser(unparsed)).parseRdn(this);
        } catch (InvalidNameException e) {
            // shouldn't happen
            throw new java.io.StreamCorruptedException(
                    "Invalid name: " + unparsed);
        }
    }
}

Generated By: JavaOnTracks Doclet 0.1.4     ©Thibaut Colar