View Javadoc
1   /*
2    * Copyright 2013 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package io.netty.handler.codec.socksx.v5;
18  
19  import io.netty.util.internal.ObjectUtil;
20  
21  /**
22   * The type of address in {@link Socks5CommandRequest} and {@link Socks5CommandResponse}.
23   */
24  public class Socks5AddressType implements Comparable<Socks5AddressType> {
25  
26      public static final Socks5AddressType IPv4 = new Socks5AddressType(0x01, "IPv4");
27      public static final Socks5AddressType DOMAIN = new Socks5AddressType(0x03, "DOMAIN");
28      public static final Socks5AddressType IPv6 = new Socks5AddressType(0x04, "IPv6");
29  
30      public static Socks5AddressType valueOf(byte b) {
31          switch (b) {
32          case 0x01:
33              return IPv4;
34          case 0x03:
35              return DOMAIN;
36          case 0x04:
37              return IPv6;
38          }
39  
40          return new Socks5AddressType(b);
41      }
42  
43      private final byte byteValue;
44      private final String name;
45      private String text;
46  
47      public Socks5AddressType(int byteValue) {
48          this(byteValue, "UNKNOWN");
49      }
50  
51      public Socks5AddressType(int byteValue, String name) {
52          this.name = ObjectUtil.checkNotNull(name, "name");
53          this.byteValue = (byte) byteValue;
54      }
55  
56      public byte byteValue() {
57          return byteValue;
58      }
59  
60      @Override
61      public int hashCode() {
62          return byteValue;
63      }
64  
65      @Override
66      public boolean equals(Object obj) {
67          if (!(obj instanceof Socks5AddressType)) {
68              return false;
69          }
70  
71          return byteValue == ((Socks5AddressType) obj).byteValue;
72      }
73  
74      @Override
75      public int compareTo(Socks5AddressType o) {
76          return byteValue - o.byteValue;
77      }
78  
79      @Override
80      public String toString() {
81          String text = this.text;
82          if (text == null) {
83              this.text = text = name + '(' + (byteValue & 0xFF) + ')';
84          }
85          return text;
86      }
87  }