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 {@link Socks5CommandRequest}.
23   */
24  public class Socks5CommandType implements Comparable<Socks5CommandType> {
25  
26      public static final Socks5CommandType CONNECT = new Socks5CommandType(0x01, "CONNECT");
27      public static final Socks5CommandType BIND = new Socks5CommandType(0x02, "BIND");
28      public static final Socks5CommandType UDP_ASSOCIATE = new Socks5CommandType(0x03, "UDP_ASSOCIATE");
29  
30      public static Socks5CommandType valueOf(byte b) {
31          switch (b) {
32          case 0x01:
33              return CONNECT;
34          case 0x02:
35              return BIND;
36          case 0x03:
37              return UDP_ASSOCIATE;
38          }
39  
40          return new Socks5CommandType(b);
41      }
42  
43      private final byte byteValue;
44      private final String name;
45      private String text;
46  
47      public Socks5CommandType(int byteValue) {
48          this(byteValue, "UNKNOWN");
49      }
50  
51      public Socks5CommandType(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 Socks5CommandType)) {
68              return false;
69          }
70  
71          return byteValue == ((Socks5CommandType) obj).byteValue;
72      }
73  
74      @Override
75      public int compareTo(Socks5CommandType 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  }