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    *   http://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  /**
20   * The type of {@link Socks5CommandRequest}.
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  }