View Javadoc
1   /*
2    * Copyright 2012 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  package io.netty.handler.codec.socksx.v4;
17  
18  import io.netty.util.internal.ObjectUtil;
19  
20  /**
21   * The type of {@link Socks4CommandRequest}.
22   */
23  public class Socks4CommandType implements Comparable<Socks4CommandType> {
24  
25      public static final Socks4CommandType CONNECT = new Socks4CommandType(0x01, "CONNECT");
26      public static final Socks4CommandType BIND = new Socks4CommandType(0x02, "BIND");
27  
28      public static Socks4CommandType valueOf(byte b) {
29          switch (b) {
30          case 0x01:
31              return CONNECT;
32          case 0x02:
33              return BIND;
34          }
35  
36          return new Socks4CommandType(b);
37      }
38  
39      private final byte byteValue;
40      private final String name;
41      private String text;
42  
43      public Socks4CommandType(int byteValue) {
44          this(byteValue, "UNKNOWN");
45      }
46  
47      public Socks4CommandType(int byteValue, String name) {
48          this.name = ObjectUtil.checkNotNull(name, "name");
49          this.byteValue = (byte) byteValue;
50      }
51  
52      public byte byteValue() {
53          return byteValue;
54      }
55  
56      @Override
57      public int hashCode() {
58          return byteValue;
59      }
60  
61      @Override
62      public boolean equals(Object obj) {
63          if (!(obj instanceof Socks4CommandType)) {
64              return false;
65          }
66  
67          return byteValue == ((Socks4CommandType) obj).byteValue;
68      }
69  
70      @Override
71      public int compareTo(Socks4CommandType o) {
72          return byteValue - o.byteValue;
73      }
74  
75      @Override
76      public String toString() {
77          String text = this.text;
78          if (text == null) {
79              this.text = text = name + '(' + (byteValue & 0xFF) + ')';
80          }
81          return text;
82      }
83  }