View Javadoc

1   /**
2    *
3    */
4   package javax.jmdns.impl.constants;
5   
6   /**
7    * DNS label.
8    * 
9    * @author Arthur van Hoff, Jeff Sonstein, Werner Randelshofer, Pierre Frisch, Rick Blair
10   */
11  public enum DNSLabel {
12      /**
13       * This is unallocated.
14       */
15      Unknown("", 0x80),
16      /**
17       * Standard label [RFC 1035]
18       */
19      Standard("standard label", 0x00),
20      /**
21       * Compressed label [RFC 1035]
22       */
23      Compressed("compressed label", 0xC0),
24      /**
25       * Extended label [RFC 2671]
26       */
27      Extended("extended label", 0x40);
28  
29      /**
30       * DNS label types are encoded on the first 2 bits
31       */
32      static final int     LABEL_MASK     = 0xC0;
33      static final int     LABEL_NOT_MASK = 0x3F;
34  
35      private final String _externalName;
36  
37      private final int    _index;
38  
39      DNSLabel(String name, int index) {
40          _externalName = name;
41          _index = index;
42      }
43  
44      /**
45       * Return the string representation of this type
46       * 
47       * @return String
48       */
49      public String externalName() {
50          return _externalName;
51      }
52  
53      /**
54       * Return the numeric value of this type
55       * 
56       * @return String
57       */
58      public int indexValue() {
59          return _index;
60      }
61  
62      /**
63       * @param index
64       * @return label
65       */
66      public static DNSLabel labelForByte(int index) {
67          int maskedIndex = index & LABEL_MASK;
68          for (DNSLabel aLabel : DNSLabel.values()) {
69              if (aLabel._index == maskedIndex) return aLabel;
70          }
71          return Unknown;
72      }
73  
74      /**
75       * @param index
76       * @return masked value
77       */
78      public static int labelValue(int index) {
79          return index & LABEL_NOT_MASK;
80      }
81  
82      @Override
83      public String toString() {
84          return this.name() + " index " + this.indexValue();
85      }
86  
87  }