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
20
21
22 public class Socks5CommandType implements Comparable<Socks5CommandType> {
23
24 public static final Socks5CommandType CONNECT = new Socks5CommandType(0x01, "CONNECT");
25 public static final Socks5CommandType BIND = new Socks5CommandType(0x02, "BIND");
26 public static final Socks5CommandType UDP_ASSOCIATE = new Socks5CommandType(0x03, "UDP_ASSOCIATE");
27
28 public static Socks5CommandType valueOf(byte b) {
29 switch (b) {
30 case 0x01:
31 return CONNECT;
32 case 0x02:
33 return BIND;
34 case 0x03:
35 return UDP_ASSOCIATE;
36 }
37
38 return new Socks5CommandType(b);
39 }
40
41 private final byte byteValue;
42 private final String name;
43 private String text;
44
45 public Socks5CommandType(int byteValue) {
46 this(byteValue, "UNKNOWN");
47 }
48
49 public Socks5CommandType(int byteValue, String name) {
50 if (name == null) {
51 throw new NullPointerException("name");
52 }
53
54 this.byteValue = (byte) byteValue;
55 this.name = name;
56 }
57
58 public byte byteValue() {
59 return byteValue;
60 }
61
62 @Override
63 public int hashCode() {
64 return byteValue;
65 }
66
67 @Override
68 public boolean equals(Object obj) {
69 if (!(obj instanceof Socks5CommandType)) {
70 return false;
71 }
72
73 return byteValue == ((Socks5CommandType) obj).byteValue;
74 }
75
76 @Override
77 public int compareTo(Socks5CommandType o) {
78 return byteValue - o.byteValue;
79 }
80
81 @Override
82 public String toString() {
83 String text = this.text;
84 if (text == null) {
85 this.text = text = name + '(' + (byteValue & 0xFF) + ')';
86 }
87 return text;
88 }
89 }