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 status of {@link Socks4CommandResponse}.
22   */
23  public class Socks4CommandStatus implements Comparable<Socks4CommandStatus> {
24  
25      public static final Socks4CommandStatus SUCCESS = new Socks4CommandStatus(0x5a, "SUCCESS");
26      public static final Socks4CommandStatus REJECTED_OR_FAILED = new Socks4CommandStatus(0x5b, "REJECTED_OR_FAILED");
27      public static final Socks4CommandStatus IDENTD_UNREACHABLE = new Socks4CommandStatus(0x5c, "IDENTD_UNREACHABLE");
28      public static final Socks4CommandStatus IDENTD_AUTH_FAILURE = new Socks4CommandStatus(0x5d, "IDENTD_AUTH_FAILURE");
29  
30      public static Socks4CommandStatus valueOf(byte b) {
31          switch (b) {
32          case 0x5a:
33              return SUCCESS;
34          case 0x5b:
35              return REJECTED_OR_FAILED;
36          case 0x5c:
37              return IDENTD_UNREACHABLE;
38          case 0x5d:
39              return IDENTD_AUTH_FAILURE;
40          }
41  
42          return new Socks4CommandStatus(b);
43      }
44  
45      private final byte byteValue;
46      private final String name;
47      private String text;
48  
49      public Socks4CommandStatus(int byteValue) {
50          this(byteValue, "UNKNOWN");
51      }
52  
53      public Socks4CommandStatus(int byteValue, String name) {
54          this.name = ObjectUtil.checkNotNull(name, "name");
55          this.byteValue = (byte) byteValue;
56      }
57  
58      public byte byteValue() {
59          return byteValue;
60      }
61  
62      public boolean isSuccess() {
63          return byteValue == 0x5a;
64      }
65  
66      @Override
67      public int hashCode() {
68          return byteValue;
69      }
70  
71      @Override
72      public boolean equals(Object obj) {
73          if (!(obj instanceof Socks4CommandStatus)) {
74              return false;
75          }
76  
77          return byteValue == ((Socks4CommandStatus) obj).byteValue;
78      }
79  
80      @Override
81      public int compareTo(Socks4CommandStatus o) {
82          return byteValue - o.byteValue;
83      }
84  
85      @Override
86      public String toString() {
87          String text = this.text;
88          if (text == null) {
89              this.text = text = name + '(' + (byteValue & 0xFF) + ')';
90          }
91          return text;
92      }
93  }