1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.netty.handler.codec.socksx.v5;
18
19 import io.netty.util.internal.ObjectUtil;
20
21
22
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 }